2

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.

manu
  • 241
  • 1
  • 2
  • 16

2 Answers2

2

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.

Community
  • 1
  • 1
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
  • Thanks for the usefull stuff Andrea. I abandoned ModelDriven and I tried this in struts.xml: added in action saveOrUpdateUser, store interceptor with STORE operation and defaultStuck. Then I changed input type to redirectAction. Did I miss something? After submitting form with empty name, redirects to the same page with empty fields. No validation errors are shown. – manu Apr 13 '16 at 14:39
  • 1
    `defaultStuck` is confuzzlingly amusing :D – Andrea Ligios Apr 13 '16 at 14:56
  • 1
    By the way: in struts.xml define a stack of your (myStack) created by copying the exploded `defaultStack` (taken from here https://struts.apache.org/docs/struts-defaultxml.html ) and adding the messagestore interceptor to it. Then use `default-interceptor-ref` to specify that myStack is the default interceptor for every action that is not defining a stack itself. Then set it to AUTOMATIC: it will work for every redirection, probably without giving you drawbacks. In case it still doesn't work, please edit the question with the updated code and we'll see – Andrea Ligios Apr 13 '16 at 15:00
  • Or do it once like in the example from the documentation: https://struts.apache.org/docs/message-store-interceptor.html – Andrea Ligios Apr 13 '16 at 15:05
  • Should I edit your comment and add the solution? Finally It worked – manu Apr 13 '16 at 15:38
  • 1
    Yes @manu, feel free to edit my answer adding the comment that helped you getting it working. Please don't forget to upvote answers that have been useful, thanks - EDIT I see now you've answered yourself, even better ;) – Andrea Ligios Apr 13 '16 at 15:49
1

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.

Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
manu
  • 241
  • 1
  • 2
  • 16
  • 1
    Check to setup your interceptors in the top of struts.xml so that you don't have to put them on every action separately. – Panos Apr 13 '16 at 15:41