<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <!-- struts2的action必須放在一個指定的包空間下定義 --> <package name="default" extends="struts-default"> <!-- 定義處理請求URL爲login.action的Action --> <action name="login" class="org.qiujy.web.struts.action.LoginAction"> <!-- 定義處理結果字符串和資源之間的映射關係 --> <result name="success">/success.jsp</result> <result name="error">/error.jsp</result> </action> </package> </struts>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <!-- struts2的action必須放在一個指定的包空間下定義 --> <package name="qiujy" extends="struts-default"> <!-- 定義處理請求URL爲login.action的Action --> <action name="login" class="org.qiujy.web.struts2.action.LoginAction"> <!-- 定義處理結果字符串和資源之間的映射關係 --> <result name="success">/success.jsp</result> <result name="error">/error.jsp</result> </action> </package> <package name="my" extends="struts-default" namespace="/manage"> <!-- 定義處理請求URL爲login.action的Action --> <action name="backLogin" class="org.qiujy.web.struts2.action.LoginAction"> <!-- 定義處理結果字符串和資源之間的映射關係 --> <result name="success">/success.jsp</result> <result name="error">/error.jsp</result> </action> </package>
</struts>
對於包default:沒有指定namespace屬性。若是某個包沒有指定namespace屬性,即該包使用默認的命名空間,默認的命名空間老是""。如上配置了兩個包:default和my,配置my包時指定了該包的命名空間爲/manage。html
<struts> <include file="struts-default.xml"/> <include file="struts-user.xml"/> <include file="struts-book.xml"/> <include file="struts-shoppingCart.xml"/></struts>
見後面章節介紹。1.4. 攔截器配置:java
<struts> <constant name="struts.custom.i18n.resources" value="messages"/> </struts>
package org.qiujy.web.struts2.action; import com.opensymphony.xwork2.ActionSupport; /** *@authorqiujy *@version1.0 */ publicclass LoginAction extends ActionSupport{ private String userName; private String password; private String msg; //結果信息屬性 /** *@returnthemsg */ public String getMsg() { returnmsg; } /** *@parammsgthemsgtoset */ publicvoid setMsg(String msg) { this.msg = msg; } /** *@returntheuserName */ public String getUserName() { returnuserName; } /** *@paramuserNametheuserNametoset */ publicvoid setUserName(String userName) { this.userName = userName; } /** *@returnthepassword */ public String getPassword() { returnpassword; } /** *@parampasswordthepasswordtoset */ publicvoid setPassword(String password) { this.password = password; } /** *處理用戶請求的excute()方法 *@return結果導航字符串 *@throwsException */ public String execute() throws Exception{ if("test".equals(this.userName) && "test".equals(this.password)){ msg = "登陸成功,歡迎" + this.userName; return this.SUCCESS; }else{ msg = "登陸失敗,用戶名或密碼錯"; return this.ERROR; } } }
public String execute() throws Exception{ if("test".equals(this.userName) && "test".equals(this.password)){ msg = "登陸成功,歡迎" + this.userName; //獲取ActionContext實例,經過它來訪問Servlet API ActionContext context = ActionContext.getContext(); //看session中是否已經存放了用戶名,若是存放了:說明已經登陸了; //不然說明是第一次登陸成功 if(null != context.getSession().get("uName")){ msg = this.userName + ":你已經登陸過了!!!"; }else{ context.getSession().put("uName", this.userName); } returnthis.SUCCESS; }else{ msg = "登陸失敗,用戶名或密碼錯"; returnthis.ERROR; } }
<form method="post" action="userOpt!login.action">
package org.qiujy.web.struts2.action; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; /** *@authorqiujy *@version1.0 */ publicclass LoginAction extends ActionSupport{ private String userName; private String password; private String msg; //結果信息屬性 /** *@returnthemsg */ public String getMsg() { returnmsg; } /** *@parammsgthemsgtoset */ publicvoid setMsg(String msg) { this.msg = msg; } /** *@returntheuserName */ public String getUserName() { returnuserName; } /** *@paramuserNametheuserNametoset */ publicvoid setUserName(String userName) { this.userName = userName; } /** *@returnthepassword */ public String getPassword() { returnpassword; } /** *@parampasswordthepasswordtoset */ publicvoid setPassword(String password) { this.password = password; } /** *處理用戶請求的login()方法 *@return結果導航字符串 *@throwsException */ public String login() throws Exception{ if("test".equals(this.userName) && "test".equals(this.password)){ msg = "登陸成功,歡迎" + this.userName; //獲取ActionContext實例,經過它來訪問Servlet API ActionContext context = ActionContext.getContext(); //看session中是否已經存放了用戶名,若是存放了:說明已經登陸了; //不然說明是第一次登陸成功 if(null != context.getSession().get("uName")){ msg = this.userName + ":你已經登陸過了!!!"; }else{ context.getSession().put("uName", this.userName); } returnthis.SUCCESS; }else{ msg = "登陸失敗,用戶名或密碼錯"; returnthis.ERROR; } } public String regist() throws Exception{ //將用戶名,密碼添加到數據庫中 //... msg = "註冊成功。"; returnthis.SUCCESS; } }
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="my" extends="struts-default" namespace="/manage"> <!-- 定義處理請求URL爲login.action的Action --> <action name="userOpt" class="org.qiujy.web.struts2.action.LoginAction"> <!-- 定義處理結果字符串和資源之間的映射關係 --> <result name="success">/success.jsp</result> <result name="error">/error.jsp</result> </action> </package> </struts>
<%@ page language="java" pageEncoding="UTF-8"%> <html> <head> <title>用戶登陸頁面</title> </head> <body> <h2>用戶入口</h2> <hr> <form action="manage/userOpt!login.action" method="post"> <table border="1"> <tr> <td>用戶名:</td> <td><input type="text" name="userName"/></td> </tr> <tr> <td>密碼:</td> <td><input type="password" name="password"/></td> </tr> <tr> <td colspan="2"> <input type="submit" value=" 肯定 "/> </td> </tr> </table> </form> </body> </html> regist.jsp
<%@ page language="java" pageEncoding="UTF-8"%> <html> <head> <title>用戶註冊頁面</title> </head> <body> <h2>用戶註冊</h2> <hr> <form action="manage/userOpt!regist.action" method="post"> <table border="1"> <tr> <td>用戶名:</td> <td><input type="text" name="userName"/></td> </tr> <tr> <td>密碼:</td> <td><input type="password" name="password"/></td> </tr> <tr> <td colspan="2"> <input type="submit" value=" 註冊 "/> </td> </tr> </table> </form> </body> </html>
4. 運行結果:web
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="my" extends="struts-default" namespace="/manage"> <action name="userLogin" class="org.qiujy.web.struts2.action.LoginAction" method="login"> <result name="success">/success.jsp</result> <result name="error">/error.jsp</result> </action> <action name="userRegist" class="org.qiujy.web.struts2.action.LoginAction" method="regist"> <result name="success">/success.jsp</result> <result name="error">/error.jsp</result> </action> </package> </struts>
<action name="user_*" class="org.qiujy.web.struts2.action.UserAction" method="{1}"> <result name="success">/success.jsp</result> <result name="error">/error.jsp</result> </action>
如上,<action name=」user_*」>定義一系列請求URL是user_*.action模式的邏輯Action。同時method屬性值爲一個表達式{1},表示它的值是name屬性值中第一個*的值。例如:用戶請求URL爲user_login.action時,將調用到UserAction類的login方法;用戶請求URL爲user_regist.action時,將調用到UserAction類的regist方法。sql
<global-results> <result name="error">/Error.jsp</result> <result name="invalid.token">/Error.jsp</result> <result name="login" type="redirect-action">Logon!input</result> </global-results>
名字 說明
chain 用來處理Action鏈
dispatcher 用來轉向頁面,一般處理JSP,這是默認的結果類型
freeMarker 處理FreeMarker模板
httpHeader 用來控制特殊的Http行爲
redirect 重定向到一個URL
redirect-action 重定向到一個Action
stream 向瀏覽器發送InputSream對象,一般用來處理文件下載
velocity 處理Velocity模板
xslt 處理XML/XLST模板
plaintext 顯示原始文件內容,例如文件源代碼
tiles 結合Tile使用數據庫
private String nextAction; public String getNextAction() { return nextAction; }
<action name="fragment" class="FragmentAction"> <result name="next" type="redirect-action"></result> </action>
package org.qiujy.domain; publicclass User { private String userName; private String password; /** *@returntheuserName */ public String getUserName() { returnuserName; } /** *@paramuserNametheuserNametoset */ publicvoid setUserName(String userName) { this.userName = userName; } /** *@returnthepassword */ public String getPassword() { returnpassword; } /** *@parampasswordthepasswordtoset */ publicvoid setPassword(String password) { this.password = password; } }
package org.qiujy.web.struts2.action; import org.qiujy.domain.User; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; publicclass UserAction extends ActionSupport{ //定義用於封裝請求參數的模型對象 private User user = new User(); private String msg; //結果信息屬性 /** *@returntheuser */ public User getUser() { returnuser; } /** *@paramusertheusertoset */ publicvoid setUser(User user) { this.user = user; } /** *@returnthemsg */ public String getMsg() { returnmsg; } /** *@parammsgthemsgtoset */ publicvoid setMsg(String msg) { this.msg = msg; } /** *處理用戶請求的login()方法 *@return結果導航字符串 *@throwsException */ public String login() throws Exception{ String userName = user.getUserName(); String password = user.getPassword(); if("test".equals(userName) && "test".equals(password)){ msg = "登陸成功,歡迎" + userName; //獲取ActionContext實例,經過它來訪問Servlet API ActionContext context = ActionContext.getContext(); //看session中是否已經存放了用戶名,若是存放了:說明已經登陸了;不然說明是第一次登陸成功 if(null != context.getSession().get("uName")){ msg = userName + ":你已經登陸過了!!!"; }else{ context.getSession().put("uName", userName); } returnthis.SUCCESS; }else{ msg = "登陸失敗,用戶名或密碼錯"; returnthis.ERROR; } } public String regist() throws Exception{ //將用戶名,密碼添加到數據庫中 //... msg = "註冊成功。"; returnthis.SUCCESS; } }
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="my" extends="struts-default" namespace="/manage"> <action name="userOpt" class="org.qiujy.web.struts2.action.UserAction"> <result name="success">/success.jsp</result> <result name="error">/error.jsp</result> </action> </package> </struts>
<%@ page language="java" pageEncoding="UTF-8"%> <html> <head> <title>用戶登陸頁面</title> </head> <body> <h2>用戶入口</h2> <hr> <form action="manage/userOpt!login.action" method="post"> <table border="1"> <tr> <td>用戶名:</td> <td><input type="text" name="user.userName"/></td> </tr> <tr> <td>密碼:</td> <td><input type="password" name="user.password"/></td> </tr> <tr> <td colspan="2"> <input type="submit" value=" 肯定 "/> </td> </tr> </table> </form> </body> </html>
public String regist() throws Exception{ //將用戶名,密碼添加到數據庫中 //... //msg = "註冊成功。"; if(true){ throw new java.sql.SQLException("沒有數據庫驅動程序"); } return this.SUCCESS; }
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="my" extends="struts-default" namespace="/manage"> <!-- 定義全局處理結果 --> <global-results> <!-- 邏輯名爲sql的結果,映射到/exception.jsp頁面 --> <result name="sql">/exception.jsp</result> </global-results> <global-exception-mappings> <!-- 當Action拋出SQLException異常時,轉入名爲sql的結果 --> <exception-mapping exception="java.sql.SQLException" result="sql"/> </global-exception-mappings> <action name="userOpt" class="org.qiujy.web.struts2.action.UserAction"> <result name="success">/success.jsp</result> <result name="error">/error.jsp</result> </action> </package> </struts>
<%@ page language="java" pageEncoding="utf-8"%> <%@ taglib uri="/struts-tags" prefix="s" %> <html> <head> <title>異常信息</title> </head> <body> <h2> 出現異常啦 </h2> <hr/> <h3 style="color:red"> <!-- 得到異常對象 --> <s:property value="exception.message"/> </h3> <br/> <!-- 異常堆棧信息 --> <s:property value="exceptionStack"/> </html>
7. Struts2的國際化:apache
1. 在工程中加入Struts支持瀏覽器
2. 編輯ApplicationResource.properties文件,在其中加入要使用國際化的信息, 例如: lable.welcome.china=Welcome!!!session
3. 建立英文資源文件ApplicationResource_en.properitesapp
4. 建立臨時中文資源文件ApplicationResource_temp.properites 例如:lable.welcom.china=中國歡迎您!框架
5. 對臨時中文資源文件進行編碼轉換。可使用myeclipse的插件,也能夠在dos下執行:native2ascii -encoding gb2312 ApplicationResource_temp.properties ApplicationResource_zh_CN.properties
6. 在jsp中加入struts的bean標記庫 <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>顯示信息: <bean:message key="label.welcome.china">能夠在struts-config.xml文件中定義多個資源包,而每一個資源包可以使用key屬性指定包的名稱。