Spring通过struts1的研究制作了属于自己的struts—SpringMVC 其原理同struts1类似,通过框架拦截,进行访问到框架中的类容,从而只需要通过spring容器进行bean的获取,构造不同功能的类的连接,降低了代码量,并且更清楚直白的体现每个功能之间的衔接
ModelAndView类的返回值获取请求页面的详细解析:
示例: 我们构建springMVC框架需要在web.xml中写入拦截路径和构造org.springframework.web.servlet.DispatcherServlet的拦截类: 在DispatcherServlet类下配置的param的value值即是所需要配置的WEB-INF下的容器文件名
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name></display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
<!-- 如果有路径要用"/"开始 -->
classpath:beans.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>hncu</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 自定义配置"配置文件"的位置与名称,若没有配置该初始化参数,则是默认文件名:servletName-servlet.xml -->
<init-param>
<param-name>namespace</param-name>
<param-value>hncu-servlet</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>hncu</servlet-name>
<url-pattern>/sp/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
通过WEB-INF下所配置的同名容器进行所需要的bean的加载: hncu-servlet.xml:
<?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
">
<!-- 解析视图 -->
<bean class="org.springframework.web.servlet.view.ResourceBundleViewResolver">
<property name="basenames">
<list>
<!-- 默认查询classpath:hncu.properties -->
<value>hncu</value>
</list>
</property>
<!-- 设置默认父类view -->
<property name="defaultParentView" value="hncu"></property>
</bean>
</beans>
在src下写入beans.xml容器进行构造后台Controller(Action) 通过org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping实现注解方式进行controller的搭建 通过context:component-scan的路径配置每当访问时,spring会自动进入文件下查找包含所请求路径的注解值
<?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
">
<!-- 分开的容器需要添加映射 -->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"></bean>
<context:component-scan base-package="cn.hncu"></context:component-scan>
</beans>
controller: 通过返回的值hncu-servlet.xml中的org.springframework.web.servlet.view.ResourceBundleViewResolver解析视图类会通过查询hncu.properties中的url和view的类型来决定传输访问的页面的路径和页面类容
package cn.hncu.demo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class DemoController {
@RequestMapping(value = "/one")
public String one() {
System.out.println("123");
return "hncu";
}
}
hncu.properties:
hncu.(class)=org.springframework.web.servlet.view.JstlView
hncu.url=/jsps/one.jsp