今天试着跑个例子,发现出了问题。
Login.JSP
<script type="text/javascript"> function regist() { var tgForm = document.forms[0]; tgForm.action = "LogReg!regist.action"; tgForm.submit(); } </script> </head> <body> <form action="LogReg!login.action" method="post" name="loginForm"> <table align="center"> <caption>用户登录</caption> <tr> <td>用户名:<input type="text" name="username"/></td> </tr> <tr> <td>密 码:<input type="text" name="password" /></td> </tr> <tr align="center"> <td colspan="2"><input type="submit" value="登录"/><input type="button" value="注册" onclick="regist()" /> </td> </tr> </table> </form> </body> </html>
|
struts.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1/EN" ""> <struts> <package name="struts2" extends="struts-default"> <action name="LogReg" class="com.augur.actions.LoginAction"> <result name="input" >/login.jsp</result> <result name="success" type="redirect">/welcome.jsp</result> <result name="error" type="redirect">/error.jsp</result> </action> </package> </struts>
|
LoginAciton.java
package com.augur.actions;
import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport;
@SuppressWarnings("serial") public class LoginAction extends ActionSupport { private String username; private String password; private String tip;
//get,set方法略 public String login() throws Exception { ActionContext ctx = ActionContext.getContext(); Integer counter = (Integer)ctx.getApplication().get("counter"); if(counter == null) { counter = 1; } else { counter++; } ctx.getApplication().put("counter", counter); ctx.getSession().put("user", getUsername()); if("xpj".equals(getUsername()) && "xpj".equals(getPassword())) { System.out.println("验证通过。。。"); ctx.put("tip", "服务器提示:您已成功登陆!"); return ActionSupport.SUCCESS; } else { System.out.println("验证失败...."); ctx.put("tip","服务器提示:登陆失败!"); return ActionSupport.ERROR; } } public String regist() throws Exception { ActionContext ac = ActionContext.getContext(); ac.put("tip","您已经成功登陆注册界面2"); return SUCCESS; } }
|
welcome.jsp
访问次数:${applicationScope.counter}<br> ${sessionScope.user},您已成功登陆!<br> Request信息: ${requestScope.tip}
|
发现在LoginAction里面设置的tip在welcome.jsp页面上获取到的值为空,看了代码也没发现什么问题。最后仔细看了配置文件,原来result 标签中我定义了type="redirect",设置了重定向,才会使设置tip的值被置空。只要把不设置type属性即可,也就是采用默认值即可。默认的是dispatcher;
阅读(13488) | 评论(0) | 转发(0) |