1、引入所需jar包;
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
</dependency>
2、增加springmvc-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:p="http://www.springframework.org/schema/p"
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/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-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">
<!-- 启用注解 -->
<mvc:annotation-driven/>
<!-- 设置使用注解的类所在的包 必须设置-->
<context:component-scan base-package="com.joyintech.batch.job.impl"></context:component-scan>
<!-- 对转向页面的路径解析。prefix:前缀, suffix:后缀 必须设置 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/" p:suffix=".do" />
<!-- 配置MultipartResolver,用于上传文件,使用spring的CommonsMultipartResolver -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 设置上传文件大小,单位byte -->
<property name="maxUploadSize" value="5000000"/>
<!-- 设置文件名称编码 -->
<property name="defaultEncoding" value="UTF-8"/>
</bean>
<!-- 对静态资源文件的访问-->
<!--<mvc:resources location="/css/" mapping="/css/**" cache-period="31556926"/>
<mvc:resources location="/images/" mapping="/images/**" cache-period="31556926"/>
<mvc:resources location="/js/" mapping="/js/**" cache-period="31556926"/>-->
</beans>
注意:springmvc-servlet.xml文件存放路径,放置不对容易报错
3、修改web.xml配置springmvc-servlet.xml
<!-- spring 支持batch接口 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/springmvc-servlet.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>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
尤其注意param-value的值区分classpath:与现在的路径区别。
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/springmvc-servlet.xml</param-value>
</context-param>
4、java类改造
注意:原有逻辑的返回值是void 需要加上
@ResponseBody
5、后期优化:
原有的代码结构中生成日志逻辑在批处理基类中处理,优化时需要使用切面编程处理