Question

In the struts.xml:

    <action name="User_UserFormSubmit"  class="actions.UserManager">
        <result name="input" >/jsp/user_form.jsp</result>
        <result name="success" type="redirectAction"> success_register</result>
    </action>

My class:

public class UserManager extends ActionSupport implements ModelDriven<User>{

   private User user = new User();

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

   public String validate() {
    addActionError("blabla");
   }
   public String execute() {
    return SUCCESS;
} ...

then in the jsp:

    <s:property value="getActionErrors()"/>

I expect in the input result :

   <li> blabla </li>

I succefully arrived to user_form.jsp, but the actionError does not appear

I tried without the "implements ModelDriven" and it work

The model driven erase the actionErrors (I supposed)

I want to use validate and modeldriven ¿any idea?

Was it helpful?

Solution

Not a big fan of model driven... but here is an example.

Before the example please note that using validate() does not make much sense in terms of ModelDriven. The reason is that the Model should be used over several actions and so the validation should probably be consistent. You don't use model driven just to make property names a bit shorter (to do that you use the struts2 push tag). As such validation should be done with xml as the model is bigger than any one action. Each action which uses that model uses the Visitor validator. This validator merely looks up the xml validation file for the model. The following example however will use the validate() method in the action to save time.

The following example will use the struts2-conventions-plugin to reduce the example size (adding it to your project is simply a matter of adding one jar).

create: com.quaternion.action.AddUser

package com.quaternion.action;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class AddUser  extends ActionSupport implements ModelDriven<User>{
    User user = new User();

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

    @Override
    public void validate(){
       if (user.age != 12) {
        super.addActionError("bla bla bla");
       }
    }  
}

create: com.quaternion.action.User

package com.quaternion.action;

public class User {
    public String name;
    public int age;
}

create: /WEB-INF/content/add-user-input.jsp

<%@taglib prefix="s" uri="/struts-tags"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <title>Form</title>
    </head>
    <body>
        <h1>Form</h1>
        <s:actionerror/>
        <s:form action="add-user">
            <s:textfield name="name"/>
            <s:textfield name="age"/>
            <s:submit/>
        </s:form>
    </body>
</html>

create: /WEB-INF/content/add-user-success.jsp

<%@taglib prefix="s" uri="/struts-tags"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <title>Success</title>
    </head>
    <body>
        <h1>Success</h1>
    </body>
</html>

To test:

Add /add-user-input as the action name on your context path. If you enter an age of 12 you will get the success page, if you enter anything else you will get an action error. This has been tested to work. It is possible a typo was made, but the main thing to take away is there is an error in your application, using both conventions or xml there should be no issues with what you are doing.

OTHER TIPS

You can also validate with @validations too, you have access to model driven object in the validator.

@Action(value = "save-user")
@Validations(
    stringLengthFields = { 
        @StringLengthFieldValidator(fieldName = "name", trim = true, key = "validate.required.string.length"),
        @StringLengthFieldValidator(fieldName = "age", trim = true, key = "validate.required.string.length"),
        @StringLengthFieldValidator(fieldName = "address.addLine1", trim = true, key = "validate.required.string.length")
        })

public String save()  {
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top