struts 中后台action接收表单数据
2013-01-29 17:00
246 查看
在struts 开发中我们都知道页面表单的数据提交,首先要经过struts拦截器,然后在后台的action类中,使用set、get方法来得到表单传过来的数据值,那么这时候我们就必须在action类中定义表单的数据变量及其set、get方法,而有开发经验的人都知道,我们的开发是基于MVC模式的,页面表单的数据变量在model类中都已经定义过,且有set、get方法了,如果为了接收数据又在action重新定义了表单变量及其set、get方法,这样纯粹是增加了代码量,且action中的表单变量及其set、get方法看起来是多余的,那有没有解决办法那,action中不用重新定义表单变量及其set、get方法就能接收数据,下面就给出解决方法。
我们只要在action中定义一个model类对象和其对应的set、get方法就可以解决这个问题,如:首先定义个user model类,定义所有的变量及set、get方法,然后在action类中直接定义这个model 类user的对象及set、get方法,就可以接收页面传过来的数据了,而不用在action中把所有表单变量再重新定义一次。
package org.xmh.demo;
publicclass User {
private String name;
private String password;
....省略set、get方法
}
package org.xmh.demo;
import com.opensymphony.xwork2.ActionSupport;
public
class Login extends ActionSupport {
private User
user;
private Stringmessage;
public String getMessage() {
return
message;
}
public User getUser() {
return
user;
}
public
void setUser(User user) {
this.user = user;
}
}
我们只要在action中定义一个model类对象和其对应的set、get方法就可以解决这个问题,如:首先定义个user model类,定义所有的变量及set、get方法,然后在action类中直接定义这个model 类user的对象及set、get方法,就可以接收页面传过来的数据了,而不用在action中把所有表单变量再重新定义一次。
package org.xmh.demo;
publicclass User {
private String name;
private String password;
....省略set、get方法
}
package org.xmh.demo;
import com.opensymphony.xwork2.ActionSupport;
public
class Login extends ActionSupport {
private User
user;
private Stringmessage;
public String getMessage() {
return
message;
}
public User getUser() {
return
user;
}
public
void setUser(User user) {
this.user = user;
}
}
相关文章推荐
- struts2 lesson one: 的action特征,三种创建方法,action接收表单数据的三种访问,读取方式
- AJax提交表单数据到后台springmvc接收
- Java后台框架篇--Struts2.0之Action的参数接收
- 【Struts】接收表单传递给Action的参数
- angular成功提交表单数据后台却无法接收问题
- 表单提交及后台数据接收
- Struts2之前台表单传值到后台Action方法总结
- Action如何接收jsp页面form表单提交的数据?
- ASP.NET MVC Action以强类型的方式接收表单数据
- struts接收表单数据的3种方式
- Struts2之前台表单传值到后台Action方法总结
- S2.3_Struts2_ActionGetInputFormData Action动作类接收用户输入表单数据的3种实现方式
- truts2中表单可以不用struts自带的<s:form>标签,直接用html的<form>标签能够提交数据到action
- Ajax 提交表单或提交数据到后台时没有提交值(后台接收不到值)
- action提交form表单,用于登录请求,servlet接收数据
- highcharts如何接收java作为后台 struts2 传递过来的动态数据
- 关于struts三种action后台接收参数的方法
- 3.Struts采用EL保持表单数据:Action
- struts接收表单数据的方式之property
- struts2设置加载非默认路径的struts.xml文件解决方案+Struts2初学 Struts2的action接收用户数据方式