您的位置:首页 > 编程语言 > Java开发

一个简单的Spring MVC的例子

2015-06-24 09:17 471 查看

一、Spring MVC的优点

1、Spring3 MVC的学习难度小于Struts2,Struts2用不上的多余功能太多。呵呵,当然这不是决定因素。

2、Spring3 MVC很容易就可以写出性能优秀的程序,Struts2要处处小心才可以写出性能优秀的程序(指MVC部分)

3、Spring3 MVC的灵活是你无法想像的,Spring的扩展性有口皆碑,Spring3 MVC当然也不会落后,不会因使用了MVC框架而感到有任何的限制。

二、简单的例子

1、配置web.xml

指除了Control层外的其它Bean的Spring配置文件,定义DispatcherServlet。这里是把spring与spring mvc的功能随着服务器启动而启动。

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <!-- 除了Control层外的其它Bean的Spring容器设置,这个与SSH整合的时候一样 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- 配置DispatcherServlet -->
<!-- 名字为mvc,那么我们在WEB-INF中需要一个名为mvc-servlet.xml的spring mvc配置文件来对Control层的Bean、相关页面以及Spring mvc提供的一些工具Bean进行管理 -->
<servlet>
<servlet-name>mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 接收页面以.abc结尾的请求 -->
<servlet-mapping>
<servlet-name>mvc</servlet-name>
<url-pattern>*.abc</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>


2、编写处理请求的Controller(处理器)

package cn.framelife.mvc.control;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import cn.framelife.mvc.entity.User;

/**
* @Controller 通过Spring的自动扫描功能,把一个POJO转化为处理请求的控制器
*通过@RequestMapping标注,可以将特定的url和具体的controller类或controller类中的方法绑定。
* @RequestMapping("/user") 处理来自/user的请求,一般来说是用于区分不同模块的
*/
@Controller
@RequestMapping("/user")
public class UserControl {

/**
* 处理/user的请求,请求的方法为POST
* 参数的User对象是把页面的表单值放进一个User对象中
* ModelAndView 是返回一个View。在这里我们是一个JSP页面。
*/
@RequestMapping(method=RequestMethod.POST)
public ModelAndView createUser(User user){
System.out.println(user.getUsername()+"-"+user.getPassword());

//ModelAndView.setViewName("/success") 根据mvc的配置文件,可知返回的是/user/success.jsp
ModelAndView view = new ModelAndView();
view.setViewName("/success");

//把一个user对象放到ModelAndView中
user.setUsername("li");
view.addObject(user);
return view;
}
}


3、编写视图文件

我们在WebRoot/uesr目录下有一个add.jsp及一个success.jsp页面。add.jsp是用以表单输入。而success.jsp是Controller处理完后返回的页面。

add.jsp:

<body>
<form action="user.abc" method="post">
用户名:<input type="text" name="username"><br/>
密 码:<input type="text" name="password"><br/>
<input type="submit">
</form>
</body>
success.jsp:
<body>
success!!${user.username}.
</body>


4、配置Spring MVC的配置文件,使控制器、视图解析器等生效

mvc-servlet.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> <!-- 扫描控制器 -->
<context:component-scan base-package="cn.framelife.mvc.control"></context:component-scan>

<!-- 视图名称解析器 -->
<!--
Controller中:ModelAndView.setViewName("/success")
配置的是/user/success.jsp
-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/user"
p:suffix=".jsp"></bean>
</beans>


5、配置其它的Bean

诸如service层、dao层等Bean在applicationContext.xml中配置,如我们之前做的工作一样。

http://blog.csdn.net/jrainbow/article/details/10526617

三、Spring MVC处理一个请求的过程

我们这里以6.2的例子来说:


1、 DispatcherServlet接受来自浏览器的user/add.abc请求

2、 DispatcherServlet使用DefaultAnnotationHandlerMapping查找负责处理这个请求的Controller

3、 DispatcherServlet将这个请求交由2中查找到的UserController

4、 Controller完成业务处理后,返回ModelAndView模型对象,其中它的视图名为”/success”,而且这个模型对象这包含一个键为user的User对象

5、 DispatcherServlet调用InternalResourceViewResolver对ModelAndView中视图名进行解析,得到/user/success.jsp这个视图

6、 DispatcherServlet把User对象中的属性渲染到/user/success.jsp中

7、 DispatcherServlet把/user/success.jsp这个视图返回给浏览器。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息