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

Struts2中Action取得表单数据的几种方法

时间:2015-01-09 00:05:32      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:

Struts2中Action取得表单数据的几种方法

 
Struts2中Action获得表单数据的几种方法
struts2 Action获取表单传值

1.通过属性驱动式
JSP:
<form action="sys/login.action" method="post">
  <input type="text" name="username">
  <input type="submit" value="submit">
</form>

Action:直接通过get、set方法获取。

public class sysAction extends ActionSupport{
    private String username;

    public String login() throws Exception {
        System.out.println(username);
        return SUCCESS;
    }

    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username= username;
    }
}

 

2.模型驱动方式,必须要实现ModelDriven<T>接口。对于要传入多个model第二种方式不方便
JSP:
<form action="sys/login.action" method="post">
  <input type="text" name="username">
  <input type="submit" value="submit">
</form>

Action:必须实现getModel() 方法

public class sysAction extends ActionSupport implements ModelDriven<User>{
    private User user;

    public String login() throws Exception {
        System.out.println(getModel().getUsername());
        return SUCCESS;
    }

    public User getModel() {
        if (null == user) {
            return user = new User();
        }    
        return user;
    }
}

 


3.第三种方式可以完全不实现ModelDriven<T>,也可使用多个model对象的属性。
JSP:
<form action="sys/login.action" method="post">
  <input type="text" name="user.username">
  <input type="text" name="teacher.level">
  <input type="submit" value="submit">
</form>

 

Action: 必须提供set方法

public class sysAction extends ActionSupport{
   //using spring ioc or new manually 
private User user=new User(); private Teacher teacher=new Teacher(); public String login() throws Exception { System.out.println(user.getUsername()); System.out.println(teacher.getLevel()); return SUCCESS; } public void setUser(User user) { this.user = user; } public void setTeacher(Teacher teacher) { this.teacher = teacher; } }

 

Struts2中Action取得表单数据的几种方法

标签:

原文地址:http://www.cnblogs.com/wancy86/p/getDataFromPage.html

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