Struts2 validation is not working properly, validation error messages not shown

☆樱花仙子☆ 提交于 2019-12-24 01:25:28

问题


First of all I use simple theme but even without it the same behaviour occurs (except page formatting). When I submit the form, name field gets empty and redirects to register.jsp without displaying the validation error. After checking the logs or with debugger, it seems that validation is working properly and server log messages are written as expected. I'll post generic code.

struts.properties

struts.ui.theme=simple

MyAction

private User user = new User()    // getter and setter

@Inject
transient UserDAO userDAO;

@Override
public User getModel() {
    return user;
}

public void validate(){

    LOG.debug("NAME VALIDATION " + user.getName());
    if("".equals(user.getName())){
        addFieldError("user.name", "Name can't be empty");
        LOG.debug("Validation Error on name");
    }       
}

Checked with debugger, validate method is working and logs are written.

struts.xml

<package name="users" extends="struts-default">
    <action name="registerUser" method="prepareRegister" class="com.test.MyAction">
        <result name="success">/register.jsp</result>
    </action>

    <action name="saveOrUpdateUser" method="saveOrUpdate" class="com.test.MyAction">
        <result name="input" type="redirect">registerUser</result>
        <result name="success" type="redirect">listUser</result>
    </action>
</package>

register.jsp

<td>
    <s:textfield id = "userName" 
              label = "User Name" 
               name = "user.name" />
</td>
<s:fielderror fieldName = "user.name" />

Feel free to ask me for clarifications. I am pretty new to struts 2, I tried the struts 2 documentation validation way, and checked other tutorials too. I don't know if I am missing something or I have some missconfiguration that I am not noticing, since the logic is working and the view part (jsp) is not. Thanks in advance.


回答1:


After having read how the INPUT result works and having abandoned the ModelDriven design pattern that adds nothing to your programming experience except problems, that might easily hide themselves in the middle of the Interceptor Stack, note that:

  • redirect is the result to use when redirecting to external URLs or non-Action URLs, while redirectAction should be used when redirecting to an Action;
  • when redirecting, a new request is created, and hence the old parameters (including action errors and messages and field errors) are lost.

To prevent that, if you want to keep using the PRG pattern (and hence the redirection), you can use the MessageStore Interceptor that will store and retrieve the messages for you across the redirections; just define an interceptor stack that contains it, and it will solve your problem.

Or do it once like in the example from the documentation.




回答2:


This is what did work in the end and after Andrea's useful comments:

<action name="saveOrUpdatePlayer" method="saveOrUpdate" class="com.test.MyAction">

     <interceptor-ref name="store">
        <param name="operationMode">STORE</param>
     </interceptor-ref>
     <interceptor-ref name="defaultStack" />

     <result name="input">/register.jsp</result>
     <result name="success" type="redirectAction">listUser</result>
</action>

With this, the validation messages are shown correctly. When I used redirectAction type for input, messages were disappearing. It looks like defaultStack made the job done.



来源:https://stackoverflow.com/questions/36598872/struts2-validation-is-not-working-properly-validation-error-messages-not-shown

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!