真的是很久沒寫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還有很多東西需要深挖的