1. 程式人生 > >struts2入門例子 登陸demo!非常詳細的圖解

struts2入門例子 登陸demo!非常詳細的圖解

1.下載所需的jar包

下載地址:https://struts.apache.org/download.cgi#struts25101


下載完後解壓,在lid資料夾裡面找以下這幾個jar包,基本功能就只需要這些


複製這些jar包到你新建的web專案WebContent/WEB-INF/lib下

右擊你的web專案如圖:




在WEB-INF/lib目錄下有你剛才複製過來的jar包

全部選中點選OK,jar就全部匯入進來了

這是我的整個目錄結構:


接下來就開始下程式碼了

配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
    <welcome-file-list> 
    <welcome-file>index.jsp</welcome-file> 
  </welcome-file-list>
  <filter> 
    <filter-name>struts2</filter-name> 
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> 
  </filter> 
  <filter-mapping> 
    <filter-name>struts2</filter-name> 
    <url-pattern>/*</url-pattern> 
  </filter-mapping>
 
</web-app>

寫DemoLog.java
package com.strtus2.action;

import com.opensymphony.xwork2.Action;

public class DemoLog implements Action {
   private String uname;
   private String upass;
	@Override
	public String execute() throws Exception {
		System.out.println("姓名:"+uname);
		System.out.println("密碼:"+upass);
		if(uname.equals("ykp")&&upass.equals("123")){
			
			return SUCCESS;
		}
		return "error";
	}
	public String getUname() {
		return uname;
	}
	public void setUname(String uname) {
		this.uname = uname;
	}
	public String getUpass() {
		return upass;
	}
	public void setUpass(String upass) {
		this.upass = upass;
	}

}

struts.xml
<?xml version="1.0" encoding="UTF-8" ?> 
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd" >
<struts>
	<package name="default" namespace="/" extends="struts-default">
		<action name="login" class="com.strtus2.action.DemoLog">
			<result name="success">/Helloworld.jsp</result>
			<result name="error">/error.jsp</result>
		</action>
	</package>
</struts>


三個jsp檔案

error.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<h1>登陸出錯</h1>
</body>
</html>

Helloword.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>

<h1>Hello Strtus2!!!!你好</h1>
</body>
</html>
index.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<form action="login"> 
      使用者名稱:<input type="text" name="uname"><br> 
      密 碼:<input type="text" name="upass"><br> 
      <input type="submit" value="登陸" /> 
    </form> 
</body>
</html>

現在基本完成了 可以執行專案了

我今天剛學習struts2,完成的小Demo,寫片文章記錄一下,希望可以幫助到有需要的人

另外有大牛看發現錯誤的話麻煩請告訴我,謝謝啦!