spring-mvc入门
2015-06-23 11:40
429 查看
spring-mvc入门
一、web.xml配置:
二、applicationContext.xml:
三、spring-mvc.xml(最重要的配置文件):
里面都带了注释,java代码太简单,在这里就不贴。
一、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代码太简单,在这里就不贴。
相关文章推荐
- Spring Web MVC 原理学习(上)
- JavaSE_基础库类_简介
- Java学习笔记Class类
- JavaSE_面向对象(封装、继承、多态)
- java mail
- java1.5新增Enum(枚举类)
- 【j2ee spring】37、巴巴运动网的产品品牌修改和查询
- Java培训Java序列化与static
- Java编程基础抽象类
- java中读取文件以及向文件中追加数据的总结
- Java面向对象的三大特征
- Java 读取数据流的几种情况总结
- Java编程基础抽象类
- stackoverflow中关于数组方法
- 浅析Java线程的三种实现
- 使用Spring2.5的Autowired实现注释型的IOC
- 第二个Spring冲刺周期团队进展报告
- Java类的继承总结
- Maven中央仓库信息速查 网址,java 源码下载地址
- java自带定时器 ScheduledExecutor