Spring mvc框架tutorial

2 篇文章 0 订阅

Spring mvc框架tutorial

翻译:MoringStar
spring web mvc框架提供了一个model-view-controller框架,在开发web应用的时候可以更加的灵活和松耦合。MVC被分隔成了不同的应用程序切面,在这些元素中提供松散的组合。

  • model包含了应用程序的数据,一般情况下由POJO简单java类组成.
  • view是数据的路由响应,一般由一html构成
  • controller响应用户请求,生成相关的model,同时路由到相关的页面

DispatcherServlet

spring mvc框架围绕着dispatcherServlet设计,dispatcherServlet处理了所有的http请求和响应,spring mvc的dispatcherServlet程序工作流如下面的图表:

Created with Raphaël 2.1.0 DispatcherServlet HTTP request HTTP request DispatcherServlet DispatcherServlet HandlerMapping HandlerMapping Contoller Contoller viewResoler viewResoler view view request controller name request view name view name view init model data html

下面一连串事件为dispatcherServlet处理Http请求的情况:

  • 在接收到一个http request,dispatcherServlet通过HandlerMapping找到对应的controller
  • Controller得到http request调用以Get,Post方法为基础的web服务方法,web服务方法会设置model的数据在基础的申明上,并且返回一个试图给dispatcherservlet
  • disoatcherServlet通过视图解析找到对应的view
  • 当view被释放,dispatcherservlet通过把model的数据给view,最后路由到浏览器.

handlerMapping.,controller,viewResolver是applicationContext的web插件扩展webApplicationContext的一部分.

必须的配置

通过在web.xml文件配置url mapping配置DispatcherServlet去处理request请求,下面是一个简单的DispatcherServlet例子:

<web-app id="WebApp_ID" version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <display-name>Spring MVC Application</display-name>

   <servlet>
      <servlet-name>HelloWeb</servlet-name>
      <servlet-class>
         org.springframework.web.servlet.DispatcherServlet
      </servlet-class>
      <load-on-startup>1</load-on-startup>
   </servlet>

   <servlet-mapping>
      <servlet-name>HelloWeb</servlet-name>
      <url-pattern>*.jsp</url-pattern>
   </servlet-mapping>

</web-app>

web.xml文件保存在web工程/WEB-INF目录下,通过上看的配置初始化了HelloWeb dispatcherServlet,这个框架会是这去夹在application context文件(通常被命名为[servlet-name]-servlet.xml在web-inf目录下),z我们配置一个叫做HelloWeb-servlet.xml.

下一步,通过<Servlet-mapping>标签配置拦截的url信息,下面配置会拦截所有jsp请求.

如果你不想使用默认的文件名字和目录,可以单独配置路径在web.xml里面,配置如下:

<web-app...>

<!-------- DispatcherServlet definition goes here----->
....
<context-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>/WEB-INF/HelloWeb-servlet.xml</param-value>
</context-param>

<listener>
   <listener-class>
      org.springframework.web.context.ContextLoaderListener
   </listener-class>
</listener>
</web-app>

现在,我们来配置基础的helloWeb-servlet.xml文件,在web-inf目录下:

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   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">

   <context:component-scan base-package="com.tutorialspoint" />

   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix" value="/WEB-INF/jsp/" />
      <property name="suffix" value=".jsp" />
   </bean>

</beans>

下面是配置文件的关键地方
- [servlet-name]-servlet.xml文件被用来创建bean的声明,会覆盖global scope下的任何bean的声明
- <context:com...-scan...>标签配置springmvc的注解扫描,允许使用香@controller和@requestMapping的注解
- internalResourceViewResolver将声明视图解析请这里解析所有在/web-inf/jsp下文件后缀为jsp的文件.

controller

DispatcherServlet转发request给控制器,去执行特殊的函数方法。@Controller注解表示这个特殊的类是控制器。@RequestMapping注解标识了 url 映射。

@Controller
@RequestMapping("/hello")
public class HelloController{

   @RequestMapping(method = RequestMethod.GET)
   public String printHello(ModelMap model) {
      model.addAttribute("message", "Hello Spring MVC Framework!");
      return "hello";
   }

}

@Controller注解声明了一个Spring mvc 控制器,@RequesrMapping表示了控制器方法拦截的 /hello路径。@RequestMapping(method=RequestMethod.GET)标识这个pringHtllo()方法是这么controller默认的执行HttpGet请求的方法。也可以声明一个同样的post请求方法在相同的url上.

@Controller
public class HelloController{

   @RequestMapping(value = "/hello", method = RequestMethod.GET)
   public String printHello(ModelMap model) {
      model.addAttribute("message", "Hello Spring MVC Framework!");
      return "hello";
   }

}

springMvc支持许多不同的视图如(jsp,html,pdf,excel worksheets,xml,Velocity tenplates,xslt,json,aton,rss feeds,jasperreports…)

http://www.tutorialspoint.com/spring/spring_web_mvc_framework.htm

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
This is a tutorial on Spring MVC, a module in the Spring Framework for rapidly developing web applications. The MVC in Spring MVC stands for Model-View-Controller, a design pattern widely used in Graphical User Interface (GUI) development. This pattern is not only common in web development, but is also used in desktop technology like Java Swing. Sometimes called Spring Web MVC, Spring MVC is one of the most popular web frameworks today and a most sought-after skill. This book is for anyone wishing to learn to develop Java-based web applications with Spring MVC. Sample applications come as Spring Tool Suite and Eclipse projects. Table of Contents Introduction Chapter 1: The Spring Framework Chapter 2: Model 2 and the MVC Pattern Chapter 3: Introduction to Spring MVC Chapter 4: Annotation-Based Controllers Chapter 5: Data Binding and the Form Tag Library Chapter 6: Converters and Formatters Chapter 7: Validators Chapter 8: The Expression Language Chapter 9: JSTL Chapter 10: Internationalization Chapter 11: File Upload Chapter 12: File Download Chapter 13: Testing Your Application Appendix A: Tomcat Appendix B: Using Spring Tool Suite with Maven Appendix C: The Servlet API Appendix D: JavaServer Pages Appendix E: Deployment Index Table of Contents Chapter 1: The Spring Framework Chapter 2: Model 2 and the MVC Pattern Chapter 3: Introduction to Spring MVC Chapter 4: Annotation-Based Controllers Chapter 5: Data Binding and the Form Tag Library Chapter 6: Converters and Formatters Chapter 7: Validators Chapter 8: The Expression Language Chapter 9: JSTL Chapter 10: Internationalization Chapter 11: File Upload Chapter 12: File Download Chapter 13: Testing Your Application Appendix A: Tomcat Appendix B: Using Spring Tool Suite with Maven Appendix C: The Servlet API Appendix D: JavaServer Pages Appendix E: Deployment

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值