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给客户端.服务器