码迷,mamicode.com
首页 > 其他好文 > 详细

Struts2_三种传参数方法

时间:2014-09-01 17:28:33      阅读:225      评论:0      收藏:0      [点我收藏+]

标签:blog   http   io   使用   java   strong   ar   数据   art   

1、通过属性传参数(Attr)
UserAction.java

bubuko.com,布布扣package com.bebig.struts2.user.action;
bubuko.com,布布扣
bubuko.com,布布扣import com.opensymphony.xwork2.ActionSupport;
bubuko.com,布布扣
bubuko.com,布布扣public class UserAction extends ActionSupport {
bubuko.com,布布扣    private String name;
bubuko.com,布布扣    private int age;
bubuko.com,布布扣
bubuko.com,布布扣    public String add() {
bubuko.com,布布扣        System.out.println("name:"+name);
bubuko.com,布布扣        System.out.println("age:"+age);
bubuko.com,布布扣        return SUCCESS;
bubuko.com,布布扣    }
bubuko.com,布布扣
bubuko.com,布布扣    public int getAge() {
bubuko.com,布布扣        return age;
bubuko.com,布布扣    }
bubuko.com,布布扣
bubuko.com,布布扣    public String getName() {
bubuko.com,布布扣        return name;
bubuko.com,布布扣    }
bubuko.com,布布扣
bubuko.com,布布扣    public void setAge(int age) {
bubuko.com,布布扣        this.age = age;
bubuko.com,布布扣    }
bubuko.com,布布扣
bubuko.com,布布扣    public void setName(String name) {
bubuko.com,布布扣        this.name = name;
bubuko.com,布布扣    }
bubuko.com,布布扣}
bubuko.com,布布扣

调用方法:

bubuko.com,布布扣<a href="user/userAdd.action?name=Don&age=26">添加用户</a>

2、通过域模型传参数(Domain Model)
UserAction.java

bubuko.com,布布扣package com.bebig.struts2.user.action;
bubuko.com,布布扣
bubuko.com,布布扣import com.bebig.struts2.user.dto.UserDTO;
bubuko.com,布布扣import com.bebig.struts2.user.model.User;
bubuko.com,布布扣import com.opensymphony.xwork2.ActionSupport;
bubuko.com,布布扣
bubuko.com,布布扣public class UserAction extends ActionSupport {
bubuko.com,布布扣    // 通过域模型传参数
bubuko.com,布布扣    private User user;
bubuko.com,布布扣
bubuko.com,布布扣    // 通过数据传输对象传参数
bubuko.com,布布扣    // private UserDTO userDTO;
bubuko.com,布布扣
bubuko.com,布布扣    public User getUser() {
bubuko.com,布布扣        return user;
bubuko.com,布布扣    }
bubuko.com,布布扣
bubuko.com,布布扣    public void setUser(User user) {
bubuko.com,布布扣        this.user = user;
bubuko.com,布布扣    }
bubuko.com,布布扣
bubuko.com,布布扣    public String add() {
bubuko.com,布布扣        System.out.println("name:" + user.getName());
bubuko.com,布布扣        System.out.println("age:" + user.getAge());
bubuko.com,布布扣        // System.out.println("name:"+userDTO.getName());
bubuko.com,布布扣        // System.out.println("age:"+userDTO.getAge());
bubuko.com,布布扣        return SUCCESS;
bubuko.com,布布扣    }
bubuko.com,布布扣
bubuko.com,布布扣    // public void setUserDTO(UserDTO userDTO) {
bubuko.com,布布扣    // this.userDTO = userDTO;
bubuko.com,布布扣    // }
bubuko.com,布布扣    //
bubuko.com,布布扣    // public UserDTO getUserDTO() {
bubuko.com,布布扣    // return userDTO;
bubuko.com,布布扣    // }
bubuko.com,布布扣}
bubuko.com,布布扣

User.java

bubuko.com,布布扣package com.bebig.struts2.user.model;
bubuko.com,布布扣
bubuko.com,布布扣public class User {
bubuko.com,布布扣    private String name;
bubuko.com,布布扣    private int age;
bubuko.com,布布扣
bubuko.com,布布扣    public int getAge() {
bubuko.com,布布扣        return age;
bubuko.com,布布扣    }
bubuko.com,布布扣
bubuko.com,布布扣    public String getName() {
bubuko.com,布布扣        return name;
bubuko.com,布布扣    }
bubuko.com,布布扣
bubuko.com,布布扣    public void setAge(int age) {
bubuko.com,布布扣        this.age = age;
bubuko.com,布布扣    }
bubuko.com,布布扣
bubuko.com,布布扣    public void setName(String name) {
bubuko.com,布布扣        this.name = name;
bubuko.com,布布扣    }
bubuko.com,布布扣}
bubuko.com,布布扣

UserDTO.java

bubuko.com,布布扣package com.bebig.struts2.user.dto;
bubuko.com,布布扣//数据传输对象
bubuko.com,布布扣public class UserDTO {
bubuko.com,布布扣    private String name;
bubuko.com,布布扣    private String password;
bubuko.com,布布扣    private String comfirmPassword;
bubuko.com,布布扣
bubuko.com,布布扣    public String getPassword() {
bubuko.com,布布扣        return password;
bubuko.com,布布扣    }
bubuko.com,布布扣
bubuko.com,布布扣    public void setPassword(String password) {
bubuko.com,布布扣        this.password = password;
bubuko.com,布布扣    }
bubuko.com,布布扣
bubuko.com,布布扣    public String getComfirmPassword() {
bubuko.com,布布扣        return comfirmPassword;
bubuko.com,布布扣    }
bubuko.com,布布扣
bubuko.com,布布扣    public void setComfirmPassword(String comfirmPassword) {
bubuko.com,布布扣        this.comfirmPassword = comfirmPassword;
bubuko.com,布布扣    }
bubuko.com,布布扣
bubuko.com,布布扣    private int age;
bubuko.com,布布扣
bubuko.com,布布扣    public int getAge() {
bubuko.com,布布扣        return age;
bubuko.com,布布扣    }
bubuko.com,布布扣
bubuko.com,布布扣    public String getName() {
bubuko.com,布布扣        return name;
bubuko.com,布布扣    }
bubuko.com,布布扣
bubuko.com,布布扣    public void setAge(int age) {
bubuko.com,布布扣        this.age = age;
bubuko.com,布布扣    }
bubuko.com,布布扣
bubuko.com,布布扣    public void setName(String name) {
bubuko.com,布布扣        this.name = name;
bubuko.com,布布扣    }
bubuko.com,布布扣}
bubuko.com,布布扣

调用方法:

bubuko.com,布布扣<a href="user/userAdd.action?user.name=Don&user.age=26">添加用户</a>

3、通过模型驱动传参数(Model Driven)
UserAction.java

bubuko.com,布布扣package com.bebig.struts2.user.action;
bubuko.com,布布扣
bubuko.com,布布扣import com.bebig.struts2.user.model.User;
bubuko.com,布布扣import com.opensymphony.xwork2.ActionSupport;
bubuko.com,布布扣import com.opensymphony.xwork2.ModelDriven;
bubuko.com,布布扣
bubuko.com,布布扣public class UserAction extends ActionSupport implements ModelDriven<User> {
bubuko.com,布布扣    private User user = new User();
bubuko.com,布布扣
bubuko.com,布布扣
bubuko.com,布布扣    public String add() {
bubuko.com,布布扣        System.out.println("name:"+user.getName());
bubuko.com,布布扣        System.out.println("age:"+user.getAge());
bubuko.com,布布扣        return SUCCESS;
bubuko.com,布布扣    }
bubuko.com,布布扣    @Override
bubuko.com,布布扣    public User getModel()
bubuko.com,布布扣    {
bubuko.com,布布扣        return user;
bubuko.com,布布扣    }
bubuko.com,布布扣
bubuko.com,布布扣}
bubuko.com,布布扣

User.java
(源代码同上)
调用方法:

bubuko.com,布布扣<p>使用ModelDriven传参数</p>
bubuko.com,布布扣<a href="user/userAdd.action?name=Don&age=26">添加用户</a>

 

Struts2_三种传参数方法

标签:blog   http   io   使用   java   strong   ar   数据   art   

原文地址:http://www.cnblogs.com/jin256/p/3949425.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!