Struts 2 ModelDriven Action

To create a ModelDriven Action our Action class should implement the ModelDriven interface and should include the modelDriven interceptor. The modelDriven interceptor is already included in the default stack.

The next step is to implement the getModel() method in such a way that it returns the application domain object, in our example we return the User object.

When using the ModelDriven method we need to initialize the User object ourselves.

The framework will automatically transfers the form data into the User object.

 1 public class UserAction extends ActionSupport implements ModelDriven {
 2 
 3     private User user = new User();
 4 
 5     public UserAction() {
 6     }
 7 
 8     public Object getModel() {
 9         return user;
10     }
11 
12     public String execute() {
13         return SUCCESS;
14     }
15 
16     public User getUser() {
17         return user;
18     }
19 
20     public void setUser(User user) {
21         this.user = user;
22     }
23 }

You can directly access the user attributes like name, age etc in Action use the following syntax.

1 user.getXXX();

The User class contains the following attributes and the corresponding getter and setter methods.

public class User {

    private String name;
    private int age;
    private String sex;
    private String[] hobby;
    private String country;


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String[] getHobby() {
        return hobby;
    }

    public void setHobby(String[] hobby) {
        this.hobby = hobby;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }
}

  In the jsp page the user attributes can be accessed directly. To refer the user's age, the value of the name attribute should be

name = "age"

  The index.jsp page contains the following code.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@taglib uri="/struts-tags" prefix="s" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>User Details</title>
</head>
<body>
<s:form action="UserAction" >
    <s:textfield name="name" label="User Name" />
    <s:textfield name="age" label="Age" />
    <s:radio name="sex" label="Sex" list="{'M','F'}" />
    <s:checkboxlist name="hobby" label="Hobby"
    list="{'Music','Art','Dance'}" />
    <s:select name="country" label="Country"
    list="{'Select','India','USA','France','Spain'}"  />
    <s:submit />
</s:form>
</body>
</html>

  The result.jsp page contains the follwing code.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@taglib uri="/struts-tags" prefix="s" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>User Details</title>
</head>
<body>
    <h2>User Details</h2>
    <hr>
    User Name :<s:property value="name" />

    Age :<s:property value="age" />

    Hobbies :<s:property value="hobby" />

    Country :<s:property value="country" />

</body>
</html>

  

posted @ 2013-03-17 11:47  daveztong  阅读(388)  评论(0编辑  收藏  举报