Struts2(二)---将页面表单中的数据提交给Action
问题:在struts2框架下,如何将表单数据传递给业务控制器Action。
struts2中,表单想Action传递参数的方式有两种,并且这两种传参方式都是struts2默认实现的,他们分别是基本属性注入、域模型注入
、其中:
---基本属性注入,是将表单的数据项分别传入给Action中的一些基本基本类型。
---域模型注入,是将表单的数据项打包传入给Action中的一个实体对象。
我们项目Struts2的实例,在其基础上使用这2中方式完成页面向Action的参数传递。具体的我们可以在项目首页regist.jsp上追加表单,
并在表单中模拟一些数据,将这些数据提交给RegistAction,最后在RegistAction中将接受的参数输出到控制台。
具体实现步骤:
1>基本属性注入
步骤一:
在项目的regist.jsp中,追加表单,并将该表单设置提交给RegistAction,即将form的action属性设置为:
<form action="regist" method="post">
在表单中增加一个文本框,用于输入一个公司姓名,该文本框的name属性值为company。代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%@ page contentType="text/html;charset=gbk"%>
<%@ taglib prefix="s" uri="/struts-tags" %> <%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>京东商城注册页面</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <%
request.setCharacterEncoding("gbk");
%>
</head> <body>
<center>
<form action="regist" method="post">
公 司: <input type="text" name="company"/> <br>
<table>
<tr>
<td><input type="submit" value="注册"/></td>
<td><input type="reset" value="重置" ></td>
</tr>
</table>
</form>
<s:fielderror />
</center>
</body>
</html>
步骤二:RegistAction中,接收表单传入的参数
在RegistAction中,追加属性并用于接收表单传入的姓名参数,该属性的名称要求与文本框的值相同(company),并且该属性需要
具备set方法。在业务方法中输出属性company的值。通知为了方便观察代码执行的顺序,在Action默认构造器中,输出任意的文字,
代码如下:
package com.wss.action; import com.opensymphony.xwork2.ActionSupport;
import com.wss.Dao.School;
import com.wss.Dao.User;
import com.wss.Dao.UserDao; public class RegistAction extends ActionSupport{ public RegistAction()
{
System.out.println("Initialization RegistAction....");
}
private String company;
public void setCompany(String company)
{
System.out.println("Setting the company");
this.company=company;
}
public String execute() throws Exception{
UserDao ud =new UserDao(); System.out.println("The company is "+this.company);
//if(ud.regist(user)!=0){ this.addFieldError("success", "注册成功");
return SUCCESS;
//}
//this.addFieldError("error", "注册失败");
//return ERROR; } }
步骤三:测试
重新部署该项目并启动tomcat,打开浏览器,针对当前的案例,在地址栏中输入地址:
http://localhost:8080/ShopDemo/regist.jsp
运行结果:
点击提交:
Eclipse控制台输出:
Initialization RegistAction....
Setting the company
The company is 公司
控制台输出的顺序可以证明代码的执行顺序:实例化Action--->调用set方法注入参数company的值-->调用业务方法execute(),
当然这个过程是Struts2的API自行实现的,我们只需要在写代码时满足上述步骤中的要求即可。
2>域模型注入一(Action中属性用private User user =new User();已创建)
步骤一:修改表单,追加演示数据
在regist.jsp修改表单,追加用户名、密码、电话和地址四个文本框,模拟输入用户的相关信息,代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%@ page contentType="text/html;charset=gbk"%>
<%@ taglib prefix="s" uri="/struts-tags" %> <%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>京东商城注册页面</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <%
request.setCharacterEncoding("gbk");
%>
</head> <body>
<center>
<form action="regist" method="post"> 用户名:<input type="text" name="user.name"/><br>
密 码:<input type="password" name="user.password"/><br>
手 机:<input type="text" name="user.phone" /><br>
地 址:<input type="text" name="user.address"/><br> 公 司: <input type="text" name="company"/> <br> <table>
<tr>
<td><input type="submit" value="注册"/></td>
<td><input type="reset" value="重置" ></td>
</tr>
</table>
</form>
<s:fielderror />
</center>
</body>
</html>
步骤二:创建实体类
创建包com.wss.Dao,用于存放实体类。在com.wss.Dao包下创建实体类User,用于封装表单中追加的数据,即用户名、密码、
电话和地址。User中要包含两个属性,用于封装用户名、密码电话和地址,并给属性提供get和set方法,代码如下:
package com.wss.Dao; public class User {
private int id;
private String name;
private String password;
private String phone;
private String address; public User()
{
System.out.println("Initialization the User......");
}
public int getId() {
System.out.println("Getting the ID");
return id;
} public void setId(int id) {
System.out.println("Setting the ID");
this.id = id;
} public String getName() {
System.out.println("Getting the name");
return name;
} public void setName(String name) {
System.out.println("Setting the name");
this.name = name;
} public String getPassword() {
System.out.println("Getting the password");
return password;
} public void setPassword(String password) {
System.out.println("Setting the password");
this.password = password;
} public String getPhone() {
System.out.println("Getting the phone");
return phone;
} public void setPhone(String phone) {
System.out.println("Setting the phone");
this.phone = phone;
} public String getAddress() {
System.out.println("Getting the address");
return address;
} public void setAddress(String address) {
System.out.println("Setting the address");
this.address = address;
} }
步骤三:修改RegistAction,接受表单传入的参数
在RegistAction中,追加属性用于接受表单传入的用户名、密码、电话和地址参数,该属性的类型为User类型,名称为user,并为
该属性提供get和set方法。
在业务方法(execute())中输出属性user的值,代码如下:
package com.wss.action; import com.opensymphony.xwork2.ActionSupport;
import com.wss.Dao.School;
import com.wss.Dao.User;
import com.wss.Dao.UserDao; public class RegistAction extends ActionSupport{ public RegistAction()
{
System.out.println("Initialization RegistAction....");
} private User user =new User();
//private User user;
public User getUser() {
System.out.println("Getting the getUser");
return user;
} public void setUser(User user) {
System.out.println("Setting the setUser");
this.user = user;
}private String company;
public void setCompany(String company)
{
System.out.println("Setting the company");
this.company=company;
} public String execute() throws Exception{
UserDao ud =new UserDao(); System.out.println("The company is "+this.company+" The name is "+this.user.getName()+" The phone is "+this.user.getAddress());
//if(ud.regist(user)!=0){ this.addFieldError("success", "注册成功");
return SUCCESS;
//}
//this.addFieldError("error", "注册失败");
//return ERROR; } }
步骤四:修改表单,设置文本框属性
在regist.jsp中,修改表单新增的4个文本框name属性值。对于域模型注入的方式,文本框name属性值应该是具有"对象名.属性名"
格式的表达式。
其中对象名指的是Action中的实体类型属性名,即User对象实例,属性名指的是实体类型中的属性名
(name,password,phone,address),代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%@ page contentType="text/html;charset=gbk"%>
<%@ taglib prefix="s" uri="/struts-tags" %> <%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>京东商城注册页面</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <%
request.setCharacterEncoding("gbk");
%>
</head> <body>
<center>
<form action="regist" method="post"> 用户名:<input type="text" name="user.name"/><br>
密 码:<input type="password" name="user.password"/><br>
手 机:<input type="text" name="user.phone" /><br>
地 址:<input type="text" name="user.address"/><br> 公 司: <input type="text" name="company"/> <br> <table>
<tr>
<td><input type="submit" value="注册"/></td>
<td><input type="reset" value="重置" ></td>
</tr>
</table>
</form>
<s:fielderror />
</center>
</body>
</html>
步骤五:测试
重新部署项目并启动tomcat,在浏览器中输入地址:http://localhost:8080/ShopDemo/regist.jsp
效果如下图所示(当然为了稍候测试方便,我自己输入了一些信息):
点击提交,查看myeclipse的控制台,输出结果如下:
Initialization the User......
Initialization RegistAction....
Setting the company
Getting the getUser
Setting the name
Getting the getUser
Setting the password
Getting the getUser
Setting the phone
Getting the name
Getting the address
The company is 公司 The name is good The phone is 111
控制台输出的顺序可以证明代码的执行顺序为:实例化Action-->实例化User并注入参数-->调用set方法注入User对象-->调用业务
方法。
但这个时候是先实例化User对象,再实例化Action对象,主要是因为Action中有private User user =new User();创建实例对象前,一般会对静态属性、静态对码段,对象属性按顺序进行初始化后,才调用Action的构造函数;user实例化后(我自己感觉实例化后并
把user对象注入了,即相当于调用了setUser方法);
再接着
Getting the getUser
Setting the name
Getting the getUser
Setting the password
Getting the getUser
Setting the phone
Getting the name
Getting the address
用于调用set方法注入user对象各属性。
3>域模型注入二(Action中属性 private School school;没有用new创建对象)
步骤一:修改表单,追加演示数据
在regist.jsp修改表单,追加用学校、城市、院系三个文本框,模拟输入用户的相关信息,代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%@ page contentType="text/html;charset=gbk"%>
<%@ taglib prefix="s" uri="/struts-tags" %> <%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>京东商城注册页面</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <%
request.setCharacterEncoding("gbk");
%>
</head> <body>
<center>
<form action="regist" method="post"> 用户名:<input type="text" name="user.name"/><br>
密 码:<input type="password" name="user.password"/><br>
手 机:<input type="text" name="user.phone" /><br>
地 址:<input type="text" name="user.address"/><br> 公 司: <input type="text" name="company"/> <br> 学 校:<input type="text" name="school.name"/>
城 市:<input type="text" name="school.city" />
院 系:<input type="text" name="school.department" /> <table>
<tr>
<td><input type="submit" value="注册"/></td>
<td><input type="reset" value="重置" ></td>
</tr>
</table>
</form>
<s:fielderror />
</center>
</body>
</html>
步骤二:创建实体类
创建包com.wss.Dao,用于存放实体类。在com.wss.Dao包下创建实体类School,用于封装表单中追加的数据,即学校、城市和
院系。
School中要包含三个属性,用于封装学校、城市和院系,并给属性提供get和set方法,代码如下:
package com.wss.Dao; public class School { private String name;
private String city;
private String department; public School()
{
System.out.println("Initilization School....");
}
public String getName() {
System.out.println("Getting the school name");
return name;
}
public void setName(String name) {
System.out.println("Setting the school name");
this.name = name;
}
public String getCity() {
System.out.println("Getting the school city");
return city;
}
public void setCity(String city) {
System.out.println("Setting the school city");
this.city = city;
}
public String getDepartment() {
System.out.println("Getting the school department");
return department;
}
public void setDepartment(String department) {
System.out.println("Setting the school department");
this.department = department;
} }
步骤三:修改RegistAction,接受表单传入的参数
在RegistAction中,追加属性用于接受表单传入的学校、城市和院系,该属性的类型为School类型,名称为school,
并为该属性提供get和set方法。
在业务方法(execute())中输出属性school的值,代码如下:
package com.wss.action; import com.opensymphony.xwork2.ActionSupport;
import com.wss.Dao.School;
import com.wss.Dao.User;
import com.wss.Dao.UserDao; public class RegistAction extends ActionSupport{ public RegistAction()
{
System.out.println("Initialization RegistAction....");
} private User user =new User();
//private User user;
public User getUser() {
System.out.println("Getting the getUser");
return user;
} public void setUser(User user) {
System.out.println("Setting the setUser");
this.user = user;
} private School school; public School getSchool() {
System.out.println("Getting the getSchool");
return school;
} public void setSchool(School school) {
System.out.println("Setting the setSchool");
this.school = school;
} private String company;
public void setCompany(String company)
{
System.out.println("Setting the company");
this.company=company;
} public String execute() throws Exception{
UserDao ud =new UserDao(); System.out.println("The company is "+this.company+" The name is "+this.user.getName()+" The phone is "+this.user.getAddress());
System.out.println("The school name is "+this.school.getName()+" The city is "+this.school.getCity()+" The department is "+ this.school.getDepartment()); //if(ud.regist(user)!=0){ this.addFieldError("success", "注册成功");
return SUCCESS;
//}
//this.addFieldError("error", "注册失败");
//return ERROR; } }
步骤四:修改表单,设置文本框属性
在regist.jsp中,修改表单新增的3个文本框name属性值。对于域模型注入的方式,文本框name属性值应该是具有"对象名.属性名"
格式的表达式。
其中对象名指的是Action中的实体类型属性名school,属性名指的是实体类型中的各属性名(name,city,department),
代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%@ page contentType="text/html;charset=gbk"%>
<%@ taglib prefix="s" uri="/struts-tags" %> <%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>京东商城注册页面</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <%
request.setCharacterEncoding("gbk");
%>
</head> <body>
<center>
<form action="regist" method="post"> 用户名:<input type="text" name="user.name"/><br>
密 码:<input type="password" name="user.password"/><br>
手 机:<input type="text" name="user.phone" /><br>
地 址:<input type="text" name="user.address"/><br> 公 司: <input type="text" name="company"/> <br> 学 校:<input type="text" name="school.name"/>
城 市:<input type="text" name="school.city" />
院 系:<input type="text" name="school.department" /> <table>
<tr>
<td><input type="submit" value="注册"/></td>
<td><input type="reset" value="重置" ></td>
</tr>
</table>
</form>
<s:fielderror />
</center>
</body>
</html>
步骤五:测试
重新部署项目并启动tomcat,在浏览器中输入地址:http://localhost:8080/ShopDemo/regist.jsp
效果如下图所示(当然为了稍候测试方便,我自己输入了一些信息):
点击提交,查看myeclipse的控制台,输出结果如下:
Initialization the User......
Initialization RegistAction....
Setting the company
Getting the getSchool
Initilization School....
Setting the setSchool
Setting the school city
Getting the getSchool
Setting the school department
Getting the getSchool
Setting the school name
Getting the getUser
Setting the address
Getting the getUser
Setting the name
Getting the getUser
Setting the password
Getting the getUser
Setting the phone
Getting the name
Getting the address
The company is 公司 The name is good The phone is wrwer
Getting the school name
Getting the school city
Getting the school department
The school name is xuexiao The city is beiijng The department is shuxue
控制台输出的顺序可以证明代码的执行顺序为:实例化Action-->实例化User并注入参数-->调用set方法注入User对象-->调用业务
方法。
在这里,private User user =new User();和private School school;不一样,school只是一个引用,并没有用new创建出对象,
所以在对school的各属性name、city和department用set方法注入时,用getSchool方法得到school对象时(Getting the getSchool),还没有school对象的存在,此时调用School的构造函数进行初始化创建school对象;然后通过school对象对其各
属性用setName....方法对school的各属性进行注入。
Struts2(二)---将页面表单中的数据提交给Action的更多相关文章
- 静态页面表单中js验证
笔记: 1. onblur事件:onblur 事件会在对象失去焦点时发生.http://www.w3school.com.cn/jsref/event_onblur.asp onkeyup事件:onk ...
- Struts2(二)之封装请求正文、数据类型转换、数据验证
一.封装请求正文到对象中(重点) 1.1.静态参数封装 在struts.xml文件中,给动作类注入值,使用的是setter方法 1.2.动态参数封装 通过用户表单封装请求正文参数 1.2.1.动作类作 ...
- 通过button将form表单的数据提交到action层
form表单中不需要写action的路径,需要给form表单一个唯一的id,将你要提交的信息的表单中的标签name="action中的javabean对象.javabean属性". ...
- struts2(二)---ModelDriven模型驱动
这篇文章是在上一篇文章(http://blog.csdn.net/u012116457/article/details/48194905)的基础上写的,大家可以先快速阅读一下上一篇. 这篇文章用来写一 ...
- 把JSON数据载入到页面表单的两种思路(对easyui自带方法进行改进)
#把JSON数据载入到页面表单的两种思路(对easyui自带方法进行改进) ##背景 项目中经常需要把JSON数据填充到页面表单,一开始我使用easyui自带的form load方法,觉得效率很低,经 ...
- JavaScript学习——使用JS完成注册页面表单校验
1.步骤分析 第一步:确定事件(onsubmit)并为其绑定一个函数 第二步:书写这个函数(获取用户输入的数据<获取数据时需要在指定位置定义一个 id>) 第三步:对用户输入的数据进行判断 ...
- wicket基础应用(1)--使用wicket对表单中的数据进行验证
作者:lhx1026 出处:http://lhx1026.iteye.com/ wicket基础应用(1)--使用wicket对表单中的数据进行验证 举个例子: 1.有一个Java文件SysCharg ...
- 遭遇“HTTP 错误 500.19 无法访问请求的页面,因为该页的相关配置数据无效。”
windows 2008下IIS7 安装ASP.NET 遇到如下错误: HTTP 错误 500.19 - Internal Server Error 无法访问请求的页面,因为该页的相关配置数据无效. ...
- SpringMVC接收页面表单参数-java-电脑编程网
body { font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI ...
随机推荐
- Eclipse 中的重构功能
Eclipse 中的重构功能使其成为了一个现代的 Java 集成开发环境 (IDE),而不再是一个普通的文本编辑器.使用重构,您可以轻松更改您的代码,而不必担心对别处造成破坏.有了重构,您可以只关注于 ...
- u3d 模型ID配置
换模型为什么要改代码 0.0 你没有逻辑ID->模型的配置么. 做2个配置.. 分别是角色的ID对应模型路径. 然后是里面的动画名对应真实动画名. 比如 ID=1 path = "xx ...
- java 16 -7 泛型方法和泛型接口(泛型类相似)
写一个ObjectTool类 泛型方法:把泛型定义在方法上 格式 public <泛型类型> 返回类型 方法名(泛型类型) 这样的好处是: 这个泛型方法可以接收任意类型的数据 public ...
- 关于OAuth2.0的文章收集
http://blog.csdn.net/seccloud/article/details/8192707
- Genymotion出现unknown generic error和This may occur if you are using a proxy错误的解决方案
今天在实验室希望在Genymotion上多下载几个模拟器,需要重新登录帐号,却发现一个错误,叫做unknown generic error.前几天还出现过一个很诡异的问题.截图如下: . (1)unk ...
- POJ 1125 Stockbroker Grapevine
Stockbroker Grapevine Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 33141 Accepted: ...
- 【转】【WPF】WriteableBitmap应用及图片数据格式转换
使用 WriteableBitmap 类基于每个框架来更新和呈现位图.这对于生成算法内容(如分形图像)和数据可视化(如音乐可视化工具)很有用. WriteableBitmap 类使用两个缓冲区.“后台 ...
- DWZ集成的xhEditor编辑器浏览本地图片上传的设置
有关xhEditor的文件上传配置官方文档链接:http://i.hdu.edu.cn/dcp/dcp/comm/xheditor/demos/demo08.html 一.xhEditor图片上传的配 ...
- C语言 结构体中属性的偏移量计算
//计算结构体偏移量 #include<stdio.h> #include<stdlib.h> #include<string.h> //详解:对于offscfof ...
- GO To Definition的背后操作【VS2015 C#】
使用VS开发U3D项目时,去察看某个变量的声明,比如某组件的gameObject变量,会看到如下代码 一看似乎有点晕,这代码什么意思啊,就一个 public GameObject gameObject ...