spring-mvc入门
一、web.xml配置:
<span style="font-size:18px;"><?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<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>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app></span>
二、applicationContext.xml:
<span style="font-size:18px;"><?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
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">
</beans></span>
三、spring-mvc.xml(最重要的配置文件):
<span style="font-size:18px;"><?xml version="1.0" encoding="UTF-8"?>
<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"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
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/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.plateno.web.action" />
<bean name="/index.html" class="com.plateno.web.action.HomeAction"></bean><!-- HomeAction要实现Controller接口 -->
<bean name="/" class="com.plateno.web.action.HomeAction"></bean>
<!-- 我知道的四种HandlerMapping -->
<!-- BeanNameUrlHandlerMapping和SimpleUrlHandlerMapping对应的handlerAdaptor都是SimpleControllerHandlerAdapter 他们都处理url和action的对应关系-->
<bean id="simpleControllerHandlerAdapter" class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>
<bean id="beanNameUrlHandlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">
<property name="interceptors" ref="httpRequestInterceptor"></property>
<property name="order" value="2"></property>
</bean>
<bean id="simpleUrlAction" class="com.plateno.web.action.SimpleUrlAction"></bean> <!-- SimpleUrlAction要实现Controller接口 -->
<bean id="simpleUrlHandlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="interceptors" ref="httpRequestInterceptor"></property>
<property name="order" value="1"></property>
<property name="urlMap">
<map>
<entry key="/" value-ref="simpleUrlAction"/>
<entry key="/index.html" value-ref="simpleUrlAction"/>
</map>
</property>
</bean>
<!-- 每种HandlerMapping都有对应的HandlerHandler,如果不对应,会报错 -->
<!-- DefaultAnnotationHandlerMapping和RequestMappingHandlerMapping都是处理requestMapping的注解,前者以废弃,不建议使用,后者用来代替前者 -->
<bean id="annotationMethodHandlerAdapter" class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"></bean>
<bean id="defaultAnnotationHandlerMapping" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="interceptors">
<list>
<ref bean="httpRequestInterceptor"/>
</list>
</property>
<property name="order" value="3"></property>
</bean>
<bean id="requestMappingHandlerAdapter" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>
<bean id="requestMappingHandlerMapping" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
<property name="interceptors">
<list>
<ref bean="httpRequestInterceptor"/>
</list>
</property>
<property name="order" value="4"></property>
</bean>
<bean id="freeMarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPath" value="/WEB-INF/pages/"/>
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="prefix" value=""/>
<property name="suffix" value=".html"/>
<property name="contentType" value="text/html;charset=UTF-8"/>
</bean>
<!-- handler处理之前,可以有拦截器,继承HandlerInterceptor接口就ok -->
<bean id="httpRequestInterceptor" class="com.plateno.interceptor.HttpRequestInterceptor"></bean>
</beans></span>
里面都带了注释,java代码太简单,在这里就不贴。