1.添加maven依賴:在 pom.xml 中添加struts-core的依賴
添加位置在 <dependencies> 與 <!-- log start --> 之間
<!-- junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.9</version> <scope>test</scope> </dependency> <!-- struts2依賴包 --> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-core</artifactId> <version>2.3.14</version> </dependency>
添加完成后保存會檢查本地庫有無包,沒有會自動下載。
若下載失敗則會報錯,此時需要去提示的路徑刪除文件夾或更新maven項目。
2.新建一個Action:在src/main/java 下新建 UserAction.java
1 package com.stu2.action; 2 3 import java.io.UnsupportedEncodingException; 4 5 import javax.servlet.http.HttpServletRequest; 6 import javax.servlet.http.HttpServletResponse; 7 8 import org.apache.struts2.ServletActionContext; 9 10 import com.opensymphony.xwork2.ActionSupport; 11 12 public class UserAction extends ActionSupport { 13 14 private static final long serialVersionUID = 1L; 15 16 public String execute(){ 17 return SUCCESS; 18 } 19 20 public String login(){ 21 22 try { 23 HttpServletRequest request = ServletActionContext.getRequest(); 24 HttpServletResponse response = ServletActionContext.getResponse(); 25 26 // 常見的兩行確保為utf-8格式的代碼(如果有過濾器則不需要) 27 request.setCharacterEncoding("UTF-8"); 28 response.setContentType("text/html;charset=utf-8"); 29 30 String username = request.getParameter("username"); 31 String password = request.getParameter("password"); 32 System.out.println("name->"+username+",password->"+password); 33 34 if("admin".equals(username) && "123456".equals(password)){ 35 return ERROR; 36 }else{ 37 return "login"; 38 } 39 40 } catch (UnsupportedEncodingException e) { 41 e.printStackTrace(); 42 } 43 return ERROR; 44 } 45 46 }
3.配置 Struts2.xml :一般放在類路徑下,如果不在類路徑則需要在 web.xml 中配置路徑(步驟4)
1 <?xml version="1.0" encoding="UTF-8" ?> 2 3 <!DOCTYPE struts PUBLIC 4 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 5 "http://struts.apache.org/dtds/struts-2.3.dtd"> 6 7 <struts> 8 9 <constant name="struts.i18n.reload" value="false" /> 10 <constant name="struts.devMode" value="false" /> 11 12 <include file="struts-default.xml" /> 13 14 <package name="default" extends="struts-default" namespace="/"> 15 16 <action name="login" class="com.stu2.action.UserAction" method="login"> 17 <result name="success">index.jsp</result> 18 <result name="login">login.jsp</result> 19 <result name="error">error.jsp</result> 20 </action> 21 22 </package> 23 24 </struts>
注意其中類的路徑
4.配置 web.xml 監聽器等
1 <!DOCTYPE web-app PUBLIC 2 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" 3 "http://java.sun.com/dtd/web-app_2_3.dtd" > 4 5 <web-app> 6 <display-name>Archetype Created Web Application</display-name> 7 <init-param> 8 <param-name>config</param-name> 9 <param-value>../../resources/struts.xml</param-value> 10 </init-param> 11 12 <filter> 13 <filter-name>struts2</filter-name> 14 <filter-class> 15 org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter 16 </filter-class> 17 </filter> 18 <filter-mapping> 19 <filter-name>struts2</filter-name> 20 <url-pattern>/*</url-pattern> 21 </filter-mapping> 22 23 <welcome-file-list> 24 <welcome-file>login.jsp</welcome-file> 25 </welcome-file-list> 26 </web-app>
5.建立頁面 login.jsp index.jsp error.jsp
1 <!-- login.jsp --> 2 3 <%@ page language="java" contentType="text/html; charset=UTF-8" 4 pageEncoding="UTF-8"%> 5 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 6 <html> 7 <head> 8 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 9 <title>登錄界面</title> 10 </head> 11 12 <body> 13 <form action="login" method="post"> 14 <table> 15 <tr> 16 <td>用戶名:</td> 17 <td><input type="text" name="username" /> </td> 18 </tr> 19 <tr> 20 <td>密碼:</td> 21 <td><input type="text" name="password" /> </td> 22 </tr> 23 <tr> 24 <td colspan="2"> 25 <input type="submit" value="登錄" /> 26 <input type="reset" value="重置" /></td> 27 </tr> 28 </table> 29 </form> 30 </body> 31 </html>
1 <!-- index.jsp --> 2 3 <%@ page language="java" contentType="text/html; charset=UTF-8" 4 pageEncoding="UTF-8"%> 5 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 6 <html> 7 <head> 8 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 9 <title>登錄成功</title> 10 </head> 11 12 <body> 13 <p>進來了</p> 14 </body> 15 </html>
<!-- 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>登錄失敗</title> </head> <body> <p>沒進來</p> </body> </html>
6.注意編碼問題 此文檔代碼為UTF-8和ISO混雜 懶得改了