首页 > 代码库 > Spring3.2 MVC框架搭建

Spring3.2 MVC框架搭建

Spring3.2 MVC框架搭建

Java开发 > 分享 > Java框架

转自:http://www.liutime.com/javainfo/2255/

—————————————————————————————————————————————————

最近struts2曝的漏洞比较严重,特别写上Spring MVC框架搭建及使用教程

最新的jar包请到Spring官方网站下载:http://www.springsource.org/spring-community-download

1、每个jar包得用处如下:

org.springframework.aop--------------------Spring的面向切面编程,提供AOP(面向切面编程)实现

org.springframework.asm--------------------Spring独立的asm程序,相遇Spring2.5.6的时候需要asmJar 包.3.0开始提供他自己独立的asmJar

org.springframework.aspects----------------Spring提供对AspectJ框架的整合

org.springframework.beans------------------SpringIoC(依赖注入)的基础实现

org.springframework.context.support--------Spring-context的扩展支持,用于MVC方面

org.springframework.context----------------Spring提供在基础IoC功能上的扩展服务,此外还提供许多企业级服务的支持,如邮件服务、任务调度、JNDI定位、EJB集成、远程访问、缓存以及各种视图层框架的封装等

org.springframework.core-------------------Spring3.0的核心工具包

org.springframework.expression-------------Spring表达式语言

org.springframework.jdbc-------------------对JDBC的简单封装

org.springframework.orm--------------------整合第三方的ORM框架,如hibernate,ibatis,jdo,以及spring的JPA实现

org.springframework.test--------------------对Junit等测试框架的简单封装

org.springframework.web--------------------SpringWeb下的工具包

web-context.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:mvc="http://www.springframework.org/schema/mvc"    xsi:schemaLocation="    http://www.springframework.org/schema/beans    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd    http://www.springframework.org/schema/context    http://www.springframework.org/schema/context/spring-context-3.0.xsd    http://www.springframework.org/schema/mvc    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">    <!-- 启动扫描jlc下所有的controller -->     <context:component-scan base-package="com.jlc"/>    <mvc:annotation-driven/>    <mvc:default-servlet-handler/>    <!-- 避免IE执行AJAX时,返回JSON出现下载文件 -->    <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">        <property name="supportedMediaTypes">            <list>                <value>text/html;charset=UTF-8</value>            </list>        </property>    </bean>    <!-- 启动Spring MVC 的注解功能,完成请求和注解POJO的映射 -->    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">        <property name="messageConverters">            <list>                <ref bean="mappingJacksonHttpMessageConverter" />  <!-- json转换器 -->            </list>        </property>    </bean>     <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">          <property name="exceptionMappings">              <props>                  <prop key="org.springframework.web.servlet.PageNotFound">error/pageNotFound</prop>                  <prop key="org.springframework.dao.DataAccessException">error/dataAccessFailure</prop>                  <prop key="org.springframework.transaction.TransactionException">error/dataAccessFailure</prop>              </props>          </property>      </bean>       <!-- jsp页面解析器,当Controller返回XXX字符串时,先通过拦截器,然后该类就会在/WEB-INF/views/目录下,查找XXX.jsp文件-->    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">        <property name="prefix" value="http://www.mamicode.com/WEB-INF/views/"/>        <property name="suffix" value="http://www.mamicode.com/.jsp"/>    </bean></beans>

web.xml配置

<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5"     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_2_5.xsd"> <listener>        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>    </listener>    <context-param>        <param-name>contextConfigLocation</param-name>        <param-value>            classpath:*-context.xml        </param-value>    </context-param>    <listener>        <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>    </listener>    <filter>        <filter-name>characterEncodingFilter</filter-name>        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>        <init-param>            <param-name>encoding</param-name>            <param-value>UTF-8</param-value>        </init-param>        <init-param>            <param-name>forceEncoding</param-name>            <param-value>true</param-value>        </init-param>    </filter>    <filter-mapping>        <filter-name>characterEncodingFilter</filter-name>        <url-pattern>/*</url-pattern>    </filter-mapping>    <servlet>        <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>        <init-param>            <param-name>contextConfigLocation</param-name>            <param-value>                classpath:web-context.xml            </param-value>        </init-param>        <load-on-startup>1</load-on-startup>    </servlet>    <servlet-mapping>        <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>        <url-pattern>/</url-pattern>    </servlet-mapping></web-app>

spring-context为空,后面会加入数据库

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"       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"></beans>

部分注解说明

@Autowired  自动注入[根据类型注入 可以对成员变量、方法以及构造函数进行注释@Qualifier  标注对象是成员变量、方法入参、构造函数入参。@Resource   自动注入[根据名称注入],可写参数name=""  @Controller 表示控制器  @Service    表示业务处理层[一般在serviceImpl]  @Repository 表示持久层[一般在daoImpl]  @Component  当你的类不清楚是哪一层的时候使用该注解  @ResponseBody  异步返回数据类型为json  @RequestMapping  路径,请求类型等设置  @InitBinder   数据绑定