在struts2中action的分類有:繼承 ActionSupport 實現 Action,模型驅動(ModelDriven)的 Action,多方法的 Action三種方式。
1、繼承 ActionSupport 實現 Action
通過繼承 ActionSupport 來實現 Action 是我們的推薦做法,因爲 ActionSupport 中提供了輸入驗證、國際化、execute 等常用方法,使得編寫 Action 時代碼很簡單。
1.1 UserAction.java
package com.lzugis.action; import com.opensymphony.xwork2.ActionSupport; public class UserAction extends ActionSupport { /** * */ private static final long serialVersionUID = 1L; private String username; private String userpass; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getUserpass() { return userpass; } public void setUserpass(String userpass) { this.userpass = userpass; } @Override public String execute() throws Exception { if (username.equals("admin") && userpass.equals("admin")) { return "success"; } else { return "error"; } } }
1.2 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> <!-- 定義包管理配置的action 繼承struts-default.xml中的配置 --> <package name="action" extends="struts-default"> <!-- 定義Action(login.action) --> <action name="login" class="com.lzugis.action.UserAction"> <!-- 定義轉發路徑對應的字符串名 --> <result name="success">/Success.jsp</result> <result name="error">/Error.jsp</result> </action> </package> </struts>
1.3 userlogin.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!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 style="font-family:Times New Roman"> <form action="login.action" method="post"> 用戶名: <!-- 參數名和action中的屬性名一樣 --> <input type="text" name="username"><br> 密 碼: <input type="password" name="userpass"> <br> <input type="submit" name="subm" value="提交"> <input type="reset" name="reset" value="取消"> </form> </body> </html>
1.4 action響應結果
1.4.1 Success.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@ taglib uri="/struts-tags" prefix="s"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!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> <h1>歡迎<s:property value="username" />,登錄</h1> </body> </html>
1.4.2 Error.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!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> <h1>用戶名或者密碼錯誤</h1> </body> </html>
2、模型驅動(ModelDriven)的 Action
Struts2 的 Action 屬於 MVC 模型層, Action 中的方法代表業務邏輯, Action 中的屬性代表請求中的參數,當頁面請求參數較多的時候,把過多的參數對象的屬性定義在 Action 中不太符合 Struts 所倡導的鬆耦合原則,所以我們推薦單獨用 JavaBean 來封裝參數,在 Action中爲 JavaBean 賦值,這就是 ModelDriven 的 Action。模型驅動的 Action 要求 Action 實現ModelDriven 接口,假如登錄頁面需要傳輸參數 username 和 userpass,我們把這 2 個參數封裝在一個數據的 JavaBean 中,然後在 Action 中定義該 JavaBean 爲 Model 即可。
2.1 UserInfo.java
package com.lzugis.javabean; public class UserInfo { private String username,userpass; public String getUsername() { return username; } public void setUsername(String username) { this.username=username; } public String getUserpass() { return userpass; } public void setUserpass(String userpass) { this.userpass=userpass; } }
2.2 UserinfoAction.java
package com.lzugis.action; import com.lzugis.javabean.UserInfo; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.ModelDriven; public class UserinfoAction extends ActionSupport implements ModelDriven<UserInfo> { /** * */ private static final long serialVersionUID = 1L; private UserInfo model; @Override public UserInfo getModel() { if(model == null) { model = new UserInfo(); } return model; } @Override public String execute() throws Exception { if (model.getUsername().equals("admin") && model.getUserpass().equals("admin")) { return "success"; } else { return "error"; } } }
2.3 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> <!-- 定義包管理配置的action 繼承struts-default.xml中的配置 --> <package name="action" extends="struts-default"> <!-- 定義Action(user.action) --> <action name="user" class="com.lzugis.action.UserinfoAction"> <!-- 定義轉發路徑對應的字符串名 --> <result name="success">/Success.jsp</result> <result name="error">/Error.jsp</result> </action> </package> </struts>
2.4 user.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!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 style="font-family:Times New Roman"> <form action="user.action" method="post"> 用戶名: <!-- 參數名和action中的屬性名一樣 --> <input type="text" name="model.username"><br> 密 碼: <input type="password" name="model.userpass"> <br> <input type="submit" name="subm" value="提交"> <input type="reset" name="reset" value="取消"> </form> </body> </html>
2.5 action結果
與1相同,在此不在贅述。
本實例通過struts中action的兩種不同方式,實現了用戶登陸的驗證。相比較繼承ActionSupport實現action,模型驅動的action比較方便。繼承ActionSupport實現action,如果實體類的屬性非常多,那麼Action中也要定義相同的屬性,這樣顯得比較繁瑣。