BOS项目01_04_搭建web环境、struts和spring的配置文件配置、struts的拦截映射dispatcher标签中配置FORWARD的作用

系列文章目录

前言

一、struts和spring的配置文件配置

在web中配置Struts

1.添加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="struts.devMode" value="true" />
	<package name="basicstruts2" extends="struts-default">
		<!-- 配置需要进行权限控制的页面访问的规则 -->
		<action name="page_*_*">
			<result name="success" type="dispatcher">/WEB-INF/pages/{1}/{2}.jsp</result>
		</action>
	</package>
</struts>

具体操作
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
struts.xml配置完成之后下一步我们做什么呢?
我们就需要在web.xml里面去配置struts的拦截器
2.在web.xml配置Struts的过滤器
在这里插入图片描述

在这里插入图片描述
参考(先只按上图写,这里只是参考)

<!-- 1.配置struts过滤器 --> 
  <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>
  	<!-- 配置请求和内部转发都会经过Strut过滤器 -->
  	<dispatcher>REQUEST</dispatcher>
  	<dispatcher>FORWARD</dispatcher>
  </filter-mapping>

这里我们发现一个问题,我们有struts配置文件要配还有Spring配置文件要配,还有hibernate,
我们可以新建一个config文件夹,来保存它们
在这里插入图片描述
在这里插入图片描述
然后把struts.xml挪(剪切、移动)到config文件夹里面去,但是,要要注意,挪上去之后默认是不会在src下加载的
那怎么办呢?
在这里插入图片描述
注意要选择对,不对的话,后面测试项目,项目报404,而且没搞过的半天找不到原因(我是走过坑的)
点一是这个
图标会相应变化,变化之后
在这里插入图片描述

在这里插入图片描述
这样我们的代码和配置文件就是不同的目录了,这样写的好处就是,更清晰明朗
代码就和代码在一起,配置文件就和配置文件在一起

在web.xml配置Spring的监听器

1.添加spring的applicationContext文件
在这里插入图片描述

直接拷贝下面的就行

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
						http://www.springframework.org/schema/beans/spring-beans.xsd
						http://www.springframework.org/schema/context
						http://www.springframework.org/schema/context/spring-context.xsd
						http://www.springframework.org/schema/aop
						http://www.springframework.org/schema/aop/spring-aop.xsd
						http://www.springframework.org/schema/tx 
						http://www.springframework.org/schema/tx/spring-tx.xsd">

</beans>

在这里插入图片描述
然后我们在web.xml里面也要改一些东西

在这里插入图片描述
参考如下

  <!-- 2.配置spring的监听器 -->
  <context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  <listener>
  	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

写完之后,我们就可以让它先跑起来
在这里插入图片描述
在这里插入图片描述

二、struts的拦截映射中配置FORWARD的作用

加上这个
在这里插入图片描述
这样的话请求和转发都会被struts2拦截,默认情况下,只有请求会被拦截
在web.xml中配置forward是为了内部转发也能被struts的过滤拦截
在这里插入图片描述
在这里插入图片描述
如果我现在要访问这两个页面要怎么访问呢?
在这里插入图片描述
这样就可以访问,{1}和{2}相当于是一个变量
在这里插入图片描述
很简单,根据struts配置的规则访问即可
在这里插入图片描述

测试:创建一个action然后跳转到WEB-INF内的某个jsp文件
在这里插入图片描述
在这里插入图片描述
然后要在struts里面去;配置好这个类
这里要注意,他们还是在同一个包(package)里面,只是是不同的action,不要瞎搞,去新建一个package
在这里插入图片描述
这里我们要知道,配置了struts之后,默认是转发,不是重定向(重定向就会改变地址了)
在这里插入图片描述
在这里插入图片描述
这样,就能实现我们想要的效果了
在这里插入图片描述
在这里插入图片描述
演示有无FORWARD的作用是否一样
在这里插入图片描述
取消dispatcher的注释
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值