Spring系列之一 Spring MVC

最近在看Spring的書,以前一直跟着項目作,雖然項目用到了Spring的不少功能,可是我不多有機會在工做的項目中去配置Spring.感受只有理論是不夠的,雖然只是用Spring的配置讓人感受很low,可是這是在工做中必須的.因爲這些配置常常會忘記,因此寫下這麼low的博客去記錄如何使用和配置.固然捎帶一些原理.我儘可能有時間就更新.css

一.SpringMVC 發展html

  1. MVC個人理解就是遊覽器請求時候,由服務器端實現的一種模式,遊覽器對此都是不知道的.MVC的全稱就是Model  View Controller.  Model是指與數據庫交互的數據模型,View是向用戶展現的顯示層,Controller就是負責轉發請求返回相對應的頁面或者或者直接就是數據給請求方(ajax或者文件下載等)  ,從而實現了數據,業務邏輯和顯示層的隔離.舉個例子:某地區每年的人口 增加比例做爲model既能夠用柱狀圖來顯示也能夠用折線圖來顯示,其中數據是不變的,而顯示層是變化的.柱狀圖既能夠顯示某地區每年的人口增加比例,也能夠顯示某地區每年的GDP增加,View是不變的,而Model是改變的.    MVC以前是用在桌面應用程序,後來流行在web開發上面,我只知道Struts就是比較流行的一款MVC框架.具體的流程以下圖所示.java


  2. 搭建Spring MVCweb

        2.1  具體須要哪些Jar包就去maven  repository找吧,這裏就不詳細說了.第一步是攔截請求交給Spring的DispatcherServlet去作Mapping.ajax

<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.spring

        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" />
	<bean
		class="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填充的viewspring-mvc

@Controller
@RequestMapping({ "/login" })
public class LoginController {
	@Resource
	private UserService userService;

	@RequestMapping({ "" })
	public String showLoginPage(Model model) {
	model.addAttribute("welcome","welcome to spring mvc");
		return "login";
	}
	
	@RequestMapping({ "/loginCheck" })
	@ResponseBody
	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給客戶端.服務器

相關文章
相關標籤/搜索