javascript
Spring系列之一 Spring MVC
最近在看Spring的書,之前一直跟著項目做,雖然項目用到了Spring的很多功能,但是我很少有機會在工作的項目中去配置Spring.感覺只有理論是不夠的,雖然只是用Spring的配置讓人感覺很low,但是這是在工作中必須的.由于這些配置經常會忘記,所以寫下這么low的博客去記錄如何使用和配置.當然捎帶一些原理.我盡量有時間就更新.
一.SpringMVC 發展
MVC我的理解就是游覽器請求時候,由服務器端實現的一種模式,游覽器對此都是不知道的.MVC的全稱就是Model ?View Controller. ?Model是指與數據庫交互的數據模型,View是向用戶展示的顯示層,Controller就是負責轉發請求返回相對應的頁面或者或者直接就是數據給請求方(ajax或者文件下載等) ?,從而實現了數據,業務邏輯和顯示層的隔離.舉個例子:某地區每一年的人口?增長比例作為model既可以用柱狀圖來顯示也可以用折線圖來顯示,其中數據是不變的,而顯示層是變化的.柱狀圖既可以顯示某地區每一年的人口增長比例,也可以顯示某地區每一年的GDP增長,View是不變的,而Model是改變的.? ? MVC之前是用在桌面應用程序,后來流行在web開發上面,我只知道Struts就是比較流行的一款MVC框架.具體的流程如下圖所示.
搭建Spring MVC
????????2.1 ?具體需要哪些Jar包就去maven ?repository找吧,這里就不詳細說了.第一步是攔截請求交給Spring的DispatcherServlet去做Mapping.
<servlet><servlet-name>springMVC</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:config/spring-mvc.xml</param-value></init-param></servlet><servlet-mapping><servlet-name>springMVC</servlet-name><url-pattern>/</url-pattern></servlet-mapping>????????2.2 配置Spring-MVC的ResourceViewResolver,當controller返回視圖的時候,由視圖解析器去結合model和view生成html.
????????2.3 靜態資源的控制,由于在web.xml中對所有的請求進行了攔截,這樣js和css,image這樣的靜態資源就會被當成頁面去處理,從而找不到對應的靜態資源.
????
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:aop="http://www.springframework.org/schema/aop"xmlns:mvc="http://www.springframework.org/schema/mvc"?xmlns:tx="http://www.springframework.org/schema/tx"xmlns:util="http://www.springframework.org/schema/util"?xmlns:context="http://www.springframework.org/schema/context"xmlns:cache="http://www.springframework.org/schema/cache"?xmlns:jdbc="http://www.springframework.org/schema/jdbc"xsi:schemaLocation="http://www.springframework.org/schema/beans? ????http://www.springframework.org/schema/beans/spring-beans-3.2.xsd ????http://www.springframework.org/schema/aop? ????http://www.springframework.org/schema/aop/spring-aop-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/tx? ????http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ????http://www.springframework.org/schema/util? ????http://www.springframework.org/schema/util/spring-util-3.2.xsd ????http://www.springframework.org/schema/context? ????http://www.springframework.org/schema/context/spring-context-3.2.xsd ????http://www.springframework.org/schema/jdbc ????http://www.springframework.org/schema/context/spring-jdbc-3.2.xsd ????http://www.springframework.org/schema/cache? ????http://www.springframework.org/schema/context/spring-cache-3.2.xsd"> <mvc:annotation-driven?/><import?resource="classpath:config/spring.xml"?/><beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver"><!--掃描路徑內所有以jsp結尾的文件?并把優先級設為第二高--><property?name="prefix"?value="/WEB-INF/page/"></property><property?name="suffix"?value=".jsp"></property><!--?jsp?priority?is?second?--><property?name="order"?value="2"?/></bean><bean?id="viewResolver"class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver"><property?name="viewClass"value="org.springframework.web.servlet.view.freemarker.FreeMarkerView"?/><!--掃描路徑內所有以ftl結尾的文件?并把優先級設為最高--><property?name="cache"?value="false"?/><property?name="suffix"?value=".ftl"?/><property?name="contentType"?value="text/html;?charset=UTF-8"?/><!--?freemarker?template?priority?is?first?--><property?name="order"?value="1"?/></bean><bean?id="freemarkerConfig"class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"><property?name="templateLoaderPath"?value="/WEB-INF/page/"?/><property?name="defaultEncoding"?value="UTF-8"?/></bean><!--?靜態資源訪問?--><mvc:resources?location="/img/"?mapping="/img/**"?/><mvc:resources?location="/js/"?mapping="/js/**"?/><mvc:resources?location="/css/"?mapping="/css/**"?/></beans>
????
?????2.4關于Controller返回由model填充的view
public?class?LoginController?{private?UserService?userService;public?String?showLoginPage(Model?model)?{model.addAttribute("welcome","welcome?to?spring?mvc");return?"login";}public?Response<User>?loginCheck(String?userName,?String?password,?Model?model)?{Response<User>?response?=?new?Response();User?user?=?this.userService.loginCheck(userName,?password);ArrayList<User>?list?=?null;if?(user?!=?null)?{list?=?new?ArrayList();list.add(user);response.setData(list);response.setSuccess(true);}?else?{response.setErrorMessage("密碼錯誤!");}return?response;} }? ? 這里說下一些Spring MVC的一些注解:?@Controller??標注這個class是Controller可以被spring掃到然后由spring去創捷然后放到自己的工廠.@RequestMapping表示所匹配的url,在class上的注解表示這個路徑是下面處理url路徑的前段.@ResponseBody是表示返回一個字符串而非頁面一般用于ajax請求.關于Model其實就是存放數據的,在view可以顯示出來,然后輸出html給客戶端.
from:https://my.oschina.net/u/2250599/blog/389854
總結
以上是生活随笔為你收集整理的Spring系列之一 Spring MVC的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java 内存模型及GC原理
- 下一篇: Spring MVC拦截器+注解方式实现