我的ActionForm中的validate方法为如下:
- Java code
public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) { // TODO Auto-generated method stub ActionErrors errors=new ActionErrors(); if((userName==null)||(userName=="")||(userName.length()==0)){ errors.add("userName",new ActionMessage("userInfo.userNameEmpty")); } if((userPassword==null)||(userPassword=="")||(userPassword.length()==0)){ errors.add("userPassword",new ActionMessage("userInfo.userPasswordEmpty")); } return errors; }
资源文件配置如下:
- Java code
# Resources for parameter 'cn.edu.zjut.struts.ApplicationResources'# Project LMSuserInfo.userNameEmpty=please input userName!userInfo.userPasswordEmpty=please input userPassword!
输入页面显示错误标签如下:
- Java code
<td colspan="3" background="images/login_29.png"><html:errors /> </td>
struts-config.xml配置如下:
- Java code
<form-beans > <form-bean name="userForm" type="cn.edu.zjut.ActionFrom.userForm" /> </form-beans> <action attribute="userForm" input="/admin/login.jsp" name="userForm" path="/userLogin" scope="request" type="cn.edu.zjut.Action.UserLoginAction"> <set-property property="cancellable" value="true" /> <forward name="loginSuccess" path="manageIndex" /> <forward name="loginFailure" path="/admin/loginFailure.html" /> </action>
当不输入用户名和密码时,直接回去执行action中execute,而不返回到输入页面显示错误信息,怎么回事?
------解决方案--------------------
哇,好久没用这种方式验证数据了,你看看你的struts-config.xml,加入valiedate=ture
<form-beans >
<form-bean name="userForm" type="cn.edu.zjut.ActionFrom.userForm" />
</form-beans>
<action
attribute="userForm"
input="/admin/login.jsp"
name="userForm"
path="/userLogin"
scope="request"
validate="true"
type="cn.edu.zjut.Action.UserLoginAction">
<set-property property="cancellable" value="true" />
<forward name="loginSuccess" path="manageIndex" />
<forward name="loginFailure" path="/admin/loginFailure.html" />
</action>
试试看