springMVC配置时,静态资源和jsp文件路径没错但是访问时controller的请求报404错误。

springMVC配置时,静态资源和jsp文件路径没错但是访问时controller的请求报404错误。

1.场景

      如果在web.xml中servlet-mapping的url-pattern设置的是/,而不是如.do。表示将所有的文件,包含静态资源文件都交给spring mvc处理。就需要用到<mvc:annotation-driven />了。如果不加,DispatcherServlet则无法区分请求是资源文件还是mvc的注解,而导致controller的请求报404错误。

<servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
 </servlet-mapping>

基础的springmvc.xml的配置:

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

	<!-- 定义组件扫描器,指定需要扫描的包 -->
	<context:component-scan
		base-package="com.lhq.controller"></context:component-scan>


	<!--注解驱动,以使得访问路径与方法的匹配可以通过注解配置-->
	<mvc:annotation-driven /><!-- 如果在web.xml中servlet-mapping的url-pattern设置的是/,而不是如.do。表示将所有的文件,包含静态资源文件都交给spring mvc处理。就需要用到<mvc:annotation-driven />了。如果不加,DispatcherServlet则无法区分请求是资源文件还是mvc的注解,而导致controller的请求报404错误。 -->

	<!-- 解除servlet对静态资源文件访问的限制,使得静态资源先经过 -->
	<!-- <mvc:default-servlet-handler /> -->


	<!--配置静态资源的访问映射,此配置中的文件,将不被前端控制器拦截 -->
	<!-- <mvc:resources location="/js/" mapping="/js/**" />
	<mvc:resources location="/css/" mapping="/css/**" />
	<mvc:resources location="/fonts/" mapping="/fonts/**" />
	<mvc:resources location="/images/" mapping="/images/**" /> -->

	<!-- 定义视图解析器 -->
	<bean id="viewResolver"
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<!-- 设置前缀解析器 -->
		<property name="prefix" value="/background/" />
		<!-- 设置后缀解析器 -->
		<property name="suffix" value=".jsp" />
	</bean>

	<!-- 配置拦截器 -->
	<mvc:interceptors>
		<mvc:interceptor>
			<mvc:mapping path="/**" />
			<bean class="com.lhq.interceptor.LoginInterceptor" />
		</mvc:interceptor>
	</mvc:interceptors>

</beans>
	
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个基本的Spring MVC配置文件示例: ```xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 配置扫描的包 --> <context:component-scan base-package="com.example.controller"/> <!-- 配置视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"/> <property name="suffix" value=".jsp"/> </bean> <!-- 启用Spring MVC注解驱动 --> <mvc:annotation-driven/> <!-- 配置静态资源的处理 --> <mvc:resources mapping="/static/**" location="/static/"/> <!-- 配置拦截器 --> <mvc:interceptors> <bean class="com.example.interceptor.LoginInterceptor"/> </mvc:interceptors> </beans> ``` 其, - `context:component-scan` 指定需要扫描的控制器所在的包。 - `InternalResourceViewResolver` 是Spring MVC的视图解析器,它将视图名称解析为JSP文件路径。 - `mvc:annotation-driven` 启用Spring MVC注解驱动,允许在控制器使用注解,如 `@RequestMapping` 等。 - `mvc:resources` 配置静态资源的处理,比如CSS、JS、图片等静态文件。 - `mvc:interceptors` 配置拦截器,可以在请求到达控制器之前或之后执行一些操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值