2

我是 Struts2 的新手,试图addFieldError在我的表单中使用<s:select>标签。我有一个表单,我必须从下拉列表中选择一个值。第一次显示此表单时,响应来自一个 Action 类,我在其中创建了一个 List。<s:select>在我的表单中,我使用如下标签在下拉列表中打印该列表:

<s:form action="clientselect">
  <h2>Select Client to add the case :</h2>  
  <table>
    <s:select list="list" headerKey="-1" headerValue="Select Client"
              label="Select Client" tooltip="Select the desired client" name="client">
    </s:select>

    <tr><td><s:submit value="Register Case" theme="simple" align="right"/></td>
    <td><s:reset value="Reset" theme="simple" align="right"/></td></tr>
  </table>
</s:form>

“pageinclude=ancar”打印此表单。

struts.xml

<action name="clientselect" class="casediary.JudicialCaseRegisterValidation" method="execute">
  <result name="addcase">user.jsp?pageinclude=ncr</result>
  <result name="error">user.jsp?pageinclude=errancar</result>
  <result name="input">user.jsp?pageinclude=ancar</result>
  <result name="loggedout">index.jsp?pageinclude=relogin</result>
</action>

在 JudicialCaseRegisterValidation.java 中

public void validate()
{       
    if(client==null || client.equals("-1"))     
        addFieldError("client", "This field can not be blank.");
}

一切正常。错误条件得到满足,结果我得到“输入”。错误消息也在打印,但下拉列表中的值已消失。列表打印为空。因为这个时间响应不是来自设置 List 的 Action 类。

然后我更改了我的 struts.xml 以将请求发送到 Action 类,<result>如下所示:

<result name="input"  type="redirect">link.action</result>

“link.action”是在 Action 类中发送请求以创建列表并打印该表单的内容。

但是这次表格只是再次打印并且没有打印错误消息。

我希望再次打印该列表以及除此之外的错误消息。请告诉如何。?

4

2 回答 2

2

Pretty much everyone starting with Struts2 encounter this problem, soon or later.

When requesting an Action in Struts2, your request will pass through an Interceptor Stack (a list of Interceptors); every Interceptor does is particular business, then redirect the request (in case of errors) or proceed the execution to the next Interceptor, or to the Action if it is the last.

The validation is performed by the Validation Interceptor. If the validation fails, it will hijack the request and will redirect it to the input result defined in struts.xml. No matter if the validation is by XML, by Annotation or in a validate() method inside the Action: the Action is not reached ! Then, everything inside the execute() method, or the method you are calling (if you are using a custom one) is not executed;

If the loading of your List elements is demanded to the execute() method, it won't be performed in case of an input result.

The main ways to avoid this problem are:

  1. Implement Preparable Interface, and put all the data loading stuff into the prepare() method.
    That method is run by the Prepare Interceptor, that is placed BEFORE the Validation Interceptor;
    it will always run, and it will run before the validation, no matter which method of your action (execute() or something else) you are calling, nor if you are encountering validation errors;

  2. Use <s:action/> tag from the JSP, to call dummy Actions returning JSP snippets;

  3. Use a redirectAction as the input result type, to the execute() (or something else) method, as you are doing.
    While redirecting, it will lose all the request parameters (including action errors, field errors, action messages etc) , then you will need to manually copy them by declaring them in the struts.xml as parameters of the redirectAction result.

The first way is the preferred, imho.

EDIT:

just found it in the official documentation too: how do we repopulate controls when validation fails ?

于 2013-06-06T12:32:50.833 回答
0

我认为 type = "redirectAction" 重定向到特定操作

像这样的东西

<result name="input" type="redirectAction">link</result>

希望它有所帮助

如果我是正确的,或者您在哪里像这样返回该操作的结果,您可以在您的 execute() 方法中设置它

list=setList(newList);

return result;
于 2013-06-06T07:51:45.603 回答