struts2中的Action接收表单传递过来的参数有3种方法
2017-09-12 10:52
507 查看
struts2中的Action接收表单传递过来的参数有3种方法:
1> 在Action类中定义表单属性
如,登陆表单login.jsp:
<form action="login" method="post" name="form">
User:<s:textfield name="username"/><br/>
Password:<s:password name="password"/><br/>
<s:submit value="提交"/>
</form>
在Action中定义两个属性
private String username;
private String password;
提供setter,getter方法。即可接收到表单传过来的参数. 这种接收参数的方法,方便简单,但是结构性不是很好,且当表单传递来的参数很多的时候,整个Action类中充斥着setter,getter方法,程序结构不是很美观。
2.把表单传递过来的参数封装成一个类,然后调用其中的属性.
如,登陆表单login.jsp:
<form action="login" method="post" name="form">
User:<s:textfield name="user.username"/><br/>
Password:<s:password name=" user.password"/><br/>
<s:submit value="提交"/>
</form>
定义Model类
public class User {
private String username;
private String password;
提供setter,getter方法。
}
在Action类中定义Model的对象
private User user;
提供setter,getter方法。
// 传说中这中方式用的比较多。
3.通过实现ModelDriven接口
接收表单数据首先Action类必须实现ModelDriven接口,同样把表单传来的数据封装起来
如,登陆表单login.jsp:
<form action="login" method="post" name="form">
User:<s:textfield name="username"/><br/>
<
4000
p align="justify">Password:<s:password name=" password"/><br/>
<s:submit value="提交"/>
</form>
定义Model类
public class User {
private String username;
private String password;
提供setter,getter方法。
}
Action类
public class loginAction extends ActionSupport implements ModelDriven<User> {
User user = new User(); // 这种方式必须自己New对象
……………
public Users getModel(){
return user;
}
}
1> 在Action类中定义表单属性
如,登陆表单login.jsp:
<form action="login" method="post" name="form">
User:<s:textfield name="username"/><br/>
Password:<s:password name="password"/><br/>
<s:submit value="提交"/>
</form>
在Action中定义两个属性
private String username;
private String password;
提供setter,getter方法。即可接收到表单传过来的参数. 这种接收参数的方法,方便简单,但是结构性不是很好,且当表单传递来的参数很多的时候,整个Action类中充斥着setter,getter方法,程序结构不是很美观。
2.把表单传递过来的参数封装成一个类,然后调用其中的属性.
如,登陆表单login.jsp:
<form action="login" method="post" name="form">
User:<s:textfield name="user.username"/><br/>
Password:<s:password name=" user.password"/><br/>
<s:submit value="提交"/>
</form>
定义Model类
public class User {
private String username;
private String password;
提供setter,getter方法。
}
在Action类中定义Model的对象
private User user;
提供setter,getter方法。
// 传说中这中方式用的比较多。
3.通过实现ModelDriven接口
接收表单数据首先Action类必须实现ModelDriven接口,同样把表单传来的数据封装起来
如,登陆表单login.jsp:
<form action="login" method="post" name="form">
User:<s:textfield name="username"/><br/>
<
4000
p align="justify">Password:<s:password name=" password"/><br/>
<s:submit value="提交"/>
</form>
定义Model类
public class User {
private String username;
private String password;
提供setter,getter方法。
}
Action类
public class loginAction extends ActionSupport implements ModelDriven<User> {
User user = new User(); // 这种方式必须自己New对象
……………
public Users getModel(){
return user;
}
}
相关文章推荐
- struts2中的Action接收表单传递过来参数的3种方法
- struts2中的Action接收表单传递过来的参数有3种方法
- struts2中的Action接收表单传递过来的参数有3种方法
- Action接收表单传递过来的参数有3种方法
- struts2中的Action接收表单传递过来的参数
- struts2中的Action接收表单传递过来参数的方式
- struts2中action接收参数的3种方法
- struts2中action接收参数的3种方法
- struts2接收表单传过来的参数方法
- struts2中action接收参数的3种方法
- struts2中的Action接收表单传递的参数
- Struts2中Action接收参数的方法
- Struts2中Action接收参数的方法
- Struts2中action接收参数的三种方法及ModelDriven和Preparable接口结合JAVA反射机制的灵活用法
- struts2中action接收参数的方法
- struts2中action接收参数的方法
- Struts2中Action接收传递的参数
- Struts2中Action接收参数的方法:属性,Model,ModelDriver
- Struts2中Action接收参数的方法
- Struts2中action接收参数的三种方法及ModelDriven跟Preparable接口结合JAVA反射机制的灵活用法