Struts2(二)---将页面表单中的数据提交给Action

时间:2021-03-14 13:05:49

转载请注明http://blog.csdn.net/uniquewonderq

问题:在struts2框架下,如何将表单数据传递给业务控制器Action。

struts2中,表单想Action传递参数的方式有两种,并且这两种传参方式都是struts2默认实现的,他们分别是基本属性注入、域模型注入、其中

---基本属性注入,是将表单的数据项分别传入给Action中的一些基本基本类型。

---域模型注入,是将表单的数据项打包传入给Action中的一个实体对象。

我们继续使用项目Struts2的hello Struts实例,在其基础上使用这2中方式完成页面向Action的参数传递。具体的我们可以在项目首页index.jsp上追加表单,并在表单中模拟一些数据,将这些数据提交给HelloAction,最后在HelloAction中将接受的参数输出到控制台。

具体实现步骤:

1>基本属性注入

步骤一:

在Struts2Day01项目的index.jsp中,追加表单,并将该表单设置提交给HelloAction,即将form的action属性设置为:“/Struts2Day01/demo/hello.action”

在表单中增加一个文本框,用于输入一个姓名,该文本框的name属性值为name。代码如下:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
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>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>

<body>
This is my JSP page. <br><br>
<form action="/Struts2Day01/demo/hello.action" method="post">
<!-- 演示基本属性注入 -->
姓名:<input type="text" name="realName"/><br/><br/>
<input type="submit" value="提交"/>
</form>
</body>
</html>

步骤二:HelloAction中,接收表单传入的参数

在HelloAction中,追加属性并用于接收表单传入的姓名参数,该属性的名称要求与文本框的值相同(realName),并且该属性需要具备set方法。

在业务方法中输出属性realName的值。通知为了方便观察代码执行的顺序,在Action默认构造器中,输出任意的文字,代码如下:

package Action;

public class HelloAction {
public HelloAction(){
System.out.println("实例化Action...");
}
//定义基本类型属性,接受表单参数:姓名
private String realName;
public void setRealName(String realName){
System.out.println("注入参数realName...");
this.realName=realName;
}
public String sayHello(){
System.out.println("Hello,Action");
//输出基本类型数据
System.out.println("姓名:"+realName);
return "success";
}
}

步骤三:测试

重新部署该项目并启动tomcat,打开浏览器,针对当前的案例,在地址栏中输入地址:http://localhost:8888/StrutsDay01/

运行结果:

Struts2(二)---将页面表单中的数据提交给Action

点击提交:

myeclipse控制台输出:

Struts2(二)---将页面表单中的数据提交给Action


控制台输出的顺序可以证明代码的执行顺序:实例化Action--->调用set方法注入参数-->调用业务方法,当然这个过程是Struts2的API自行实现的,我们只需要在写代码时满足上述步骤中的要求即可。

由于index.jsp中的表单将请求提交给HelloAction,而HelloAction又会跳转到hello.jsp,因此最终浏览器显示的效果如下图:

Struts2(二)---将页面表单中的数据提交给Action

2>域模型注入

步骤一:修改表单,追加演示数据

在index.jsp修改表单,追加用户名、密码两个文本框,模拟输入用户的相关信息,代码如下:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
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>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>

<body>
This is my JSP page. <br><br>
<form action="/StrutsDay01/demo/hello.action" method="post">
<!-- 演示基本属性注入 -->
姓名:<input type="text" name="realName"/><br><br>
<!-- 演示域模型注入 -->
用户名:<input type="text"/><br><br>
密码:<input type="password"/><br><br>
<input type="submit" value="提交"/>
</form>
</body>
</html>


步骤二:创建实体类

创建包entity,用于存放实体类。在entity包下创建实体类User,用于封装表单中追加的数据,即用户名、密码。User中要包含两个属性,用于封装用户名、密码,并给属性提供get和set方法,代码如下:

package entity;

public class User {
private String userName;//用户名
private String password;//密码
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}


}


步骤三:修改HelloAction,接受表单传入的参数

在HelloAction中,追加属性用于接受表单传入的用户名、密码参数,该属性的类型为User类型,名称为user,并为该属性提供get和set方法。

在业务方法(sayHello())中输出属性user的值,代码如下:

package action;

import entity.User;

public class HelloAction {
public HelloAction(){
System.out.println("实例化 Action...");
}
//定义基本类型属性,接受表单参数:姓名
private String realName;
public void setRealName(String realName){
System.out.println("注入参数realName...");
this.realName=realName;
}
//定义实体对象属性,接受表单参数:用户名、密码
private User user;
public void setUser(User user){
this.user=user;
}
public User getUser(){
return this.user;
}
//在业务方法中输出"Hello,Action"
public String sayHello(){
System.out.println("Hello,Action");
//输出基本类型数据
System.out.println("姓名:"+realName);

//输出域模型方法注入的参数
System.out.println("用户名:"+user.getUserName());
System.out.println("密码:"+user.getPassword());
return "success";
}
}

步骤四:修改表单,设置文本框属性

在index.jsp中,修改表单新增的2个文本框name属性值。对于域模型注入的方式,文本框name属性值应该是具有"对象名.属性名"格式的表达式。其中对象名指的是Action中的实体类型属性名,属性名指的是实体类型中的属性名,代码如下:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
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>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>

<body>
This is my JSP page. <br><br>
<form action="/StrutsDay01/demo/hello.action" method="post">
<!-- 演示基本属性注入 -->
姓名:<input type="text" name="realName"/><br><br>
<!-- 演示域模型注入 -->
用户名:<input type="text" name="user.userName"/><br><br>
密码:<input type="password" name="user.password"/><br><br>
<input type="submit" value="提交"/>
</form>
</body>
</html>

步骤五:测试

重新部署项目并启动tomcat,在浏览器中输入地址:http://localhost:8888/StrutsDay01/

效果如下图所示(当然为了稍候测试方便,我自己输入了一些信息):

Struts2(二)---将页面表单中的数据提交给Action

点击提交,查看myeclipse的控制台,输出结果如下:

Struts2(二)---将页面表单中的数据提交给Action


控制台输出的顺序可以证明代码的执行顺序为:实例化Action-->实例化User并注入参数-->调用set方法注入User对象-->调用业务方法。

最终浏览器显示的效果如下图:

Struts2(二)---将页面表单中的数据提交给Action