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

在struts中使用 <html:errors/>不会显示错误请问是错在
点击右边红色标题查看本文完整版:在struts中使用 <html:errors/>不会显示错误请问是错在哪里?

在struts中使用 <html:errors/>不会显示错误请问是错在哪里?
? 我要使输入框不能为空 不然显示 "不能为空!" 但是实际运行是我不输入内容点提交,页面刷新了一下就是没有显示文字.
? jsp代码:
? <%@ page contentType="text/html; charset=UTF-8" language="java" %>
? <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
? <html>
? <head>
? <title>
? jsp1
? </title>
? </head>
? <body bgcolor="#ffffff">
? <html:errors/>
? <html:form action="/tAction.do" focus="username">
? <html:text property="username" /><br>
? <html:submit property="submit" value="Submit">
? </html:submit>
? </html:form>
? </body>
? </html>
?
? tActionForm的validate方法代码是
? public ActionErrors validate(ActionMapping mapping,
? ? ? HttpServletRequest request) {
? ActionErrors errors=new ActionErrors();
? if((username==null)||(username.length()<1)){
? errors.add("ee",new ActionError("不能为空!"));
? return errors;
? }else{
? return null;
? }
?
? }
? Struts_config.xml代码:
? <?xml version="1.0" encoding="UTF-8"?>
? <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
? <struts-config>
? <form-beans>
? <form-bean name="tActionForm" type="errortest.tActionForm"/>
? </form-beans>
? <action-mappings>
? <action input="/jsp1.jsp" name="tActionForm" path="/tAction" scope="request" type="errortest.tAction" validate="true">
? <forward name="succe" path="/jsp2.jsp"/>
? </action>
? </action-mappings>
? </struts-config>

------解决方法--------------------
<html:errors/> 应该放到form里
? 其实这种非空校验可以用javascript,节约服务器的资源
------解决方法--------------------
同意,用javascript验证这种东西较好,避免提交一次,浪费资源
------解决方法--------------------
new ActionError("不能为空!"));
?
? 应为 new ActionError("errors.abc"));
?
? 在 ApplicationResources.properties中