Struts2 - 验证&瓷砖参数

时间:2011-03-22 19:24:14

标签: validation struts2 tiles

所以,我的场景如下。

  1. 页面显示逐条调用newInfo.action with articleId参数
  2. 表单操作将调用postComment.action
  3. postComment.action将调用validate()
  4. validate()返回验证错误。
  5. * 问题出在这里,我怎样才能返回到tile并获得验证错误?
  6. 我的struts.xml

    <action name="newInfo" class="org.blog.controller.NewInfo">
        <result type="tiles" name="success">NewInfo</result>
    </action>
    <action name="postComment" class="org.blog.controller.CommentController">
        <result type="redirectAction" name="success">newInfo?id=${articleId}</result
        <result type="redirect" name="input">newInfo.action?id=${articleId}</result>
    </action>
    

    CommentController.java

    public void validate() {
        if(getName().length() == 0)
            addFieldError("name", "Name is required");
        if(getEmail().length() == 0)
            addFieldError("email", "Email is required");
        if(getCurrentURL().length() == 0)
            addFieldError("website", "Website is required");
    
        if(hasFieldErrors())
            System.out.println("Field error.");
    
    }
    

    当前,页面结果是文章页面,带有“字段错误”,但页面没有显示任何字段错误。 那么,有没有解决方案来解决它?

2 个答案:

答案 0 :(得分:0)

将type =“redirect”更改为type =“chain”,请参阅:http://struts.apache.org/2.0.14/docs/result-types.html了解详情。

答案 1 :(得分:0)

试试这个:

<action name="postComment" class="org.blog.controller.CommentController">
         <interceptor-ref name="store">
            <param name="operationMode">STORE</param>
         </interceptor-ref>
         <interceptor-ref name="defaultStack" />        
         <result type="redirectAction" name="success">newInfo?id=${articleId}</result
         <result type="tiles" name="input">NewInfo</result>
</action>