SpringMVC 注解方式开发
1、要保证项目编码为UTF-8,否则中文乱码。JDK1.6以上版本编译
2、引入jar包
spring-aop-3.2.2.jar 面向切片编程
spring-aspects-3.2.2.jar 提供对AspectJ的支持,以便可以方便的将面向方面的功能集成进IDE中
spring-beans-3.2.2.jar 核心。访问配置文件、创建和管理bean 以及进行IoC/DI操作相关的所有类。
spring-context-3.2.2.jar为Spring 核心提供了大量扩展。
spring-context-support-3.2.2.jar
spring-core-3.2.2.jar Spring 框架基本的核心工具类。外部依赖Commons Logging 。
spring-expression-3.2.2.jar 配置对象的注入,它便是SpEL (Spring Expression Language)
spring-web-3.2.2.jar Web 应用开发时,用到Spring 框架时所需的核心类
spring-webmvc-3.2.2.jar Spring MVC 框架相关的所有类。包括框架的Servlets,Web MVC框架,控制器和视图支持。
注意spring3.0的包名是 org.spingframework.web.servlet-3.1.0 RELEASE.jar
com.springsource.org.apache.commons.logging-1.1.1.jar 日志
com.springsource.org.aopalliance-1.0.0.jar AOP联盟的API包,里面包含了针对面向切面的接口。 <pre name="code" class="java">3、创建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">
<!--加载和监听spring 这里 已经将Spring和SpringMVC拆分 -->
<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>
<!-- 配置springmvc分发器servlet -->
<servlet>
<servlet-name>action</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>action</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
</web-app>
4、创建spring-mvc.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd ">
<!-- 自动扫描 springmvc中的包-->
<span style="white-space:pre"> </span><context:component-scan base-package="cn.itcast.springmvc.web.controller"/>
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span><!-- 注解驱动 -->
<span style="white-space:pre"> </span><mvc:annotation-driven/>
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span><!-- 资源管理 springmvc的机制,通过这样注册才能对这个目录下的所有文件进行访问,**代表一级目录及其子目录-->
<span style="white-space:pre"> </span><mvc:resources location="/resources/" mapping="/resources/**"/>
<span style="white-space:pre"> </span><mvc:resources location="/upload/" mapping="/upload/**"/>
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span><!-- 上传文件解析器 -->
<span style="white-space:pre"> </span><bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<span style="white-space:pre"> </span><property name="maxUploadSize" value="10485670"/> <!-- 10M -->
<span style="white-space:pre"> </span></bean>
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span><!-- 内部资源视图解析器 prefix + logicName + suffix /WEB-INF/jsps/ + index + .jsp-->
<span style="white-space:pre"> </span><bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<span style="white-space:pre"> </span><!-- 前缀 -->
<span style="white-space:pre"> </span><property name="prefix" value="/WEB-INF/jsps/"/>
<span style="white-space:pre"> </span><!-- 后缀 -->
<span style="white-space:pre"> </span><property name="suffix" value=".jsp"/>
<span style="white-space:pre"> </span></bean>
<span style="white-space:pre"> </span>
</beans>
5、创建beans.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd ">
<!-- 自动扫描 spring中的包 -->
<context:component-scan base-package="cn.itcast.springmvc.service"/>
</beans>