SpringMVC 笔记——访问静态资源DispatcherServlet 配置问题

一、普通方式访问

web.xml 关键配置:

<servlet-mapping>
		<servlet-name>default</servlet-name>
		<url-pattern>*.jsp</url-pattern>
</servlet-mapping>
<servlet-mapping>
		<servlet-name>default</servlet-name>
		<url-pattern>*.jpg</url-pattern>
</servlet-mapping>
<servlet-mapping>
		<servlet-name>default</servlet-name>
		<url-pattern>*.html</url-pattern>
</servlet-mapping>
<!--- 所有类型静态资源都可如此访问 -->

访问时,直接文件名访问:

localhot:8080/projectName/.../fileName.jpg

二、SpringMVC 的DispatcherServlet 中处理

DispatcherServlet 配置文件:

<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:mvc="http://www.springframework.org/schema/mvc"
     	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-4.0.xsd
        			http://www.springframework.org/schema/mvc 
        			http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
         
         
         <!-- 扫描Controller 组件基础包 -->
         <context:component-scan base-package="com.milan.web.controller"></context:component-scan>
         
         <!-- 配置视图解析器 如Controller方法返回字符串解析为实际的物理视图 -->
         <!-- 假设所有jsp页面放在webapp目录下自建的views文件夹中 -->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
             <property name = "prefix" value="/views/"></property>
             <property name = "suffix" value = ".jsp"></property>
             <property name="order" value="1"></property>
         </bean>
</beans>

com.milan.web.controller.TestController 组件:

package com.milan.web.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/request")
public class TestController {
	
	@RequestMapping("/testController")
	public String getString(){
		System.out.println("hello world");
		return "success";
	}
}

web.xml 关键配置:

<listener>
	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  	</listener>
  	<listener>  
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>  
</listener>
  
<servlet>
	<servlet-name>dispatcherServlet</servlet-name>
	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	<load-on-startup>1</load-on-startup>
</servlet>
  	<servlet-mapping>
	<servlet-name>dispatcherServlet</servlet-name>
	<url-pattern>/</url-pattern>
</servlet-mapping>

测试:

1、访问路径:

http://localhost:8082/Test/request/testController
结果:会进入Controller,但页面提示500 错误:java.lang.NoSuchMethodError: javax.servlet.ServletContext.getEffectiveMajorVersion()I

2、在web.xml中添加如下配置:

<servlet-mapping>
		<servlet-name>default</servlet-name>
		<url-pattern>*.jsp</url-pattern>
</servlet-mapping>
结果:会进入Controller ,并显示页面。

问题:

既然要配置第二步中的信息才能正确返回页面,那么DispatcherServlet 配置文件中配置 InternalResourceViewResolver 视图解析器有啥作用?

测试:去掉配置的 InternalResourceViewResolver ,再次访问 http://localhost:8082/Test/resource/testController 将提示404 错误,说明配置的视图解析器解决了访问资源的路径问题,定位资源位置,而web.xml 中配置的default 的servlet-mapping 才会真正渲染页面并传给客户端。


三、mvc:default-servlet-handler 、 mvc:annotation-driven 与 mvc:resources

这三项配置在DispatcherServlet 的配置文件中。

对第一项具体作用不太了解,按其注释,它会将所有请求指向默认的Servlet (应该是对应的default的servlet),其order值是Integer.MAX,所以其它的servlet必须设置order比该值小才会起作用。既然是指向default 的servlet ,但当自己去掉web.xml中关于jsp映射路径的default的配置后,将无法访问页面,虽然已经配置了该项。

第二项,它驱动了Controller 组件的映射模型,即使@RequestMapping 注解能工作。注意:若配置了MVC:default-servlet-handler,则必须配置此项,否则Controller 组件@RequestMapping 将不起作用,即Controller 无法访问。

配置方式:

<mvc:default-servlet-handler/>
<mvc:annotation-driven></mvc:annotation-driven>


第三项,实现资源路径映射,见上一篇。


还在学基础,这里好多错误与累赘,后面慢慢整理。













  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在开发SpringMVC应用时,需要配置DispatcherServlet来处理所有的请求和响应。以下是配置DispatcherServlet的步骤: 1. 在web.xml文件中配置DispatcherServlet: ```xml <servlet> <servlet-name>springMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/springMVC-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springMVC</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> ``` 2. 在上面的配置中,我们指定了DispatcherServlet的名称为springMVC,并将请求URL的根路径指定为DispatcherServlet的映射路径。 3. 我们还需要创建一个springMVC-servlet.xml文件用于配置DispatcherServlet的上下文。在这个文件中,我们可以配置视图解析器、拦截器、控制器等。 ```xml <beans xmlns="http://www.springframework.org/schema/beans" 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.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <mvc:annotation-driven /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value=".jsp" /> </bean> <bean id="handlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" /> <bean name="helloController" class="com.example.controller.HelloController" /> </beans> ``` 在这个配置文件中: - 使用`<mvc:annotation-driven />`启用注解驱动的Spring MVC。 - 配置视图解析器,这里使用InternalResourceViewResolver来解析JSP视图。 - 配置控制器,这里使用BeanNameUrlHandlerMapping和HelloController来处理请求。 以上就是配置DispatcherServlet的步骤,可以根据实际需求进行配置

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值