小弟初学 在struts中使用 <html:errors/>不会显示错误请问是错在哪里?

J2EE 码拜 9年前 (2015-05-11) 589次浏览 0个评论
 

小弟初学 在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>

2分
<html:errors/> 应该放到form里
其实这种非空校验可以用javascript,节约服务器的资源
不是说<html:errors/>可以放在任何位子,既可以放表单里也可以放在表单外的啊?
怎么java这么冷啊。
帮顶!!!
各位大哥帮帮我啊!
1分
同意,用javascript验证这种东西较好,避免提交一次,浪费资源
20分
new ActionError(“不能为空!”));

应为 new ActionError(“errors.abc”));

在 ApplicationResources.properties中
  error.abc=不能为空!

建议用javascript
1分
支持chocolate.建议好好看看流程,再决定放在哪里。
6分
<action-mappings>
    <action input=”/jsp1.jsp” name=”tActionForm” path=”/tAction” scope=”request” type=”errortest.tAction” validate=”false”>
      <forward name=”succe” path=”/jsp2.jsp”/>
    </action>
</action-mappings>
如果使用 actionForm 中的 validate 方法校验表单, 应该在 action-mapping 中设置 validate=”false” 。如果 validate=”true” , struts 会使用 validator 校验框架进行表单验证。
20分
new ActionError(“不能为空!”));

应为 new ActionError(“errors.abc”));

在 ApplicationResources.properties中
  error.abc=不能为空!


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明小弟初学 在struts中使用 <html:errors/>不会显示错误请问是错在哪里?
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!