已存项目改造spring mvc

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、后期优化:

原有的代码结构中生成日志逻辑在批处理基类中处理,优化时需要使用切面编程处理

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值