标签:
struts2 Action获取表单数据的方式有三种:1.通过属性驱动的方式。2.模型驱动方式。3.使用多个model对象的属性。
<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; } }
<form action="sys/login.action" method="post"> <input type="text" name="username"> <input type="submit" value="submit"> </form>
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; } }
<form action="sys/login.action" method="post"> <input type="text" name="username"> <input type="submit" value="submit"> </form>
Action: 必须提供set方法
public class sysAction extends ActionSupport{ private User user; private Teacher 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; } }
标签:
原文地址:http://www.cnblogs.com/longshiyVip/p/4558351.html