日期:2014-05-17  浏览次数:20694 次

大侠们帮忙啊!!struts2获取验证信息时有问题
struts.xml
<action name="loginAction" class="loginAction" method="doLogin">
  <result name="l_success">/index.jsp</result>
  <result name="l_faile">/login.jsp</result>
  <result name="input">/login.jsp</result>
</action>

login.jsp

<div align="center" style="margin-top:200px">
<s:fielderror/>
<form action="loginAction.action" method="Post">
<table height="100" border="3">
<tr>
<td>用户名:</td>
<td><input type="text" name="userName"></td>
</tr>
<tr>
<td>密 码:</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="登录"> <input type="button" value="注册"/></td>
</tr>
</table>
</form>
</div>
LoginAction

public String doLogin() {
return "l_success";
}

public void validateDoLogin() {
if (userName == null || userName.equals("")) {
addFieldError("user.userName","请输入用户名!");

}
if (password == null || password.equals("")) {
addFieldError("user.userPassword","请输入密码!");

}
if (userName.length() > 15 || userName.length() < 5) {
addFieldError("user.userName","用户名不能大于15或小于5个字符!");

}
if (password.length() > 20|| password.length() < 5) {
addFieldError("user.userPassword","密码不能大于20或小于5个字符!");

}

}

为什么连续点击登录总是连上一次的错误信息都从fileError中取出啊,连续点击两次,如下
  * 请输入用户名!
  * 用户名不能大于15或小于5个字符!
  * 请输入用户名!
  * 用户名不能大于15或小于5个字符!
  * 请输入密码!
  * 密码不能大于20或小于5个字符!
  * 请输入密码!
  * 密码不能大于20或小于5个字符!




------解决方案--------------------
你用的spring 管理所有的类对吧? 
是这样子的,spring注入的类默认是单例的,即scope="singleton",所以你所有的请求都是 一个同一个实例,错误信息就累积起来了,只要把注入的bean中加scope="prototype"就可以了。。加油~~