Struts的html:errors 没有显示的解决办法
    
版权声明:
原创作品,允许转载,转载时请务必以超链接形式标明
文章 原始出处
 、作者信息和本声明。否则将追究法律责任。http://icansoft.blog.51cto.com/268543/56608
- 在struts-config.xml必须设置需要验证的ActionForm
<action
????? attribute="findSellForm"
?????validate="true"
???? input="/index.jsp"
????? name="findSellForm"
?????
 path="/findSell"
????? scope="request"
????? 
type="ucshop.action.FindSellAction" >
????? <forward 
name="fail" path="/index.jsp" />
????? <forward name="success" 
path="/findsell.jsp" />
??? </action>
 
validate
默认值是true的,所以可以省略;如果不想验证,则设为false,这样的话,后面的就不用看了!
input要指明,检
验流程:当执行validate方法时,返回的ActionErrors检查内部是否存在元素,有则返回到原来的input指定的页面;否则继续前进,执
行Action的execute方法!到时候forward的fail和success才起作用!
 
- JSP页面
<html:form action="/findSell">
?物品分类: <html:select 
property="productType">
??<html:option value="电脑配件"/>
??<html:option
 value="数码产品"/>
??<html:option value="运动用品"/>
??<html:option
 value="生活用品"/>
??<html:option value="户外用品"/>
??<html:option
 value="其他"/>
?</html:select>
<br/><br/>
?物
品名称: <html:text property="name"/><html:errors
 property="sellname"/>
<br/><br/>
?<html:submit
 value="搜索"/>
</html:form>
 
注意:
 加蓝的语句中property的值sellname是对应下面的validate()方法中的"errors.add("sellname"
, new
 
ActionMessage("errors.productNameEmpty"
)); 
"的selllname!
 
- ActionForm的validate
下面的代码是struts的1.2版本的:
public
 ActionErrors 
validate(ActionMapping mapping, 
??????HttpServletRequest request) { 
????????????????//创建ActionErrors 
??ActionErrors errors = new
 ActionErrors(); 
?? 
??//物品名称的检验 
??if
 
(getName() == null
 || 
getName().trim().equals("")) 
??{ 
??????errors.add("sellname"
, new
 
ActionMessage("errors.productNameEmpty"
)); 
??}
 
??//返回ActionErrors 
??return
 errors;?
}
 
 
自struts1.2,
 ActionError类都Deprecated,
不再赞成使用,官方建议使用ActionMessage类,所以
在struts1.2以下版本的,上面的"errors.add("sellname"
, new
 ActionMessage("errors.productNameEmpty"
));
 "改为errors.add("sellname"
, new
 ActionError("errors.productNameEmpty"
));
 
同时ActionErrors的GLOBAL_ERROR 
被Deprecated,被ActionMessages.GLOBAL_MESSAGE 代替!
========================================
有
时候需要在Action的execute()方法里面进行例如权限的验证.
举个例子,validate()是检查用户名和密码数据是否为空,当用
户输入了完整信息后,执行execute(),这时若发现"用户名或密码有误",,就需要显示错误信息
public
 class
 
ServerValidationAction extends
 Action { 
????public
 ActionFor