标签:
参考自:http://blog.csdn.net/zz_mm/article/details/5460397
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <constant name="struts.enable.DynamicMethodInvocation" value="false"></constant> <constant name="struts.devMode" value="true"></constant> <package name="default" namespace="/" extends="struts-default"> <!-- 注册拦截器 --> <interceptors> <interceptor name="authi" class="interceptor.authInterceptor"></interceptor> <!-- 自定义拦截器栈 --> <interceptor-stack name="myStack"> <interceptor-ref name="defaultStack"></interceptor-ref> <interceptor-ref name="authi"></interceptor-ref> </interceptor-stack> </interceptors> <action name="auth"> <result>/WEB-INF/pages/manager.jsp</result> <result name="login">/login.jsp</result> <!-- 引用自定义拦截器栈 --> <interceptor-ref name="myStack"></interceptor-ref> </action> <action name="login" class="action.loginAction" method="login"> <result name="success">/WEB-INF/pages/manager.jsp</result> <result name="error">/login.jsp</result> </action> </package> </struts>
如上示例的配置,配置了一个名为default的包,该包下定义了一个Action和interceptor。
<!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>
<struts> <include file="struts-default.xml"/> <include file="struts-user.xml"/> <include file="struts-book.xml"/> <include file="struts-shoppingCart.xml"/> ...... </struts>
1.4. 拦截器配置:
<struts> ...... <constant name="struts.custom.i18n.resources" value="messages"/> </struts>
表示指定了资源文件的放置在classes目录下,基本名是messages,则在classes目录下您就应该放置类似messages_zh_CN.properties,message_en.properties名的文件。
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; returnthis.SUCCESS; }else{ msg = "登录失败,用户名或密码错"; returnthis.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); } return this.SUCCESS; }else{ msg = "登录失败,用户名或密码错"; return this.ERROR; } }
Struts2中通过ActionContext来访问Servlet API,让Action彻底从Servlet API 中分离出来,最大的好处就是可以脱离Web容器测试Action。
<form method="post" action="userOpt!login.action"> |
标签:
原文地址:http://www.cnblogs.com/jasonlixuetao/p/5927287.html