问题:在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">
公&nbsp;&nbsp;司: <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>
密&nbsp;&nbsp;码:<input type="password" name="user.password"/><br>
手&nbsp;&nbsp;机:<input type="text" name="user.phone" /><br>
地&nbsp;&nbsp;址:<input type="text" name="user.address"/><br> 公&nbsp;&nbsp;司: <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>
密&nbsp;&nbsp;码:<input type="password" name="user.password"/><br>
手&nbsp;&nbsp;机:<input type="text" name="user.phone" /><br>
地&nbsp;&nbsp;址:<input type="text" name="user.address"/><br> 公&nbsp;&nbsp;司: <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>
密&nbsp;&nbsp;码:<input type="password" name="user.password"/><br>
手&nbsp;&nbsp;机:<input type="text" name="user.phone" /><br>
地&nbsp;&nbsp;址:<input type="text" name="user.address"/><br> 公&nbsp;&nbsp;司: <input type="text" name="company"/> <br> 学&nbsp;&nbsp;校:<input type="text" name="school.name"/>
城&nbsp;&nbsp;市:<input type="text" name="school.city" />
院&nbsp;&nbsp;系:<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>
密&nbsp;&nbsp;码:<input type="password" name="user.password"/><br>
手&nbsp;&nbsp;机:<input type="text" name="user.phone" /><br>
地&nbsp;&nbsp;址:<input type="text" name="user.address"/><br> 公&nbsp;&nbsp;司: <input type="text" name="company"/> <br> 学&nbsp;&nbsp;校:<input type="text" name="school.name"/>
城&nbsp;&nbsp;市:<input type="text" name="school.city" />
院&nbsp;&nbsp;系:<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的更多相关文章

  1. 静态页面表单中js验证

    笔记: 1. onblur事件:onblur 事件会在对象失去焦点时发生.http://www.w3school.com.cn/jsref/event_onblur.asp onkeyup事件:onk ...

  2. Struts2(二)之封装请求正文、数据类型转换、数据验证

    一.封装请求正文到对象中(重点) 1.1.静态参数封装 在struts.xml文件中,给动作类注入值,使用的是setter方法 1.2.动态参数封装 通过用户表单封装请求正文参数 1.2.1.动作类作 ...

  3. 通过button将form表单的数据提交到action层

    form表单中不需要写action的路径,需要给form表单一个唯一的id,将你要提交的信息的表单中的标签name="action中的javabean对象.javabean属性". ...

  4. struts2(二)---ModelDriven模型驱动

    这篇文章是在上一篇文章(http://blog.csdn.net/u012116457/article/details/48194905)的基础上写的,大家可以先快速阅读一下上一篇. 这篇文章用来写一 ...

  5. 把JSON数据载入到页面表单的两种思路(对easyui自带方法进行改进)

    #把JSON数据载入到页面表单的两种思路(对easyui自带方法进行改进) ##背景 项目中经常需要把JSON数据填充到页面表单,一开始我使用easyui自带的form load方法,觉得效率很低,经 ...

  6. JavaScript学习——使用JS完成注册页面表单校验

    1.步骤分析 第一步:确定事件(onsubmit)并为其绑定一个函数 第二步:书写这个函数(获取用户输入的数据<获取数据时需要在指定位置定义一个 id>) 第三步:对用户输入的数据进行判断 ...

  7. wicket基础应用(1)--使用wicket对表单中的数据进行验证

    作者:lhx1026 出处:http://lhx1026.iteye.com/ wicket基础应用(1)--使用wicket对表单中的数据进行验证 举个例子: 1.有一个Java文件SysCharg ...

  8. 遭遇“HTTP 错误 500.19 无法访问请求的页面,因为该页的相关配置数据无效。”

    windows 2008下IIS7 安装ASP.NET 遇到如下错误: HTTP 错误 500.19 - Internal Server Error 无法访问请求的页面,因为该页的相关配置数据无效. ...

  9. SpringMVC接收页面表单参数-java-电脑编程网

    body { font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI ...

随机推荐

  1. python如何控制数据库?

    http://www.w3cschool.cc/python/python-mysql.html 通过利用MySQLdb可以操作数据库 实例: 以下实例链接Mysql的TESTDB数据库: # enc ...

  2. Android 动态加载 (二) 态加载机制 案例二

    探秘腾讯Android手机游戏平台之不安装游戏APK直接启动法 重要说明 在实践的过程中大家都会发现资源引用的问题,这里重点声明两点: 1. 资源文件是不能直接inflate的,如果简单的话直接在程序 ...

  3. javascript单元测试工具

    单元测试关注的是验证一个模块或一段代码的执行效果是否和设计或预期一样.有些开发人员认为,编写测试用例浪费时间而宁愿去编写新的模块.然而,在处理大型应用程序时,单元测试实际上会节省时间:它能帮助您跟踪问 ...

  4. 一道看似简单的sql需求却难倒各路高手 - 你也来挑战下吗?

    转自:http://www.cnblogs.com/keguangqiang/p/4535046.html 听说这题难住大批高手,你也来试下吧.ps:博问里的博友提出的. 原始数据 select *  ...

  5. KnockOutJS步步深入

    由于项目原因,目前需要用到KnockOutJS,找到了一个锻炼Knockout的绝好的网址:http://learn.knockoutjs.com/ 一步一步的按照教程来,可以把KO掌握的八九不离十.

  6. Android Drawable的9种子类 介绍

    原文: Android Drawable的9种子类 介绍   Drawable 在android里面 就是代表着图像,注意是图像 而不是图片. 图片是图像的子集.图像除了可以包含图片以外 还可以包含颜 ...

  7. Chrome 消息传递机制

    Chrome插件开发入门(二)——消息传递机制 Blog | Qiushi Chen 2014-03-31 9538 阅读 Chrome 插件 由于插件的js运行环境有区别,所以消息传递机制是一个重要 ...

  8. 《图解tcp/ip》读书笔记(二)

    <图解tcp/ip>读书笔记(二) 本周主要阅读的是本书的第三章--数据链路. 当然了,从某些角度讲,我认为这一章就是计算机网络的最基本的内容之一.整章讲述了数据链路层的作用和相关技术,主 ...

  9. 20145222黄亚奇《Java程序设计》第1周学习总结

    教材学习内容总结 BJVM是Java程序唯一认识的操作系统,其可执行文件为.class文档 Java的三大平台为Java SE,Java EE,Java ME. Java SE的四个部分为:JVM,J ...

  10. MVC5 + EF6 + Bootstrap3 (14) 分部视图PartialView

    Slark.NET-博客园 http://www.cnblogs.com/slark/p/mvc5-ef6-bs3-get-started-partialview.html 系列教程:MVC5 + E ...