我有以下定义

struts-config.xml:

<struts-config>
<form-beans>
    <form-bean name="LoginForm" type="com.actionform.LoginForm"/>
      </form-beans>

        <action-mappings>

    <!-- action for login  -->
    <action input="/views/login.jsp" name="LoginForm" path="/Login" scope="session" type="com.actions.LoginAction"
    parameter="method" validate="true">
        <forward name="success" path="/views/Frameset.html" />

    </action>
       </action-mappings>

<message-resources parameter="/WEB-INF/ApplicationResources"/>
    <!-- ========================= Validator plugin ================================= -->
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
    <set-property
        property="pathnames"
        value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>
</plug-in>

</struts-config>


登录表单:

public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
    ActionErrors errors = new ActionErrors();
    if (userName == null || userName.length() < 1) {
        System.out.println("in validate ---");
        errors.add("userName", new ActionMessage("error.userName.required"));
        // TODO: add 'error.name.required' key to your resources
    }
    if (password == null || password.length() < 1) {
        errors.add("password", new ActionMessage("error.password.required"));
        // TODO: add 'error.name.required' key to your resources
    }
    return errors;
}


login.jsp:

<html:form action="/Login?method=loginUser">

<html:errors/>

<html:text name="LoginForm" property="userName" />

<html:messages id="err_userName" property="userName">
            <bean:write name="err_userName" />
</html:messages>
</html:form>


属性文件:

error.userName.required = User Name is required.
error.password.required = Password is required.


我在哪里做错了?我收到以下错误

javax.servlet.jsp.JspException: Cannot find bean error in any scope


我只想在同一JSP中显示错误。

最佳答案

获得包含要在输入页面中显示的消息或错误的ActionMessages / ActionErrors对象(使用<html:messages>标记或<html:errors>标记)后,必须从对象,将您的验证结果放在范围内:

addMessages(HttpServletRequest request, ActionMessages messages)


要么

addErrors(HttpServletRequest request, ActionMessages errors)


你在那样吗

10-06 13:53
查看更多