一、普通方式访问
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>
第三项,实现资源路径映射,见上一篇。
还在学基础,这里好多错误与累赘,后面慢慢整理。