1 創建一個web項目。
2 導入必要的JAR文件。
放在WEB-INF下lib包里。
3 添加web.xml配置,添加啟動配置。
1 <?xml version="1.0" encoding="UTF-8"?> 2 <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"> 3 <display-name>StrutsDemo</display-name> 4 <welcome-file-list> 5 <welcome-file>index.html</welcome-file> 6 <welcome-file>index.htm</welcome-file> 7 <welcome-file>index.jsp</welcome-file> 8 <welcome-file>default.html</welcome-file> 9 <welcome-file>default.htm</welcome-file> 10 <welcome-file>default.jsp</welcome-file> 11 </welcome-file-list> 12 <!-- struts2.1.3之后的版本,可以在該過濾器之前之間定義一定的過濾器--> 13 <!-- 定義struts2 的核心控制器,用於生成ActionMapper ,攔截所有的Action請求--> 14 <filter> 15 <filter-name>struts2</filter-name> 16 <filter-class> 17 org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter 18 </filter-class> 19 </filter> 20 <filter-mapping> 21 <filter-name>struts2</filter-name> 22 <url-pattern>/*</url-pattern> 23 </filter-mapping> 24 25 </web-app>
4 編寫Action。
- Struts2直接使用Action來封裝HTTP請求參數,因此Action類應該包含與請求相對應的屬性,並為該屬性提供對應的setter和getter方法。
- 為Action類里增加一個execute方法,因為Struts2框架默認會執行這個方法。這個方法本身並不做業務邏輯處理,而是調用其他業務邏輯組件完成這部分工作。
- Action類返回一個標准的字符串,該字符串是一個邏輯視圖名,該視圖名對應實際的物理視圖。
我們來寫個用戶登錄驗證,提供用戶名和密碼兩個屬性。如果正確返回success否則返回error。
1 package com.cy.action; 2 3 import com.opensymphony.xwork2.ActionSupport; 4 5 public class LoginAction extends ActionSupport { 6 7 private static final long serialVersionUID = 1L; 8 9 private String userName; 10 private String password; 11 12 public String execute() { 13 14 if (userName.equals("hellokitty") && password.equals("123")) { 15 16 return SUCCESS; 17 } else { 18 return ERROR; 19 } 20 21 } 22 23 public String getUserName() { 24 return userName; 25 } 26 27 public void setUserName(String userName) { 28 this.userName = userName; 29 } 30 31 public String getPassword() { 32 return password; 33 } 34 35 public void setPassword(String password) { 36 this.password = password; 37 } 38 39 }
Action有一下特點:
- Struts2框架中Action是一個POJO,沒有被代碼污染。
- Struts2中的Action的execute方法不依賴於servlet API,改善了Struts1中耦合過於緊密,極大方便了單元測試。
- Struts2的Action無須用ActionForm封裝請求參數。
5 添加框架核心配置文件struts.xml文件:在WEB-INF/classes文件夾下創建struts.xml。
在struts2-core-2.3.16.jar中有strust-defalut.xml.我們需要繼承它。
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 4 "http://struts.apache.org/dtds/struts-2.3.dtd"> 5 6 <struts> 7 <package name="default" extends="struts-default"> 8 <action name="login" class="com.cy.action.LoginAction"> 9 <result name="success">/jsp/success.jsp</result> 10 <result name="error">/jsp/error.jsp</result> 11 </action> 12 </package> 13 14 15 </struts>
- 在action標簽中定義了name和class。name屬性對應的是用戶URL請求中的action名,class屬性是處理請求的實現類(注意:要包含完整路徑)。
- result標簽定義邏輯視圖和物理視圖之間的映射,在我們的Action中,如果返回的字符串是"success”則由對應的success.jsp頁面進行處理;如果返回的字符串是"error”則由error.jsp頁面進行處理。
6 編寫界面
6.1 login.jsp
1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 8 <html> 9 <head> 10 <base href="<%=basePath%>"> 11 12 <title>My JSP 'Login.jsp' starting page</title> 13 14 <meta http-equiv="pragma" content="no-cache"> 15 <meta http-equiv="cache-control" content="no-cache"> 16 <meta http-equiv="expires" content="0"> 17 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 18 <meta http-equiv="description" content="This is my page"> 19 <!-- 20 <link rel="stylesheet" type="text/css" href="styles.css"> 21 --> 22 23 </head> 24 25 <body> 26 <form action="login" method="post"> 27 用戶名:<input type="text" name="userName"><br/> 28 密 碼:<input type="password" name="password"/><br/> 29 <input type="submit" value="提交"/> 30 <input type="reset" value="重置"/> 31 </form> 32 </body> 33 </html>
6.2 success.jsp
<body> <h1>登錄成功!</h1> </body>
6.3 error.jsp
<body> <h1>登錄失敗!</h1> </body>
整體: