美文网首页struts2
struts2 的三种常用获取值封装

struts2 的三种常用获取值封装

作者: DouDouZH | 来源:发表于2018-05-06 12:33 被阅读0次

一、属性驱动

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.png
image.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.png
image.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

相关文章

  • struts2 的三种常用获取值封装

    一、属性驱动 action类接收三个参数 username、password、type 1、CheckLogin1...

  • Struts2干货笔记——第二天

    目录 1. Struts2提供了三种数据封装的方式 2. 封装数据到Collection和Map 3. Strut...

  • struts2 拦截器概述

    一、概述 1、struts2是框架,封装了很多的功能,struts2 里面封装的功能都封装在链接器里2、strut...

  • JavaWeb日记——浅析SSH框架

    Spring,Struts2和Hibernate(简称SSH)是JavaWeb很常用的三种框架,初学者一般从某种框...

  • AngularJS指令里的scope是个啥

    directive里,scope有三种取值,true/false/{}。 · scope的三种取值 false(默...

  • 拦截器

    一 概述 1 Struts2是框架,封装了很多功能,Struts2里面封装的功能都是在拦截器里面2 Struts2...

  • struts2获取值封装到List和Map

    一、封装数据到List 1、实现步骤 在action声明List 生成List变量的set和get方法 在表单输入...

  • 拦截器

    拦截器的概述: 1、struts2是框架,封装了很多的功能,struts2里面封装的功能都是在拦截器里面。2、st...

  • struts2 笔记

    Struts2 1. struts2的了解 1.1 struts的优势: 自动封装参数 参数校验 结果的处理(转发...

  • JavaScript基础 函数 案例

    封装的写法(以后经常用的写法) 用数组取值来代替if判断,比if好,运动速度也比较快;思路:获取判断的做判断,会返...

网友评论

    本文标题:struts2 的三种常用获取值封装

    本文链接:https://www.haomeiwen.com/subject/ecturftx.html