【Struts2】二_Struts2参数映射、核心配置文件struts.xml中的标签与属性的使用

news2025/3/1 20:14:55

文章目录

      • Struts2
        • 一、参数映射:
          • ▶传递基本数据类型:
          • ▶传递对象
        • 二、核心配置文件struts.xml:
          • 2.1、constant标签
          • 2.2、package标签
          • 2.3、action标签
        • 三、Action配置:
          • 3.1、Action简介:
          • 3.2、继承ActionSupport类:
          • 3.3、动态方法调用:(不安全)
          • 3.4、使用通配符:
          • 3.5、默认Action:
        • 四、Result配置:
          • 4.1、dispatcher
          • 4.2、redirect
          • 4.3、redirectAction
          • 4.4、chain
          • 4.5、动态结果:
          • 4.6、全局结果:
        • package里面标签的顺序:

Struts2


项目的初始配置见:idea快速搭建struts2框架:https://blog.csdn.net/m0_70083523/article/details/127899288
额外的其他jar包,可能会用到:

<!-- lombok -->
<dependency>
	<groupId>org.projectlombok</groupId>
	<artifactId>lombok</artifactId>
	<version>1.16.12</version>
</dependency>
<!-- jstl -->
<dependency>
	<groupId>javax.servlet.jsp.jstl</groupId>
	<artifactId>jstl</artifactId>
	<version>1.2</version>
</dependency>
<!-- jsp-api -->
<dependency>
	<groupId>javax.servlet</groupId>
	<artifactId>jsp-api</artifactId>
	<version>2.0</version>
	<scope>provided</scope>
</dependency>
<!-- servlet-api -->
<dependency>
	<groupId>javax.servlet</groupId>
	<artifactId>servlet-api</artifactId>
	<version>2.5</version>
	<scope>provided</scope>
</dependency>

一、参数映射:


▶传递基本数据类型:
public class HelloAction implements Action {

    @Getter
    @Setter
    private String name;
    @Getter
    @Setter
    private String message;

    public String execute() throws Exception {
        System.out.println("hello struts2~~~struts构建完成");
        System.out.println("接收到的name是===="+name);
        message="----message----"; //相当于request.SetAttribute();
        return "OK";  
    }
}

【读取请求参数的时候使用Set方法,在浏览器页面读取值得时候使用get方法,必须要给属性进行get/set方法】

<%@ page contentType="text/html;charset=UTF-8" isELIgnored="false" language="java" %>
<html>
<head>
    <title>Hello</title>
</head>
<body>
<h2>struts2</h2>
<br/>
<form action="hello" method="post">
    <input name="name">
    <input type="submit" value="提交">
</form>
 <span>后端读取值:${requestScope.message}</span>
</body>
</html>

在这里插入图片描述
在这里插入图片描述

▶传递对象

创建实体类User:set/get方法使用了注解

@Getter
@Setter
@ToString
public class User {
    private Integer id;   //id
    private String userName; //用户名称
}
public class HelloAction implements Action {
    @Getter
    @Setter
    private User user;

    public String execute() throws Exception {
        System.out.println("hello struts2~~~struts构建完成");
        System.out.println("user==="+user);
        return "OK";  //返回的结果
    }
}

传值:属性名.实体类属性如:user.id

<%@ page contentType="text/html;charset=UTF-8" isELIgnored="false" language="java" %>
<html>
<body>
<h2>struts2</h2>
<br/>
<form action="hello" method="post">
    id:<input name="user.id"><br/>
    userName:<input name="user.userName"><br/>
    <input type="submit" value="提交">
</form>

</body>
</html>

在这里插入图片描述

二、核心配置文件struts.xml:


2.1、constant标签

<!--设置请求后缀默认是.action-->
<!--设置请求后缀:http://localhost:8080/hello.do -->
<constant name="struts.action.extension" value="do,html"/>

<!--设置编码,解决中文乱码-->
<constant name="struts.i18n.encoding" value="utf-8"/>

<!--设置struts标签主题-->
<constant name="struts.ui.theme" value="simple"/>
  • constant用来配置常量。name属性是常量名,value属性是常量值。
  • constant常量可以改变Struts2的一些行为,比如UI标签的样式、编码格式等。
  • 因为struts2默认的编码格式就是UTF-8,所以不用特意指定编码,中文也不会乱码
2.2、package标签

<package name="default" namespace="/" extends="struts-default">

package是包。Struts2的package与java中的package类似,可以把同一个业务模块的action和result集中到一个包中,方便管理。不同的是Struts2的包可以继承,比如商品有增删改查操作,订单也有增删该查操作,我们可以将商品和订单的action分别放两个package中方便管理。

  • name属性是包的名字,一个struts.xml中可以有很多个package,通过name属性进行区分。
  • namespace是命名空间,/代表的是根目录。namespace的作用类似于SpringMVC中在Controller类上加@RequestMapping注解。相当于此包中所有的action前都加一个父路径。如:
  • extends属性是继承,通常都会继承struts-default。在struts-default中定义了大星的struts特性,如拦截器和参数处理的功能,如果不继承struts-default,会遇到参数无法绑定或找不到action类。
<!-- namespace默认是根路径上 -->
<package name="default" namespace="/user" extends="struts-default">
	<action name="hello" class="com.wxy.action.HelloAction">
		<result name="success">/index.jsp</result>
	</action>
</package>


在这里插入图片描述

2.3、action标签

<action name="login" class="com.wxy.action.LoginAction">
	<!-- success是Action类中返回的字符串,根据不同字符串返回不同的页面-->
	<result name="success">index.jsp</result>
	<result name="error">error.jsp</result>
	<result name= "input">login.jsp</result>
</action>

action标签用来处理请求和响应结果。

  • name属性是请求的名字,此处不需要加.action。同一个package下的action不能重名。
  • class属性指定处理该请求的类,是类的全路径。默认的处理请求时会去类中找名为execute的方法。如果不指定class,将默认ActionSupport为处理请求的类。
  • result标签用来处理请求结果,name属性是Action类中返回的字符串。标签的值是要跳转的页面地址。name如果不写的话,默认是success。

示例:
实体类:

@Data
public class User {
    private String userName;
    private String password;
}

UserAction类:

public class UserAction extends ActionSupport {
    @Getter
    @Setter
    private User user;

    public String execute() throws Exception {
        System.out.println("用户名:" + user.getUserName());
        System.out.println("密码:" + user.getPassword());

        if ("admin".equals(user.getUserName()) && "1234567".equals(user.getPassword())) {
            // 将user对象保存在session中
            Map<String, Object> session = null;
            session = ActionContext.getContext().getSession();
            session.put("user_session", user);
            return "success";
        } else {
            return "error";
        }
    }
}

login.jsp:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>登录</title>
</head>
<body>
<form action="hello" method="post">
    用户名:<input type="text" name="user.userName" /><br />&nbsp;码:<input type="password" name="user.password" /><br />
    <input type="submit" />
</form>
</body>
</html>

success.jsp:

<%@ page contentType="text/html;charset=UTF-8"  isELIgnored="false" language="java" %>
<html>
<head>
    <title>登录成功</title>
</head>
<body>
<h2>欢迎您,${sessionScope.get("user_session").userName}</h2>
</body>
</html>

error.jsp:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>登录失败</title>
</head>
<body>
<h2>用户名或密码不匹配。</h2>
</body>
</html>

struts.xml:

<package name="userAction" namespace="/" extends="struts-default">
    <action name="login" class="com.wxy.action.UserAction" method="execute">
        <result name="success">/success.jsp</result> <!--登录成功返回的页面-->
        <result name="error">/fail.jsp</result> <!--登录失败返回的页面-->
    </action>
</package>

访问路径:localhost:8080/login

三、Action配置:


3.1、Action简介:

【Struts的业务核心是Action类】前面我们已经使用过了此类实现Action接口

  • 一个Action业务里可以实现Action接口,也可以继承ActionSupport类。在ActionSupport中提供了一些实现好的业务方法。在以后的编程中,建议全部继承ActionSupport类。
  • Action中的方法必须返回一个String类型的字符串,这个字符串与struts.xml中result标签的name属性相对应,struts.xml会根据返回的字符串查找对应的页面。
3.2、继承ActionSupport类:

下面讲述继承ActionSupport类的使用:

可以存在多个方法且可自定义,但是都得返回String类型:
创建ActionTest类:

public class TestAction extends ActionSupport {
    @Setter
    @Getter
    private User user;

    //登录的方法
    public String login() {
        System.out.println("登录方法 ");
        return SUCCESS;
    }
    //注册的方法
    public String register() {
        System.out.println("注册方法 ");
        return SUCCESS;
    }
}

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>
    <package name="default" namespace="/user" extends="struts-default">
    	<!--访问路径:localhost:8080/user/login-->
        <action name="login" class="com.wxy.action.TestAction" method="login">
            <result name="success">/index.jsp</result>
        </action>
        <!--访问路径:localhost:8080/user/register-->
        <action name="register" class="com.wxy.action.TestAction" method="register">
            <result name="success">/index.jsp</result>
        </action>
    </package>
</struts>

action标签method属性:调用具体是类中的哪个方法(method=“方法名”)

  • method="login"表示要调用类中的login()方法处理请求。如果找不到login()方法,默认会去找execute()方法,在找不到,报错。
3.3、动态方法调用:(不安全)

如果一个类中有多个业务方法,又不想给每个业务方法都配置一个action标签,可以使用动态方法调用,语法是:请求名!方法名.action

  • 当请求的格式是user!login.action时,代表调用UserAction中的login()方法处理当前请求。
  • 当请求的格式是user!regist.action时,代表调用UserAction中的regist()方法处理当前请求。

Struts2中可以开启调用动态方法,设置的常量为:

<constant name="struts.enable.DynamicMethodInvocation" value="true"/>
<?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="struts.enable.DynamicMethodInvocation" value="true"/>

<package name="default" namespace="/user" extends="struts-default"> 
	<!--指定哪些方法可以使用-->
	<global-allowed-methods>login,regist</global-allowed-methods>
	
	<!--将TestAction类中的方法统一定义为user -->
	<action name="user" class="com.wxy.action.TestAction">
		<result name="success">/index.jsp</result>
	</action>
</package>

登录访问路径:localhost:8080/user/user!login
注册访问路径:localhost:8080/user/user!register

3.4、使用通配符:

另—种减少action数量的方法是使用通配符:

<package name="default" namespace="/user" extends="struts-default"> 
	<global-a1lowed-methods>login,regist</global-a1lowed-methods>
	
	<action name="*User" class="com.wxy.action.TestAction" method="{1}">
		<result name="success">/index.jsp</result>
		<result name="error">{1}.jsp</result><!--失败了就返回原来的页面-->
	</action>
</package>

name属性值为*User就是匹配以User结束的请求,method="{1}"中的{1}匹配的就是User中的*

  • 登录访问路径:localhost:8080/user/loginUser
  • 注册访问路径:localhost:8080/user/registerUser
3.5、默认Action:

如果在struts.xml中找不到匹配的action,将会报错。可以设置一个默认的action,当所有请求都不匹配时,将匹配默认action。

<!--此标签放在package标签里面,<global-a1lowed-methods>标签前面-->
<default-action-ref name="default"/>
<action name="default">
	<result>/error.jsp</result>
</action>
  • < default-action-ref>对当前的package有效。
  • action标签的class省略调用的ActionSupport类,result的name省略将默认为success。
  • 注意default-actiop-ref必须在所有的action标签上面。也就是说default-action-ref出现在action标签之前。不然不符合DTD验证。

四、Result配置:


【常用结果有三种类型: dispatcher、redirect、redirectAction、chain】

4.1、dispatcher

result的默认类型就是dispatcher。以下两个标签是等价的:

<result name="success" type="dispatcher ">index.jsp</result>
<result name="success">index.jsp</result>
  • dispatcher的结果等同于Servlet中的请求转发,即:request.getRequestDispatcher(“success.jsp”).forward(request, response);
  • 请求转发的意思是当前请求中的参数、属性在下一个页面或请求中仍然可以使用。
4.2、redirect

redirect是重定向,重定向之后,当前请求中的参数和属性在下一个页面或请求中将不能使用。

<result name="success" type="redirect" >index.jsp</result>

相当于Servlet中的:response.sendRedirect(“success.jsp”); 地址栏改变

4.3、redirectAction

redirectAction与redirect类似,不过redirectAction是重定向到某一个action
主要redirectAction的action后缀是action

<action name="reg" class="com.wxy.action.UserAction" method="regist">
	<result name="success" type="redirectAction">login.action</result> <!--地址不是jsp页面-->
</action>

如果要调用不同package下的action,需要在result中传参数:

<!-- 另一个package -->
<package name="default" namespace="/" extends="struts-default"> 
<action name="login" class="com.wxy.action.UserAction" method="login">
	<result name="success" type="redirectAction">
		<!--调用不同package下的action-->
		<param name="namespace">/user</param>
		<param name="actionName">hello.action</param>
		<!--传递其它参数-->
		<param name="username">123</par am>
	</result>
	<result name="error">login.jsp</result>
</action>

此时访问路径:localhost:8080/test就会跳转到localhost:8080/user/hello.do

4.4、chain

redirectAction不能共享request中的数据,如果想共享数据,可以将type设置为chain。
注意chain的action后面没有后缀

<action name="test" class="com.wxy.action.UserAction" method="regist">
	<!--注意chain的action后面没有后缀-->
	<result name="success" type="chain">hello</result>
</action>
4.5、动态结果:

动态结果是根据某个属性不同的值返回到不同页面。

@Data
private String username;
@Data
private string page;
I

public String login() {
	//参数和业务略
	System.out.println("我是登录");
	if ("admin".equals(username)) {	//管理员去管理页
		page = "admin_page";
	}else {		//其他人去用户页
		page = "user_page";
	}
	return SUCCESS;
}
<action name="page" class="com.wxy.action.ResultTest" method="goPage">
   	<result name="success">${page}.jsp</result>
</action>

访问路径:

  • localhost:8080/page?page=admin_page 返回管理员管理页面
  • localhost:8080/page?page=user_page 返回用户使用页面
  • 可以用来设置访问权限
4.6、全局结果:

一个action中的result只能在当前action中有效。如果多数的action中都用到同一个结果,可以将此结果定义为全局结果。比如index代表用户没有登录,需要跳转到登录页面,那么可以设置成全局结果:

<global-results>
	<result name="success">index.jsp</result>
</global-results>

如果action中定义了与全局结果同名的result,将优先action标签中的。
注意global-results在action之前,default-action-ref之后。

在这里插入图片描述

package里面标签的顺序:

? 代表出现0次或1次
星号 代表0次或多次

  1. result-types?
  2. interceptors?
  3. default-interceptor-ref?
  4. default-action-ref?
  5. default-class-ref?
  6. global-results?
  7. global-exception-mappings?
  8. action*

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/18766.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

JAVA初阶——继承和多态

目录 一、继承 1、定义&#xff1a; 2、用法&#xff1a; 3、使用从父类继承的成员 &#xff08;1&#xff09;、子类使用从父类继承的成员变量 &#xff08;2&#xff09;、子类使用从父类继承的成员方法 4、super &#xff08;1&#xff09;、定义&#xff1a; 5、子…

ID3算法

目录 ID3算法 例子 ID算法总结 ID3算法 ID3算法是在每个结点处选取能获得最高信息增益的分支属性进行分裂 在每个决策结点处划分分支、选取分支属性的目的是将整个决策树的样本纯度提升 衡量样本集合纯度的指标则是熵&#xff1b; 举例来说&#xff0c;如果有一个大小为10的…

被裁后,狂刷607页JUC源码分析笔记,立马拿蚂蚁offer

前言 可能大家最近&#xff0c;在公众号&#xff0c;或者各大自媒体平台&#xff0c;都能够刷到&#xff0c;因为疫情美国经济面临结构性衰退&#xff0c;美联储疯狂印钞导致世界性经济波动&#xff0c;导致国际环境不是很好&#xff0c;也间接影响到了中国&#xff0c;中国也…

跟艾文学编程《Python基础》(2)Python 容器

作者&#xff1a; 艾文&#xff0c;计算机硕士学位&#xff0c;企业内训讲师和金牌面试官&#xff0c;公司资深算法专家&#xff0c;现就职BAT一线大厂。 邮箱&#xff1a; 1121025745qq.com 博客&#xff1a;https://wenjie.blog.csdn.net/ 内容&#xff1a;跟艾文学编程《Pyt…

JAVA微服务场景下分布式日志收集排查问题实战

问题产生的根由&#xff1f;不同服务的日志存在哪里&#xff1f;我们怎么去排查线上问题&#xff1f; 问题场景&#xff1a;我们部署的java服务可能有几十个&#xff0c;不同的项目里面他是看不到别的服务的日志&#xff0c;只有服务的返回msg消息&#xff0c;相比传统的单体服…

计算机体系结构:1.1.系统加速比计算例题

文章目录题目内容题目分析题目求解题目内容 假设在某程序的执行过程中&#xff0c;浮点操作时间占整个执行时间的10%&#xff0c;现希望对浮点 操作加速 。 &#xff08;1&#xff09;设对浮点操作的加速比为Sf&#xff0c;请推导出程序总的加速比S和Sf之间的关系表达式&#…

IoU的计算实现详解(基于Python)

文章目录1. 交并比&#xff08;IoU&#xff09;2.原理3.代码实现1. 交并比&#xff08;IoU&#xff09; 具体来说&#xff0c;它是两边界框相交部分面积与相并部分面积之比&#xff0c;如下所示&#xff1a; 也就是两个框的交集和两个框的并集之比。 2.原理 这里详细解释一…

如何安装与配置Node.js

Node.js发布于2009年5月&#xff0c;由Ryan Dahl开发&#xff0c;是一个基于Chrome V8引擎的JavaScript运行环境&#xff0c;使用了一个事件驱动、非阻塞式I/O模型&#xff0c; 让JavaScript 运行在服务端的开发平台&#xff0c;它让JavaScript成为与PHP、Python、Perl、Ruby等…

使用轻量应用服务器搭配宝塔面板搭建可道云kodbox私有云网盘的方法教程

你是否有过网盘下载速度只有十几KB&#xff0c;时不时出现网盘的文件被删除的问题&#xff0c;不如自己搭建一个云网盘吧&#xff0c;只需要一云服务器&#xff0c;即可搭建一个跟某度云一样的云盘。可以自由下载&#xff0c;不限制网速&#xff0c;随时都可上传下载。这篇文章…

nginx+redis+jvm三级缓存设计与落地实现

由于涉及到个人隐私,使用的是阿里云平台,所以下面的IP地址和密码我都做了修改。不是真实的地址。此模拟秒杀商品列表信息缓存案例实现的技术解决方案。 远程环境1版本操作系统ubuntu22.04openresty1.21.4.1jdkOracle JDK17IP192.168.1.1远程环境2版本redis7.0.5Springboot2.5…

149. SAP UI5 Table 控件数据进行 Excel 导出时如何进行格式控制

文章目录 字符串类型的显示控制数值类型(Number)的值显示控制日期和时间显示的格式控制布尔值的显示控制BigNumber 和百分比数值的显示总结本教程的前一步骤,我们成功的将 sap.m.Table 控件里显示的数据导出到了本地 Excel 文件中。 下图是使用 sap.m.Table 显示的表格页面:…

菜小白聊聊开源和开源协议

最近想入linux的深坑&#xff0c;于是开启了马哥sre课程的探险之旅。在了解到Linux是一款自由和开放源码的类UNIX操作系统的历史时&#xff0c;深深被开源精神所折服。也强烈感受到了开源精神的伟大。也正是因为有了开放源码的精神&#xff0c;才有了国产百花齐放的android系统…

【JUC源码专题】AQS 源码分析(JDK8)

文章目录同步队列同步队列结点 Node同步队列状态 state获取互斥锁acquire 方法tryAcquire 方法获取互斥锁addWaiter 方法enq() 入队acquireQueued()setHead 方法设置头节点shouldParkAfterFailedAcquire()parkAndCheckInterrupt()cancelAcquire 发生异常&#xff0c;取消线程获…

第七章第一节:顺序查找和折半查找

文章目录教程1. 查找的基本概念1.1 对查找表的常见操作1.2 查找算法的评价指标2. 顺序查找2.1 顺序查找的算法思想2.2. 顺序查找的实现2.3 查找效率分析2.4 顺序查找的优化&#xff08;对有序表&#xff09;2.5 用查找判定树分析ASL2.6 顺序查找的优化&#xff08;被查概率不相…

在centos中注册gitlab runner

注册runner 有几种不同的方式&#xff0c;这里介绍的是在centos7中使用 rpm包来安装 按照gitlab runner的官网链接里面的介绍&#xff1a; gitlab runner 链接 下载 官网描述&#xff1a; ## Replace ${arch} with any of the supported architectures, e.g. amd64, arm, ar…

python 空间滤波

均值滤波器 空域变换包含灰度变换和空间滤波 灰度变换是通过点对点的映射进行图像增强&#xff0c;是一种点运算 空间滤波是基于邻域的一种运算&#xff0c;即图像像素的灰度值不仅和当前点有关&#xff0c;还和这个点周围邻域像素点的灰度值有关。所以空间滤波其实是一种加…

如何管理oralce口令文件和参数文件

口令文件 口令文件审核 Step 1: 使用root账号将 oracle dba的权限移除 [rootoracle-db-19c ~]# su - oracle [oracleoracle-db-19c ~]$ [oracleoracle-db-19c ~]$ id oracle uid1501(oracle) gid1501(oinstall) groups1501(oinstall),1502(dba),1503(oper),1504(backupdba)…

浅析linux 内核 高精度定时器(hrtimer)实现机制(一)

1 hrtimer 概述 在Linux内核中已经存在了一个管理定时器的通用框架。不过它也有很多不足&#xff0c;最大的问题是其精度不是很高。哪怕底层的定时事件设备精度再高&#xff0c;定时器层的分辨率只能达到Tick级别&#xff0c;按照内核配置选项的不同&#xff0c;在100Hz到1000…

灵界的科学丨一、灵界在哪里?

摘自李嗣涔教授《灵界的科学》 在国内物理学界近十位学者的见证下&#xff0c; 发现我们所处四度时空的物质世界之外&#xff0c; 似乎还有一个世界的存在&#xff0c; 当年我把这个世界称作信息场&#xff0c; 也就是俗称的灵界。 二十世纪末宇宙大尺度谜团的重大发现──…

设计模式学习记录

设计模式 UML图&#xff1a; ------> 依赖 ——>关联 -------▲ 实现 —–—▲ 继承 &#x1f53a;———> 聚合 ▲———> 组合&#xff08;关联性更强&#xff09; 一、策略模式&#xff08;行为型&#xff09; 策略模式&#xff1a;是一种定义一系列算法的方法…