struts2jsp页面表单提交乱码失败之后,返回jsp页面。这是一般流程。

1498人阅读
Struts(5)
作为一个ssm新人,对struts的理解也仅仅是书本上一些简单的示例,
& 对如下这个form进行提交之后,执行完action,并return 一个字符串,在struts配置文件中配置了对应的jsp页面,但并没有按想象中跳转到相应页面。
&form id=&form& action=&saveuser.action& method=&post&
class=&form-horizontal form-input&&
进行修改之后
&form id=&form& action=&saveuser.action& method=&post& next=&addsuccess.action& class=&form-horizontal form-input&&
添加了一个&next=&addsuccess.action& & & & & & & & & & & & & & & & & & & & & & &
再经过一次action处理,返回一个字符串,会跳转到相对应的jsp页面。
ps:大神如果看到,求解释原理机制
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:330424次
积分:6394
积分:6394
排名:第3788名
原创:300篇
转载:12篇
评论:78条
访问:/IAMTJW
文章:37篇
阅读:14450
文章:23篇
阅读:21626
文章:23篇
阅读:42958
文章:58篇
阅读:56802
文章:29篇
阅读:9238
文章:14篇
阅读:10519
文章:55篇
阅读:87592
(23)(9)(9)(15)(25)(8)(15)(33)(17)(39)(35)(6)(2)(6)(1)(1)(12)(6)(10)(9)(1)(30)2010年2月 Java大版内专家分月排行榜第二
2011年7月 Java大版内专家分月排行榜第三2010年1月 Java大版内专家分月排行榜第三2009年12月 Java大版内专家分月排行榜第三
本帖子已过去太久远了,不再提供回复功能。struts2.0表单form的提交 - solo.du - ITeye博客
博客分类:
在Struts2.0里面有一个非常牛*的功能就是支持更高级的POJO访问,这句话是什么意思呢?下面来通过例子实际操作一把就能体会到这个功能的强大与好使了。 要实现的功能:如果用户输入用户名xiaozu,密码111111,则显示welcome,xiaozhu!否则提示用户名或密码错误。 下面是我们所需的文件: 登陆页面login.jsp:
&%@ page language="java" contentType="text/ charset=utf-8"%&
&%@ taglib prefix="s" uri="/struts-tags" %&
&!DOCTYPE html PUBLIC "-//W 3C //DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&
&title&登陆页面&/title&
&s:form action="/test/loginAction.action" method="post"&
&s:textfield name="userName" label="用户名"/&
&s:password name="password" label="密码"/&
&s:submit/&
&%@ page language="java" contentType="text/ charset=utf-8"%&
&%@ taglib prefix="s" uri="/struts-tags" %&
&!DOCTYPE html PUBLIC "-//W 3C //DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&
&title&登陆页面&/title&
&s:form action="/test/loginAction.action" method="post"&
&s:textfield name="userName" label="用户名"/&
&s:password name="password" label="密码"/&
&s:submit/&
请求处理LoginAction.java:
import com.opensymphony.xwork2.ActionS
public class LoginAction extends ActionSupport {
private String msg="显示默认消息";
private String userN
public String getPassword() {
public void setPassword(String password) {
this.password =
public String getUserName() {
return userN
public void setUserName(String userName) {
this.userName = userN
public String getMsg() {
public String execute(){
if ("xiaozhu".equals(userName)&&"111111".equals(password)) {
msg="welcome,"+userN
msg="用户名或密码错误";
return this.SUCCESS;
import com.opensymphony.xwork2.ActionS
public class LoginAction extends ActionSupport {
private String msg="显示默认消息";
private String userN
public String getPassword() {
public void setPassword(String password) {
this.password =
public String getUserName() {
return userN
public void setUserName(String userName) {
this.userName = userN
public String getMsg() {
public String execute(){
if ("xiaozhu".equals(userName)&&"111111".equals(password)) {
msg="welcome,"+userN
msg="用户名或密码错误";
return this.SUCCESS;
响应页面HelloWorld.jsp:
&%@ page language="java" contentType="text/ charset=utf-8"%&
&%@ taglib prefix="s" uri="/struts-tags" %&
&!DOCTYPE html PUBLIC "-//W 3C //DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&
&title&Hello&/title&
&h3&&s:property value="msg"/&&/h3&
&%@ page language="java" contentType="text/ charset=utf-8"%&
&%@ taglib prefix="s" uri="/struts-tags" %&
&!DOCTYPE html PUBLIC "-//W 3C //DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&
&title&Hello&/title&
&h3&&s:property value="msg"/&&/h3&
如上所示,对于login.jsp表单中的每个值域我们都会在相应的action中声明一个对应的属性并产生相应的get和set方法,如果这个表单中的值很多(例如注册用户信息的表单),就会导致action十分庞大和容易混乱,并且也不利于我们的软件分层,违背了OO的原则,下面看一下改进后的方案。 登陆页面login.jsp:
&%@ page language="java" contentType="text/ charset=utf-8"%&
&%@ taglib prefix="s" uri="/struts-tags" %&
&!DOCTYPE html PUBLIC "-//W 3C //DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&
&title&Insert title here&/title&
&s:form action="/test/loginAction.action" method="post"&
&s:textfield name="user.userName" label="用户名"/&
&s:password name="user.password" label="密码"/&
&s:submit/&
&%@ page language="java" contentType="text/ charset=utf-8"%&
&%@ taglib prefix="s" uri="/struts-tags" %&
&!DOCTYPE html PUBLIC "-//W 3C //DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&
&title&Insert title here&/title&
&s:form action="/test/loginAction.action" method="post"&
&s:textfield name="user.userName" label="用户名"/&
&s:password name="user.password" label="密码"/&
&s:submit/&
新增实体类User.java:
public class User {
private String userN
public String getPassword() {
public void setPassword(String password) {
this.password =
public String getUserName() {
return userN
public void setUserName(String userName) {
this.userName = userN
public class User {
private String userN
public String getPassword() {
public void setPassword(String password) {
this.password =
public String getUserName() {
return userN
public void setUserName(String userName) {
this.userName = userN
请求处理LoginAction.java:
import com.opensymphony.xwork2.ActionS
public class LoginAction extends ActionSupport {
private String msg="显示默认消息";
public User getUser() {
public void setUser(User user) {
this.user =
public String getMsg() {
public String execute(){
if("xiaozhu".equals(user.getUserName())&&"111111".equals(user.getPassword()))
msg="welcome,"+user.getUserName();
msg="用户名或密码错误";
return this.SUCCESS;
import com.opensymphony.xwork2.ActionS
public class LoginAction extends ActionSupport {
private String msg="显示默认消息";
public User getUser() {
public void setUser(User user) {
this.user =
public String getMsg() {
public String execute(){
if("xiaozhu".equals(user.getUserName())&&"111111".equals(user.getPassword()))
msg="welcome,"+user.getUserName();
msg="用户名或密码错误";
return this.SUCCESS;//
响应页面HelloWorld.jsp:
&%@ page language="java" contentType="text/ charset=utf-8"%&
&%@ taglib prefix="s" uri="/struts-tags" %&
&!DOCTYPE html PUBLIC "-//W 3C //DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&
&title&Hello&/title&
&h3&&s:property value="msg"/&&/h3&
&%@ page language="java" contentType="text/ charset=utf-8"%&
&%@ taglib prefix="s" uri="/struts-tags" %&
&!DOCTYPE html PUBLIC "-//W 3C //DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&
&title&Hello&/title&
&h3&&s:property value="msg"/&&/h3&
前后两种处理方式有何不同呢?后者新建了一个User实体类,用于封装从表单中接收的数据,而在action中就不用设置相应的属性和方法了,并且这样做能够让我们的软件层次感更加明显,系统结构也更加清晰。以User为例,描述一下采用这种方式的关键步骤: 1.
在action中定义一个User类型的对象:User user; 2.
表单中各个控件的name属性要采取如下命名规则:对象名.属性名,注意对象名必须与action中定义的一样,属性名也必须和该对象的属性一一对应。如上例中的user.userName和user.password。 注意:
public User getUser() {
public User getUser() {
Action中的这个get方法必须,否则会出现丢值的现象;set方法就更不用说了,必须的。 上述原理解释: 1. 前面讲的自定义类型转换器是基于 OGNL 的 DefaultTypeConverter 类并实现 convertValue() 方法,两个转换方向的逻辑都写在这一个方法中。而 Struts 2 为我们提供了一个 DefaultTypeConverter 的抽象子类 StrutsTypeConverter 来继承,并实现其中两个抽象方法 convertFromString() 和 convertToString(),这要简单易懂。对比 Struts 1 的转换器是要实现 mons.beanutils.Converter 接口,以及它的 convert() 方法的。 2. 注意,上面的 convertFromString() 的第二个参数是一个字符串数组,所以可为请求的数组(如请求串为 ?u=1&u=2&u=3)定义转换器,Action 中相应的属性就应为数组或 List,这一方法的返回值也该为相应的类型(数组或List,要通过第三个参数 toClass 来判断是返回数组还是 List 了)。 3. 字符串(如 "user,pass") 转换成 Action 中的复合属性(如 User user) 前面是自定了类型转换器。除此之外,还可以 Struts 2 内置的 OGNL 表达式,更简单的转换,不用写转换器。例如,你的 Action 有属性 User user,只要在 jsp 页面的输入框命名为 user.name
和 user.pass 即可:
&input type="text" name="user.name"/& 或用标签:&s:textfield name="user.name" label="用户名"/&
&input type="text" name="user.pass"/&
或用标签:&s:textfield name="user.pass" label="密 码"/&
提交后,Struts 2 即会帮你构造 User 对象(user = new User()),并赋上属性值(user.setName(),user.setPass()),最后 user 对象赋给 Action (xxxAction.setUser(user))。所以要明白有三个必备的东西:
1) User 要用一个默认构造方法 2) User 要有对应 name 和 pass 的设置方法 setName() 和 setPass() 3) Action 要有 user 属性的设置方法 setUser(),getUser() 也是要的,至于功用后面能看到。 其实在 Struts 1 中也有这种用法,不过那是在 BeanUtils 中实现的。 4. 如果 Action 中的属性是 Map&String, User& 那么与此对应的表单写法就是:(用标签来写)
&s:textfield name="users['one'].name" label="第一个用户名"/&
&s:textfield name="users['one'].name" label="第一个密码"/&
&s:textfield name="users['two'].name" label="第二个用户名"/&
&s:textfield name="users['two'].name" label="第二个密码"/&
应该不难想像,这个表单提交后,users
中存储的是什么吧!
如果是对于 Action 中的
List 属性,List&User& 那么与此对应的表单写法就是:
&s:textfield name="users[0].name" label="第一个用户名"/&
&s:textfield name="users[0].name" label="第一个密码"/&
&s:textfield name="users[1].name" label="第二个用户名"/&
&s:textfield name="users[1].name" label="第二个密码"/& 5. 归纳前面3、4、5 几点,Struts2 的 Action 在设置每一个属性时都会 get 一下相应的元素 getUser() 或 getUsers()。
对于 3,在设置 user.name 和 user.pass 之前都会 getUser() 来获取 user 属性,如果 user 为 null 就构造 User 对象,然后设置相应的值。假如声明的时候就已构造好 User 对象,如有其他属性如 age=18,并不会被覆盖。
对于 4 和 5,也是在设置每一个属性前都会调用 getUsers() 判断声明的 Map 或 List 是否为 null,是则构造对应的 HashMap 或
ArrayList() 对象;接着根据 Key 或下标去获取相应位置的元素,如果不存在或为 null 则构造之,然后设置相应属性值。由此可见,若某元素的某个属性未重设值则保留原值,若原来Map或List 已有多个元素,也只会改变到 Key 或索引所对应元素的某个属性。对于 List 有可能出现跳空的情况,如页面只有索引不从 0 开始
&s:textfield name="users[1].name" label="第二个用户名"/&
&s:textfield name="users[1].name" label="第二个密码"/& 提交后就会发现,List 属性 users 的第一个元素为 null 了。同时如果尝试一下,你就会发现这里的 List 不能替代为数组 User[] users。
这种样法,可在 Struts 1 中实现,但要略施些小节,见我的另一篇日志:提交多行数据到Struts的ActionForm的List属性中 ,行为表现完全一致,只是换到 Struts 2 中一切都不用自己操心。 6. 看第四点,Action 之所以知道该构造什么类型的元素完全是由泛型告诉它的。如果不用泛型(比如用的是 JDK1.4),Action 中仅仅声明的是 M 或 L Action 该如何处理呢?它也不知道,只能够帮你构造出无类型的 HashMap 和 ArrayList(),填充不了元素。这就必须在局部类型转换的配置文件中来指定集合元素的类型。例如 Action 为 LoginAction,就要在 LoginAction-conversion.properties 中声明了,格式如下:
#Element_xxx=复合类型,基中 Element 是固定的,xxx 为属性名
#下面表示为 List 属性 users 的元素为 com.unmi.vo.User 类型
Element_users=com.unmi.vo.User
对于 Map,须分别指定 Key 的类型和 Value 的类型
#Key_xxx=复合类型,基中 Key 是固定的,xxx 为 map 属性名,下面写成 String 都不行的
Key_users=java.lang.String
指定 Map 的 Value 的类型与指定 List 元素类型是一样的
Element_users=com.unmi.vo.User 难怪 Struts 2 要与 1.5 以上 JDK
使用,泛型比配置来得方便。如果硬要用 1.4 JDK,就只有配置类型了,会多很多 conversion 文件的。在 提交多行数据到Struts的ActionForm的List属性中 中类型的确定由 AutoArrayList() 的构造参数完成。 7. Set 是无序集合,所以无法像 List 那样用数字下标来访问,幸好 Struts 2 可为其指定索引属性。例如,LoginAction 声明为 S (这里好像用泛型只能省得了 Element_users 说明,KeyProperty_users 少不了)。则需在 LoginAction-conversion.properties 中写下:
#指定 Set 的元素类型
Element_users=com.unmi.vo.User
#KeyProperty_集合属性名=集合元素的索引属性名,这里为 User 的 name 属性
KeyProperty_users=name 此时提交页面这么写,最好提交前能根据输入的用户名自动修动输入框的 name。
用户名: &input name="users('scott').name"/&
密 码: &input name="users('scott').pass"/& 显示的时候页面可用标签
用户名: &s:property value="users('scott').name"/&
密 码: &s:property value="users('scott').pass"/& 注意前面,访问 Set 元素是用的圆括号,而不同于 Map、List、数组是用中括号。我想一般也犯不着非要用 Set 而不用 List,Struts 2 中用 Set 比在 Struts 1 中似乎还麻烦。 8. Struts 2 内建了一批转换器:boolean、char、int、long、float、double 和它们的包装类型;Date,日期格式使用请求所在 Locale 的 SHORT 格式;数组,默认元素为字符串,其他类型则要转换每一个元素?(好像是一次性转换完成的);集合,默认元素为字符串 XWorkList(String.class, Object[]),其他如 List&Integer& ids,类型为 XWorkList(Integer.class, Object[]),XWorkList 继承自 ArrayList。 9. 类型转换出错由 Struts 来帮你处理,在默认拦截器栈中提供了 conversionError 拦截器,不用你写一点代码逻辑。conversionError 在出错时将错误封装成 fieldError,并放在 ActionContext 中。你所要做的就是遵循它的规则,1) 你的 Action 要继承自 ActionSupport,2)在 struts.xml 中声明名为 "input" 的 result,出错时会在 input 逻辑视图显示信息。3)尽量用标签来写输入域(如&s:textfield name="number" label="数量"/&),这样转换出错后,就会像校验失败一样把错误信息显示在每个输入框上面(视模板而定),否则要手工用 &s:fielderror/& 输出在某处。 默认时输出错误信息为(比如是属性 number,输入的是字符串时):Invalid field value for field "number".你可以改变默认显示,在全局国际化资源文件中加上 xwork.default.invalid.fieldvalue={0}字段类型转换失败!。在某些时候,可能还需要对特定字段指定特别的提示信息,那么在名为 ActionName.properties 的局部资源文件中加上 invalid.fieldvalue.属性名=提示信息 (如 invalid.fieldvalue.number=数量格式错误) 10. 最后是集合属性转换错误时的显示,对于页面中的同名输入框,有多个出错误,如果手工用 &s:fieldError/& 只会显示一条错误,但要是输入页是用标签(如&s:textfield name="number" label="数量"/&),仍会在每一个出错的输入框上都提示。至此类型转换的内容也就完结了。 11. struts2中 &input type="user.name" value="jack"& &input type="user.name" value="lucy"& action中取得name的值是"jack,lucy". 注意:此处name是字符串类型,不是数组类型
浏览 19539
duyunfengsolo
浏览: 777844 次
来自: 广州
视频下载地址:http://download.csdn.net ...
aaaaaaaaaaaaaaa问题:在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/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"&
&base href="&%=basePath%&"&
&title&京东商城注册页面&/title&
&meta http-equiv="Content-Type" content="text/ charset=utf-8" /&
request.setCharacterEncoding("gbk");
&form action="regist" method="post"&
公&&司: &input type="text" name="company"/& &br&
&td&&input type="submit" value="注册"/&&/td&
&td&&input type="reset" value="重置" &&/td&
&s:fielderror /&
步骤二:RegistAction中,接收表单传入的参数
在RegistAction中,追加属性并用于接收表单传入的姓名参数,该属性的名称要求与文本框的值相同(company),并且该属性需要
具备set方法。在业务方法中输出属性company的值。通知为了方便观察代码执行的顺序,在Action默认构造器中,输出任意的文字,
代码如下:
package com.wss.
import com.opensymphony.xwork2.ActionS
import com.wss.Dao.S
import com.wss.Dao.U
import com.wss.Dao.UserD
public class RegistAction extends ActionSupport{
public RegistAction()
System.out.println("Initialization RegistAction....");
public void setCompany(String company)
System.out.println("Setting the company");
this.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/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"&
&base href="&%=basePath%&"&
&title&京东商城注册页面&/title&
&meta http-equiv="Content-Type" content="text/ charset=utf-8" /&
request.setCharacterEncoding("gbk");
&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&
&td&&input type="submit" value="注册"/&&/td&
&td&&input type="reset" value="重置" &&/td&
&s:fielderror /&
步骤二:创建实体类
创建包com.wss.Dao,用于存放实体类。在com.wss.Dao包下创建实体类User,用于封装表单中追加的数据,即用户名、密码、
电话和地址。User中要包含两个属性,用于封装用户名、密码电话和地址,并给属性提供get和set方法,代码如下:
package com.wss.D
public class User {
private int
public User()
System.out.println("Initialization the User......");
public int getId() {
System.out.println("Getting the ID");
public void setId(int id) {
System.out.println("Setting the ID");
public String getName() {
System.out.println("Getting the name");
public void setName(String name) {
System.out.println("Setting the name");
this.name =
public String getPassword() {
System.out.println("Getting the password");
public void setPassword(String password) {
System.out.println("Setting the password");
this.password =
public String getPhone() {
System.out.println("Getting the phone");
public void setPhone(String phone) {
System.out.println("Setting the phone");
this.phone =
public String getAddress() {
System.out.println("Getting the address");
public void setAddress(String address) {
System.out.println("Setting the address");
this.address =
步骤三:修改RegistAction,接受表单传入的参数
在RegistAction中,追加属性用于接受表单传入的用户名、密码、电话和地址参数,该属性的类型为User类型,名称为user,并为
该属性提供get和set方法。
在业务方法(execute())中输出属性user的值,代码如下:
package com.wss.
import com.opensymphony.xwork2.ActionS
import com.wss.Dao.S
import com.wss.Dao.U
import com.wss.Dao.UserD
public class RegistAction extends ActionSupport{
public RegistAction()
System.out.println("Initialization RegistAction....");
private User user =new User();
//private U
public User getUser() {
System.out.println("Getting the getUser");
public void setUser(User user) {
System.out.println("Setting the setUser");
this.user =
}private S
public void setCompany(String company)
System.out.println("Setting the company");
this.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/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"&
&base href="&%=basePath%&"&
&title&京东商城注册页面&/title&
&meta http-equiv="Content-Type" content="text/ charset=utf-8" /&
request.setCharacterEncoding("gbk");
&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&
&td&&input type="submit" value="注册"/&&/td&
&td&&input type="reset" value="重置" &&/td&
&s:fielderror /&
步骤五:测试
重新部署项目并启动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 getUserSetting the nameGetting the getUserSetting the passwordGetting the getUserSetting the phoneGetting the nameGetting the address用于调用set方法注入user对象各属性。
3&域模型注入二(Action中属性 private S没有用new创建对象)
步骤一:修改表单,追加演示数据
在regist.jsp修改表单,追加用学校、城市、院系三个文本框,模拟输入用户的相关信息,代码如下:
&%@ page language="java" import="java.util.*" pageEncoding="gbk"%&
&%@ page contentType="text/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"&
&base href="&%=basePath%&"&
&title&京东商城注册页面&/title&
&meta http-equiv="Content-Type" content="text/ charset=utf-8" /&
request.setCharacterEncoding("gbk");
&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" /&
&td&&input type="submit" value="注册"/&&/td&
&td&&input type="reset" value="重置" &&/td&
&s:fielderror /&
步骤二:创建实体类
创建包com.wss.Dao,用于存放实体类。在com.wss.Dao包下创建实体类School,用于封装表单中追加的数据,即学校、城市和
School中要包含三个属性,用于封装学校、城市和院系,并给属性提供get和set方法,代码如下:
package com.wss.D
public class School {
public School()
System.out.println("Initilization School....");
public String getName() {
System.out.println("Getting the school name");
public void setName(String name) {
System.out.println("Setting the school name");
this.name =
public String getCity() {
System.out.println("Getting the school city");
public void setCity(String city) {
System.out.println("Setting the school city");
this.city =
public String getDepartment() {
System.out.println("Getting the school department");
public void setDepartment(String department) {
System.out.println("Setting the school department");
this.department =
步骤三:修改RegistAction,接受表单传入的参数
在RegistAction中,追加属性用于接受表单传入的学校、城市和院系,该属性的类型为School类型,名称为school,
并为该属性提供get和set方法。
在业务方法(execute())中输出属性school的值,代码如下:
package com.wss.
import com.opensymphony.xwork2.ActionS
import com.wss.Dao.S
import com.wss.Dao.U
import com.wss.Dao.UserD
public class RegistAction extends ActionSupport{
public RegistAction()
System.out.println("Initialization RegistAction....");
private User user =new User();
//private U
public User getUser() {
System.out.println("Getting the getUser");
public void setUser(User user) {
System.out.println("Setting the setUser");
this.user =
public School getSchool() {
System.out.println("Getting the getSchool");
public void setSchool(School school) {
System.out.println("Setting the setSchool");
this.school =
public void setCompany(String company)
System.out.println("Setting the company");
this.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/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"&
&base href="&%=basePath%&"&
&title&京东商城注册页面&/title&
&meta http-equiv="Content-Type" content="text/ charset=utf-8" /&
request.setCharacterEncoding("gbk");
&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" /&
&td&&input type="submit" value="注册"/&&/td&
&td&&input type="reset" value="重置" &&/td&
&s:fielderror /&
步骤五:测试
重新部署项目并启动tomcat,在浏览器中输入地址:http://localhost:8080/ShopDemo/regist.jsp
效果如下图所示(当然为了稍候测试方便,我自己输入了一些信息):
&点击提交,查看myeclipse的控制台,输出结果如下:
Initialization the User......Initialization RegistAction....Setting the companyGetting the getSchoolInitilization School....Setting the setSchoolSetting the school cityGetting the getSchoolSetting the school departmentGetting the getSchoolSetting the school nameGetting the getUserSetting the addressGetting the getUserSetting the nameGetting the getUserSetting the passwordGetting the getUserSetting the phoneGetting the nameGetting the addressThe company is 公司 The name is good The phone is wrwerGetting the school nameGetting the school cityGetting the school departmentThe school name is xuexiao The city is beiijng The department is shuxue
&控制台输出的顺序可以证明代码的执行顺序为:实例化Action--&实例化User并注入参数--&调用set方法注入User对象--&调用业务
在这里,private User user =new User();和private S不一样,school只是一个引用,并没有用new创建出对象,
所以在对school的各属性name、city和department用set方法注入时,用getSchool方法得到school对象时(Getting the getSchool),还没有school对象的存在,此时调用School的构造函数进行初始化创建school对象;然后通过school对象对其各
属性用setName....方法对school的各属性进行注入。
阅读(...) 评论()

我要回帖

更多关于 struts2拦截jsp页面 的文章

 

随机推荐