用spring mvc替换Struts2.0

真的是很久没写web应用了,所以对于web应用的那些个框架的动向也没有关注。以至到最近需要写一个web应用来完成个需求才把这块儿又拾起来。我用的还是之前那一套东西Tomcat+hibernate+struts2.0+jsp毕竟比较熟悉嘛。可是应用发布到server端之后收到告警才又想起来struts当初被爆出来有安全漏洞的问题。而且现在公司是禁止使用该框架了。所以要不然将struts去掉直接使用servlet或者换成spring mvc。考虑到应用之后可能还需要扩展而且还能接触学习个新东西所以就用spring mvc来替代struts吧

1. 引如spring的相关jar包

 2. 在web.xml中添加spring的相关配置去掉struts的相关配置

 

3. 在src目录下创建一个与step2里面红框部分的文件名相同的文件,删除struts.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:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="
  http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans.xsd
  http://www.springframework.org/schema/util
  http://www.springframework.org/schema/util/spring-util.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">

	<!-- 默认的注解映射的支持 -->
	<mvc:annotation-driven />

	<!-- 静态资源映射 -->
	<mvc:resources mapping="/js/**" location="/WEB-INF/js/" />
	<mvc:resources mapping="/css/**" location="/WEB-INF/css/" />
	<mvc:resources mapping="/fonts/**" location="/WEB-INF/fonts/" />
	<mvc:resources mapping="/plugins/**" location="/WEB-INF/plugins/" />
	<mvc:resources mapping="images/**" location="/WEB-INF/images/" />


	<!-- 当上面要访问的静态资源不包括在上面的配置中时,则根据此配置来访问 -->
	<mvc:default-servlet-handler />

	<!-- 开启controller注解支持 -->
	<!-- use-default-filters="false" 只扫描指定的注解 -->
	<context:component-scan base-package="com.tencent.sms.test.controller"
		use-default-filters="false">
		<context:include-filter type="annotation"
			expression="org.springframework.stereotype.Controller" />
	</context:component-scan>

	<!-- 视图解析器 -->
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="viewClass"
			value="org.springframework.web.servlet.view.JstlView" />
		<property name="contentType" value="text/html" />
		<property name="prefix" value="/WEB-INF/jsp/" />
		<property name="suffix" value=".jsp" />
	</bean>

	<!-- 有文件上传的请求需要添加这个 -->
	<bean id="multipartResolver"
		class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<property name="maxUploadSize" value="102400000"></property>
	</bean>

</beans>

4. 将之前strus的请求改为spring方式的。

    * 首先先shuo'yi'xstrus与spring请求写法的区别    

       a. struts的每一个请求都是一个action都是一个单独的类,spring是通过一个叫做controller的类和@RequestMapping来识别请求的,一个controller类里面可以包含所有的请求

       b. struts 从前端页面获得值是通过action类里面属性以及属性的set/get方法来完成的,spring是通过请求函数的同名参数来完成

       c. struts向页面返回值:

           ServletActionContext.getRequest().setAttribute("readyState", 4);

           struts向页面返回值:

           return new ModelAndView("taskconfirm", "filePath", filePath);

        d. 页面中发送请求

            struts: XXXXActiong.do 或者XXXXActiong.action,例如

<form action="fileUpload.action" method="post"
		enctype="multipart/form-data">
		<input type="file" name="file"><br> 
		<input type="submit" value="submit">
	</form>

            spring: /XXXXX,例如

<form action="uploadDBFile" method="post"
		enctype="multipart/form-data">
		<input type="file" name="file"><br> 
		<input type="submit" value="submit">
	</form>

   * 将struts请求修改为spring请求

     1. 创建一个controller类,并添加@Controller注释

     2. 创建controller类方法,每一个方法代表一个请求,方法上添加注释@RequestMapping(value = "/uploadDBFile", method = RequestMethod.POST)

     3. 将action类里面的excute方法中方法体内容都移过来按照上面的说的区别修改一下值获取和值返回即可

@Controller
public class SmsCheckController {

	/**
	 * 上传DB文件
	 * 
	 * @param req
	 * @return
	 * @throws Exception
	 */
	// value指定的是请求的名称,method是指定请求方式GET/POST这个请求方式必须和页面是对应的否则会出现405异常
	@RequestMapping(value = "/uploadDBFile", method = RequestMethod.POST)
	public ModelAndView uploadDBFile(HttpServletRequest req) throws Exception {
		MultipartHttpServletRequest mreq = (MultipartHttpServletRequest) req;
		MultipartFile file = mreq.getFile("file");
		String fileName = file.getOriginalFilename();

		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		String filePath = req.getSession().getServletContext().getRealPath("/") + "upload/" + sdf.format(new Date())
				+ fileName.substring(fileName.lastIndexOf('.'));
		System.out.println("-----------------filePath = " + filePath);
		FileOutputStream fos = new FileOutputStream(filePath);
		fos.write(file.getBytes());
		fos.flush();
		fos.close();

		return new ModelAndView("taskconfirm", "filePath", filePath);
	}
}

5. 将页面的action请求都修改为spring格式的请求

    这里面要注意controller的method里面指定请求方式GET/POST这个请求方式必须和页面是对应的否则会出现405异常


        如上只是简单的将我目前的小工具用spring来替代,spring mvc还有很多东西需要深挖的


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章