一、属性驱动
action类接收三个参数 username、password、type
1、CheckLogin1.java类
package cn.zhanghan.checkaction;
public class CheckLogin1 {
private String username;
private String password;
private String type;
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;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
// 从前台页面接收到参数后会在这个方法里打印
public String checkLogin(){
System.out.println(username+" "+password+" "+type);
return "ok";
}
}
2、对应的struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="false" value="struts.enable.DynamicMethodInvocation"/>
<constant value="true" name="struts.devMode"/>
<package name="default" extends="struts-default" namespace="/">
<!--name:访问名称 -->
<action name="checklogin1" method="checkLogin" class="cn.zhanghan.checkaction.CheckLogin1">
<!--配置返回值到页面-->
<result name="ok">/success.jsp</result>
</action>
</package>
</struts>
3、web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>struts2.217</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
4、前台的Login.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>
<div align="center">
<h1>Login1</h1>
<form action="checklogin1" method="post">
username:<input type="text" name="username"/><br/><br/>
password:<input type="password" name="password"/><br/><br/>
type:<input type="text" name="type"/><br/><br/>
<input type="submit" value="Login"/>
</form>
</div>
</body>
</html>
5、登录成功success.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 'success.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>
<div align="center">
<h1>普通传值</h1>
Username:${username }<br/>
Password:${password }<br/>
Type:${type }<br/>
</div>
</body>
</html>
6、页面效果
image.png image.png在实践后会发现,如果遇到参数非常多的情况,那么就需要在Action类中写非常多的属性以及对应的get/set方法.所以这种方式不太可取.解决问题的方法必然是封装一个JavaBean.这就用到了Strut2的第二种传值方式--DomainModel
二、DomainModel对象驱动(表达式封装)
首先要创建一个存储的JavaBean,使用表达式封装可以吧表单数据封装到实体类里面
实现过程
1、在action里头生命实体类
2、生成实体变量的set和get方法
3、在表单输入项的name属性值写表达式形式
1、UserBean类 User.java文件
package cn.zhanghan.bean;
public class User {
private String username;
private String password;
private String type;
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;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
2、CheckLogin2.java类
package cn.zhanghan.checkaction;
import cn.zhanghan.bean.User;
public class CheckLogin2 {
private User u;
public User getU() {
return u;
}
public void setU(User u) {
this.u = u;
}
public String checkLogin(){
System.out.println(u.getUsername()+" "+u.getPassword()+" "+u.getType());
return "ok";
}
}
3、对应的对应的struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="false" value="struts.enable.DynamicMethodInvocation"/>
<constant value="true" name="struts.devMode"/>
<package name="default" extends="struts-default" namespace="/">
<action name="checklogin2" method="checkLogin" class="cn.zhanghan.checkaction.CheckLogin2">
<!--配置返回值到页面-->
<result name="ok">/success.jsp</result>
</action>
</package>
</struts>
4、web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>struts2.217</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
5、前台的Login.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>
<div align="center">
<div align="center">
<h1>Login2</h1>
<form action="checklogin2" method="post">
username:<input type="text" name="u.username"/><br/><br/>
password:<input type="password" name="u.password"/><br/><br/>
type:<input type="text" name="u.type"/><br/><br/>
<input type="submit" value="Login"/>
</form>
</div>
</body>
</html>
6、登录成功success.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 'success.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>
<div align="center">
<div align="center">
<h1>BeanModel传值</h1>
Username:${u.username }<br/>
Password:${u.password }<br/>
Type:${u.type }<br/>
</div>
</body>
</html>
7、页面效果
image.pngimage.png
实际上User类不需要实例化,struts会自动帮你实例化,但前提条件是,传值时需要使用对象.参数名的方式进行传递.
除了这种传值方式外,struts2还提供另外一种传值方式.
三、ModelDriven模型驱动
1、依然要创建User的JavaBean User.java文件
package cn.zhanghan.bean;
public class User {
private String username;
private String password;
private String type;
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;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
2、CheckLogin3.java类
需要实现ModelDriven<T>接口
package cn.zhanghan.checkaction;
import com.opensymphony.xwork2.ModelDriven;
import cn.zhanghan.bean.User;
public class CheckLogin3 implements ModelDriven<User>{
private User user;
public String checkLogin(){
System.out.println(user.getUsername()+" "+user.getPassword()+" "+user.getType());
return "ok";
}
@Override
public User getModel() {
if(null==user){
user=new User();
}
return user;
}
}
这种方式可以不用在Action类中编写对应的get/set方法,但是需要实例化User类.
3、对应的对应的struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="false" value="struts.enable.DynamicMethodInvocation"/>
<constant value="true" name="struts.devMode"/>
<package name="default" extends="struts-default" namespace="/">
<!--name:访问名称 -->
<action name="checklogin3" method="checkLogin" class="cn.zhanghan.checkaction.CheckLogin3">
<!--配置返回值到页面-->
<result name="ok">/success.jsp</result>
</action>
</package>
</struts>
4、web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>struts2.217</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
5、前台的Login.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>
<div align="center">
<h1>Login3</h1>
<form action="checklogin3" method="post">
username:<input type="text" name="username"/><br/><br/>
password:<input type="password" name="password"/><br/><br/>
type:<input type="text" name="type"/><br/><br/>
<input type="submit" value="Login"/>
</form>
</div>
</body>
</html>
6、登录成功success.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 'success.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>
<div align="center">
<h1>ModelDriven传值</h1>
Username:${username }<br/>
Password:${password }<br/>
Type:${type }<br/>
</div>
</body>
</html>
7、页面效果
image.pngimage.png
页面还是和普通传值一样.
四、属性驱动封装和模型驱动封装要注意的问题
- 在action中可以使用属性封装也可以使用驱动模型封装,但是不能同时使用属性封装和模型驱动封装来获取同一个表单数据,如果同时使用只会执行模型驱动
五、比较对象模型封装和驱动的模型封装
1、相同点
- 都可以吧数据封装在对象里
2、不同点
- 使用模型驱动只能把数据封装到一个实体类对象里面,不能在action用模型驱动封装把数据封装到不同实体类对象里
- 用表达式封装可以把数据可以封装到不同的对象里头
3、代码实现
创建User的JavaBean User.java文件
package cn.zhanghan.bean;
public class User {
private String username;
private String password;
private String type;
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;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
创建Bookr的JavaBean Book.java文件
package cn.zhanghan.bean;
public class Book {
private String bookname;
public String getBookname() {
return bookname;
}
public void setBookname(String bookname) {
this.bookname = bookname;
}
}
CheckLogin4.java类
package cn.zhanghan.checkaction;
import cn.zhanghan.bean.Book;
import cn.zhanghan.bean.User;
public class CheckLogin4 {
private User u;
private Book b;
public Book getB() {
return b;
}
public void setB(Book b) {
this.b = b;
}
public User getU() {
return u;
}
public void setU(User u) {
this.u = u;
}
public String checkLogin(){
System.out.println(u.getUsername()+" "+u.getPassword()+" "+u.getType()+" "+b.getBookname());
return "ok";
}
}
对应的对应的struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="false" value="struts.enable.DynamicMethodInvocation"/>
<constant value="true" name="struts.devMode"/>
<package name="default" extends="struts-default" namespace="/">
<!--name:访问名称 -->
<action name="checklogin4" method="checkLogin" class="cn.zhanghan.checkaction.CheckLogin4">
<!--配置返回值到页面-->
<result name="ok">/success.jsp</result>
</action>
</package>
</struts>
web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>struts2.217</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
前台的Login.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>
<div align="center">
<h1>Login4</h1>
<form action="checklogin4" method="post">
username:<input type="text" name="u.username"/><br/><br/>
password:<input type="password" name="u.password"/><br/><br/>
type:<input type="text" name="u.type"/><br/><br/>
bookname:<input type="text" name="b.bookname"/><br/><br/>
<input type="submit" value="Login"/>
</form>
</div>
</body>
</html>
登录成功success.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 'success.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>
<div align="center">
<h1>BeanModel传值封装到多个对象</h1>
Username:${u.username }<br/>
Password:${u.password }<br/>
Type:${u.type }<br/>
bookname:${b.bookname}
</div>
</body>
</html>
效果演示
image.png image.png
网友评论