Spring MVC流程解析

Spring MVC 简介


       Spring MVC是Spring的后续产品,也可以说是Spring的子集,其主要体现在视图层的MVC。本文对Spring MVC做一个较为详细的说明,后续会比较其优缺点。其中最为明显的一点为:其入口为一个Servlet不同于Struts2为一个Filter,这样在速度上有明显的优势,但缺点就是会导致多线程问题。

     

       M为其Model对象:


       底层为一个Map,因此可以接受任意类型的vo对象。 

     

       V为视图模板,支持:


       1、Jsp,采用EL表达式的方式,直接获取model中对象,减少了业务代码的侵入。


       2、Velocity和FreeMarker,两种常见的模板引擎技术,有一套语法规则,其详细使用请见如下地址:

                  

                  http://velocity.apache.org/(velocity)


                   http://freemarker.org/(FreeMarker)


       3、 XSLT,xml文档格式转换工具,通过定义xslt样式,可以将xml转换成需要的样式。


       4、 DocumentView视图(PDF/Excel)


       5、 JasperReports(报表工具)


       6、 XML


       7、 JSON


       8、 Feed Views(rss订阅),解释请参见:
http://wenku.baidu.com/view/add76818964bcf84b9d57b9e.html

 

       C为控制器

 

       用户可以自定义Pojo类,通过controller注解声明为控制器。下面会有详细的Demo使用说明。

 

Spring MVC 流程架构

 

        Spring MVC 控制器


       包括前端控制器和后端控制器,前端控制器是所有请求的入口,为一个servlet。后端控制器是用户自定义的控制器,采用注解或者xml配置的方式来声明,本文采用注解方式。负责接收请求,分发请求,调用model和视图解析器渲染视图模板,返回用户视图。


        Spring MVC Model对象


        model为一个接口,封装vo对象。其包括两个实现类,如下图:


 

       modelMap为一个继承了LinkedMap的类,是真正存放vo的地方。

       ExtendModelMap从字面上理解其继承了modelMap,区别与RedirectAttributes为其是forward到视图,而redirectAttributes为重定向到视图。
       Spring MVC ModelAndView对象ModelAndView对象,是代表了MVC Web程序中Model与View的对象,不过它只是方便您一次返回这两个对象的holder,Model与View两者仍是分离的概念。详见如下地址:http://itroop.iteye.com/blog/263845

 

Spring MVC Demo


     所需Jar包


 

      SpringMVC配置文件


 

<?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:mvc="http://www.springframework.org/schema/mvc"     
    xsi:schemaLocation="      
           http://www.springframework.org/schema/beans      
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd      
           http://www.springframework.org/schema/context      
           http://www.springframework.org/schema/context/spring-context-3.0.xsd     
           http://www.springframework.org/schema/mvc      
           http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
    
    <!-- 注解探测器 -->
    <context:component-scan base-package="snippet"/>
    
    <mvc:annotation-driven/>
    
    
    <mvc:resources location="/WEB-INF/js/" mapping="/js/**" />
    <mvc:resources location="/img/" mapping="/img/**" />
    
    <!--  annotation默认的方法映射适配器 -->
    <!-- bean id="handlerMapping" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
    <bean id="handlerAdapter" class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /-->
    
    <!-- 视图解释类 -->  
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
        <property name="prefix" value="/"/>  
        <property name="suffix" value=".jsp"/>
    </bean> 
</beans>


       自定义控制器

package snippet;

import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
	

@Controller
public class HelloWorldController {
	
	@RequestMapping("/add")
	public String addUser() {
		return "add";//返回到add.jsp视图
	} 
	
	
	//定义url请求的映射名称为updateUser
	@RequestMapping("/updateUser")
	public String updateUser(User user, Model model) {
		
		
		//接受表单数据
		System.out.println(
				"收集表单数据,用户名为:"+user.getUserName()+",密码为:"+user.getPwd());
		
		
		//查看是否支持封装为List
		List list = user.getStrArray();
		for(int i=0; i<list.size(); i++){
			System.out.println("页面传递List值为:"+list.get(i));
		}
		
		//封装视图显示数据,封装到model中
		model.addAttribute(user);
		
		
		//复杂类型对象应用的user和简单类型string
		Student s = new Student();
		s.setUser(user);
		model.addAttribute(s);
		
		//返回到updateUser.jsp
		return "updateUser";
	}

}

 

        JSP视图

 

        添加视图

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form action="updateUser" method="post">
		用户名:<input type="text" name="userName"><br>
		u密码:<input type="password" name="pwd"><br>
		L内容:<input type="text" name="strArray"><br>
		L内容:<input type="text" name="strArray">
		<br>
		<input type="submit" value="添加">
	</form>
</body>
</html>

       

       添加成功视图

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>成功页面</title>
</head>
<body>
	更新用户成功。<br></br>
	用户名为:${user.userName}<br>
	密码为:${user.pwd}<br>
	内部引用对象名称为:${student.user.userName}
	<br>
</body>
</html>

 

对于demo中代码如果不清楚,可以查看注释说明。对于demo大家可以在这里下载:

http://download.csdn.net/detail/lb85858585/5128005


总结

 

         通过上面的描述,我们清楚了spring mvc的原理,以及spring mvc涉及到的控制器定义,requestmapping定义,Mode定义,ModelAdnView定义。

 

         通过demo我们可以看到spring mvc比较有意思的是,当将vo对象设置到Map中时可以不指定键值,原因是其会默认将类名称的小写作为键值,这也就是可以在jsp视图中直接使用user.username和student.user.userName的原因。如果有所怀疑(源码如此),可以亲自尝试,比如将User对象改为User1,那么在页面显示的时候只要使用user1.username就可以获取到其内容。这点很人性化,它充分考虑了开发人员定义参数的习惯(命名规范),就这一点就减少了代码量。

 

       另外,spring mvc提供的注解方式,大大减少了开发过程的配置文件。

 

      当然要想发挥springmvc的更大作用,还需要结合spring的其它功能(控制反转,依赖注入),比如集成hibernate,ibatis等等持久层框架

 

      对于spring mvc和其他mvc框架的对比还在研究中,敬请大家期待。。。

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值