首页 > 代码库 > struts2接收参数的几种形式
struts2接收参数的几种形式
1、使用属性
HTML:
<form action="login" method="post" name="form1"> 用户名:<s:textfield name="username"/><br/> 密 码:<s:password name="password"/><br/> <s:submit value="提交"/> </form>
action:
public class TestAction extends ActionSupport{ private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
2、使用类来接收
HTML:
<form action="login" method="post" name="form1"> 用户名:<s:textfield name="users.username"/><br/> 密 码:<s:password name="users.password"/><br/> <s:submit value="提交"/> </form>
Action:
public class TestAction extends ActionSupport{private Users users;public Users getUsers(){return users;}public void setUsers(Users users){this.users=users;}
Users类:
public class Users{ private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; }}
3、使用模型驱动
HTML:
<form action="login" method="post" name="form1"> 用户名:<s:textfield name="username"/><br/> 密 码:<s:password name="password"/><br/> <s:submit value="提交"/> </form>
Action:
public class sysAction extends ActionSupport implements ModelDriven<User>{ private User user = new User(); //必须手动实例化 public User getModel() { return user; //返回实例 } }
4、使用传统的request
public class TestAction extends ActionSupport{ private static final long serialVersionUID = -7463970150000893325L; public void execeute() { String name = super.getRequest().getParameter("paraName"); System.out.println("姓名:" + name); } }
struts2接收参数的几种形式
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。