解决 struts 中<html:errors/>错误显示不出来问题。
1。在struts-config.xml必须设置需要验证的 action 。其中 validate="true" input="/error.jsp"。使validate 函数有效,input="/error.jsp" 当errors不为空时跳转的地址 <action attribute="userRegist" name="registForm" path="/regist" scope="request" type="com.tplayer.action.RegistAction" validate="true" input="/error.jsp"> 2。配置资源文件 ApplicationResources.properties 如:在资源文件中 配置errors.username=userName must be filled 3。在validate 方法中处理错误验证时必须把在资源中心配置的变量引入 ActionErrors errors=new ActionErrors(); ActionError eroorDetail=new ActionError("errors.username");//引入资源文件的定义 //1.0中的用法 ActionMessage eroorDetail=new ActionMessage("errors.username");//1.2中的用法 errors.add("test",eroorDetail); 4。页面的表示(error.jsp) <%@ page contentType="text/html; charset=GBK" language="java"%> <%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" divfix="bean" %> <%@ taglib uri="http://jakarta.apache.org/struts/tags-html" divfix="html" %> <%@ taglib uri="http://jakarta.apache.org/struts/tags-logic" divfix="logic" %> <head> <meta http-equiv="Content-Type" content="text/html; charset=GBK" />
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" divfix="bean" %> <%@ taglib uri="http://jakarta.apache.org/struts/tags-html" divfix="html" %> <%@ taglib uri="http://jakarta.apache.org/struts/tags-logic" divfix="logic" %> <html:errors/> 如果 properties文件包函有中文的时候,错误信息中文部分会乱码。必须将 Application.propertyies文件编码:如 D:\first_struct\webRoot\WEB-INF\classes>native2ascii -encoding utf-8 ApplicationResources.properties ApplicationResources_zh_CN.properties。重启服务器运行就可以了 如有不明白的地方QQ:272468801留言
本文来源:http://blog.csdn.net/tangcx/archive/2007/08/20/1751256.aspx
|