首页 > 代码库 > Struts2_接收参数

Struts2_接收参数

  1.1
        直接创建接收的数据类型

 1         public class HelloAction extends ActionSupport { 2      3             private String name; 4             private int age; 5              6             public String add(){ 7                 System.out.println("name="+name); 8                 System.out.println("age="+age); 9                 return SUCCESS;10             }11 12             public String getName() {13                 return name;14             }15 16             public void setName(String name) {        //服务器自动调用17                 this.name = name;18             }19 20             public int getAge() {21                 return age;22             }23 24             public void setAge(int age) {25                 this.age = age;26             }27         }

 


        在URL中: http://localhost:8080/Struts2_0100_Introduction/HelloAction_add?name=a&age=123
    1.2
        创建一个User类, 放置接收的参数

 1             public class User { 2                 private String name; 3                 private int age; 4                 public String getName() { 5                     return name; 6                 } 7                 public void setName(String name) { 8                     this.name = name; 9                 }10                 public int getAge() {11                     return age;12                 }13                 public void setAge(int age) {14                     this.age = age;15                 }16             }

 


            不需要自己new user类

 1             public class UserAction extends ActionSupport{ 2                 private User user; 3                  4                 public String add(){ 5                     System.out.println("User is run."); 6                     System.out.println("name="+ user.getName()); 7                     System.out.println("age="+user.getAge()); 8                     return SUCCESS; 9                 }10                 public User getUser() {11                     return user;12                 }13                 public void setUser(User user) {14                     System.out.println("setUser run");15                     this.user = user;16                 }17             }

 


            在URL中: http://localhost:8080/Struts2_0100_Introduction/UserAction_add?user.name=a&user.age=123
    1.3
        基本不用
        创建一个User类, 放置接收的参数
            public class User {
            添加了implements ModelDriven<User>
            需要自己new User
          

 1  public class ModelUserAction extends ActionSupport implements ModelDriven<User>{ 2                 private User user = new User(); 3                  4                 public String add(){ 5                     System.out.println("ModelUser is run."); 6                     System.out.println("name="+ user.getName()); 7                     System.out.println("age="+user.getAge()); 8                     return SUCCESS; 9                 }10 11                 @Override12                 public User getModel() {13                     // TODO 自动生成的方法存根14                     return user;15                 }16             }

 


        URL: http://localhost:8080/Struts2_0100_Introduction/ModelUser_add?name=a&age=123

Struts2_接收参数