zoukankan      html  css  js  c++  java
  • springMVC helloworld入门

    一、SpringMVC概述与基本原理       

            spring Web MVC是一种基于Java的实现了Web MVC设计模式的请求驱动类型的轻量级Web框架,即使用了MVC架构模式的思想,将web层进行职责解耦,基于请求驱动指的就是使用请求-响应模型,框架的目的就是帮助我们简化开发,Spring Web MVC也是要简化我们日常Web开发的。

    另外还有一种基于组件的、事件驱动的Web框架在此就不介绍了,如Tapestry、JSF等。

              Spring Web MVC也是服务到工作者模式的实现,但进行可优化。前端控制器是DispatcherServlet;应用控制器其实拆为处理器映射器(Handler Mapping)进行处理器管理和视图解析器(View Resolver)进行视图管理;页面控制器/动作/处理器为Controller接口(仅包含ModelAndView handleRequest(request, response) 方法)的实现(也可以是任何的POJO类);支持本地化(Locale)解析、主题(Theme)解析及文件上传等;提供了非常灵活的数据验证、格式化和数据绑定机制;提供了强大的约定大于配置(惯例优先原则)的契约式编程支持。

            在Spring的Web MVC框架提供了模型 - 视图 - 控制器架构以及可用于开发灵活,松散耦合的Web应用程序准备的组件。 MVC模式会导致分离的应用程序(输入逻辑,业务逻辑和UI逻辑)的不同方面,同时提供这些元素之间的松耦合。

    • 模型(Model )封装了应用程序的数据和一般他们会组成的POJO。

    • 视图(View)是负责呈现模型数据和一般它生成的HTML输出,客户端的浏览器能够解释。

    • 控制器(Controller )负责处理用户的请求,并建立适当的模型,并把它传递给视图渲染。

    1.1、DispatcherServlet

    Spring的web模型 - 视图 - 控制器(MVC)框架是围绕着处理所有的HTTP请求和响应的DispatcherServlet的设计。 Spring的Web MVC框架的DispatcherServlet的请求处理流程说明如下图:

     

    下面是对应于传入的HTTP请求到DispatcherServlet的事件序列:

    1. 接收HTTP请求后,DispatcherServlet 咨询 HandlerMapping 来调用相应的控制器。

    2. 该控制器接受请求并调用基于使用GET或POST方法相应的服务方法。服务方法将基于定义的业务逻辑设置模型数据,并返回视图名到DispatcherServlet。

    3. DispatcherServlet将需要帮助的ViewResolver从拾取到该请求所定义的视图。

    4. 一旦视图被敲定,DispatcherServlet会传递模型数据是在浏览器上最终呈现的视图。

    所有上述部件,即HandlerMapping,控制器和视图解析器WebApplicationContext 部分是纯的 ApplicationContext 必要的 Web应用程序的一些额外的功能扩展。

    1.2、所需配置

    你需要映射所需的DispatcherServlet处理,通过在web.xml文件中使用URL映射请求。下面是一个例子,说明声明和映射 HelloWeb DispatcherServlet 的例子:

    [html] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. <?xml version="1.0" encoding="UTF-8"?>  
    2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    3.     xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"  
    4.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"  
    5.     id="WebApp_ID" version="3.0">  
    6.     <!-- SpringMVC的前端控制器 -->  
    7.     <servlet>  
    8.         <servlet-name>Hello</servlet-name>  
    9.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
    10.         <load-on-startup>1</load-on-startup>  
    11.     </servlet>  
    12.     <!-- Spring MVC配置文件结束 -->  
    13.   
    14.     <!-- 拦截设置 -->  
    15.     <servlet-mapping>  
    16.         <servlet-name>Hello</servlet-name>  
    17.         <!-- 由SpringMVC拦截所有请求 -->  
    18.         <url-pattern>/</url-pattern>  
    19.     </servlet-mapping>  
    20.       
    21. </web-app>  

            web.xml 文件将被保存在您的Web应用程序的 WebContent/ WEB-INF 目录。 在 DispatcherServlet 的 HelloWeb 初始化,该框架将尝试从一个名为 [servlet-name]-servlet.xml位于应用程序 WebContent/WEB-INF 目录文件加载应用程序上下文。在这种情况下我们的文件将是HelloWeb-servlet.xml。

              接下来,<servlet-mapping>标记指示URL会被DispatcherServlet处理。这里全部用。jsp结束HTTP请求将由DispatcherServlet的HelloWeb处理。如果不想用默认文件名为[servlet-name]-servlet.xml和默认位置的WebContent/WEB-INF 在 web.xml 文件中定义该文件的名称和位置如下:  

    [html] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. <?xml version="1.0" encoding="UTF-8"?>  
    2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    3.     xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"  
    4.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"  
    5.     id="WebApp_ID" version="3.0">  
    6.     <!-- SpringMVC的前端控制器 -->  
    7.     <servlet>  
    8.         <servlet-name>Hello</servlet-name>  
    9.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
    10.         <!-- 设置自己定义的控制器xml文件 -->  
    11.         <init-param>  
    12.             <param-name>contextConfigLocation</param-name>  
    13.             <param-value>/WEB-INF/Hello-servlet.xml</param-value>  
    14.         </init-param>  
    15.         <load-on-startup>1</load-on-startup>  
    16.     </servlet>  
    17.     <!-- Spring MVC配置文件结束 -->  
    18.   
    19.     <!-- 拦截设置 -->  
    20.     <servlet-mapping>  
    21.         <servlet-name>Hello</servlet-name>  
    22.         <!-- 由SpringMVC拦截所有请求 -->  
    23.         <url-pattern>/</url-pattern>  
    24.     </servlet-mapping>  
    25.       
    26. </web-app>  


    现在,让我们检查所需配置的HelloWeb-servlet.xml文件,放置在 Web应用程序的WebContent/WEB-INF目录:

    下面是有关 Hello-servlet.xml 文件的要点:

    [html] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. <beans xmlns="http://www.springframework.org/schema/beans"  
    2.     xmlns:context="http://www.springframework.org/schema/context"  
    3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"  
    4.     xsi:schemaLocation="    
    5.         http://www.springframework.org/schema/mvc   
    6.         http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd  
    7.         http://www.springframework.org/schema/beans         
    8.         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd    
    9.         http://www.springframework.org/schema/context     
    10.         http://www.springframework.org/schema/context/spring-context-3.0.xsd">  
    11.     <!-- 把标记了@Controller注解的类转换为bean -->  
    12.     <context:component-scan base-package="com.mucfc" />  
    13.     <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->  
    14.     <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />  
    15.     <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->  
    16.     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"  
    17.         p:prefix="/WEB-INF/views/" p:suffix=".jsp"/>  
    18.   
    19. </beans>  
    •  [servlet-name]-servlet.xml 文件将被用于创建定义的Bean,会覆盖在全局范围里名字相同的Bean的定义。

    • <context:component-scan...>标签将使用启动Spring MVC的注解扫描功能,允许做出像 @Controller和使用@RequestMapping注解等使用。

    • 使用InternalResourceViewResolver将有定义来解析视图名的规则。按照上述定义的规则,命名为你好的逻辑视图被委托给一个视图实现位于/WEB-INF/jsp/hello.jsp。

    1.3、定义控制器

    DispatcherServlet 委托请求发送到控制器,以执行特定于它的功能。注解@Controller表示一个特定的类提供一个控制器的角色。注解@RequestMapping 用于将URL映射到任何一个类或者一个特定的处理方法。

    [java] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. package com.mucfc;  
    2. import org.springframework.stereotype.Controller;    
    3. import org.springframework.ui.ModelMap;    
    4. import org.springframework.web.bind.annotation.RequestMapping;    
    5. import org.springframework.web.bind.annotation.RequestMethod;    
    6. @Controller    
    7. public class HelloWorldController {    
    8.     @RequestMapping(value="/hello")    
    9.     public String printWelcome(ModelMap model) {    
    10.         model.addAttribute("message", "Spring 3 MVC Hello World");    
    11.         return "hello";    
    12.     }    
    13. }  

    注解@Controller类定义为一个Spring MVC控制器。在这里,使用@RequestMapping第一次使用表明,该控制器上的所有处理方法是相对于/hello 路径。下一步标注使用@RequestMapping(方法= RequestMethod.GET)用于声明printHello() 方法作为控制器的默认服务方法来处理HTTP GET请求。可以定义另一种方法来处理同一URL的POST请求。

    可以写在上面的另一种形式,可以使用@RequestMapping添加额外的属性如下控制:

    [java] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. package com.mucfc;  
    2.   
    3. import org.springframework.stereotype.Controller;    
    4. import org.springframework.ui.ModelMap;    
    5. import org.springframework.web.bind.annotation.RequestMapping;    
    6. import org.springframework.web.bind.annotation.RequestMethod;    
    7. @Controller    
    8. public class HelloWorldController {    
    9.     @RequestMapping(value="/hello",method=RequestMethod.GET,params="userid")    
    10.     public String printWelcome(ModelMap model,String userid) {    
    11.         model.addAttribute("message", "Spring 3 MVC Hello World");    
    12.         return "hello";    
    13.     }    
    14. }  

    value属性指示该处理方法所映射到的URL和method属性定义了服务的方法来处理HTTP GET请求。有以下重要点要注意关于上述定义的控制器:

    • 将定义所需的业务逻辑内部的服务方法。可以调用此方法在另一个方法按要求。

    • 基于定义的业务逻辑,将在此方法中创建一个模型。您可以设定器不同的模型的属性和这些属性将被视图访问提出的最终结果。此示例创建一个具有自己的属性“message”的模型。

    • 定义的服务方法可以返回一个字符串,其中包含要用于渲染模型视图的名称。这个例子返回“hello”作为逻辑视图名。

    1.4、创建JSP视图

    Spring MVC支持多种类型的视图不同的演示技术。这些措施包括:JSP,HTML,PDF,Excel,XML,Velocity,XSLT,JSON,Atom和RSS订阅,JasperReports等,但最常用JSTL JSP模板。因此,我们编写一个/WEB-INF/hello/hello.jsp简单的hello 视图:

    在这里, ${message}是我们已经建立了控制器内部的属性。你可以在视图中显示多个属性。

    [html] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. <%@ page language="java" contentType="text/html; charset=gb2312"  
    2.     pageEncoding="gb2312"%>  
    3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
    4. <html>  
    5. <head>  
    6. <meta http-equiv="Content-Type" content="text/html; charset=gb2312">  
    7. <title>Insert title here</title>  
    8. </head>  
    9. <body>  
    10. Message:${message}  
    11. </body>  
    12. </html>  



    二、第一个SpringMVC程序

    工程免费下载

    1、在ecliose中新建立一个web工程,首先将需要的包导入到WebContentWEB-INFlib

    2、先配置web.xml

     

    [html] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. <?xml version="1.0" encoding="UTF-8"?>  
    2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    3.     xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"  
    4.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"  
    5.     id="WebApp_ID" version="3.0">  
    6.     <!-- SpringMVC的前端控制器 -->  
    7.     <servlet>  
    8.         <servlet-name>Hello</servlet-name>  
    9.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
    10.         <!-- 设置自己定义的控制器xml文件 -->  
    11.         <init-param>  
    12.             <param-name>contextConfigLocation</param-name>  
    13.             <param-value>/WEB-INF/Hello-servlet.xml</param-value>  
    14.         </init-param>  
    15.         <load-on-startup>1</load-on-startup>  
    16.     </servlet>  
    17.     <!-- Spring MVC配置文件结束 -->  
    18.   
    19.     <!-- 拦截设置 -->  
    20.     <servlet-mapping>  
    21.         <servlet-name>Hello</servlet-name>  
    22.         <!-- 由SpringMVC拦截所有请求 -->  
    23.         <url-pattern>/</url-pattern>  
    24.     </servlet-mapping>  
    25.       
    26. </web-app>  


    3、配置Hello-servlet.xml:

     

    [html] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. <beans xmlns="http://www.springframework.org/schema/beans"  
    2.     xmlns:context="http://www.springframework.org/schema/context"  
    3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"  
    4.     xsi:schemaLocation="    
    5.         http://www.springframework.org/schema/mvc   
    6.         http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd  
    7.         http://www.springframework.org/schema/beans         
    8.         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd    
    9.         http://www.springframework.org/schema/context     
    10.         http://www.springframework.org/schema/context/spring-context-3.0.xsd">  
    11.     <!-- 把标记了@Controller注解的类转换为bean -->  
    12.     <context:component-scan base-package="com.mucfc" />  
    13.     <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->  
    14.     <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />  
    15.     <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->  
    16.     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"  
    17.         p:prefix="/WEB-INF/views/" p:suffix=".jsp"/>  
    18.   
    19. </beans>  


    4、配置控制器:

     

    [java] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. package com.mucfc;  
    2.   
    3. import org.springframework.stereotype.Controller;    
    4. import org.springframework.ui.ModelMap;    
    5. import org.springframework.web.bind.annotation.RequestMapping;    
    6. import org.springframework.web.bind.annotation.RequestMethod;    
    7. @Controller    
    8. public class HelloWorldController {    
    9.     @RequestMapping(value="/hello",method=RequestMethod.GET,params="userid")    
    10.     public String printWelcome(ModelMap model,String userid) {    
    11.         model.addAttribute("message", "Spring 3 MVC Hello World");    
    12.         return "hello";    
    13.     }    
    14. }  


    5、WEB-INF下新建一个views文件夹,然后再新建一个hello.jsp

     

    [html] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. <%@ page language="java" contentType="text/html; charset=gb2312"  
    2.     pageEncoding="gb2312"%>  
    3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
    4. <html>  
    5. <head>  
    6. <meta http-equiv="Content-Type" content="text/html; charset=gb2312">  
    7. <title>Insert title here</title>  
    8. </head>  
    9. <body>  
    10. Message:${message}  
    11. </body>  
    12. </html>  


    6、整个工程目录如下:

    7、直接运行,浏览器中输入http://localhost:8080/SpringMVCLearningChapter1/hello

    结果如下:

    工程免费下载

  • 相关阅读:
    二叉树 Java 实现 前序遍历 中序遍历 后序遍历 层级遍历 获取叶节点 宽度 ,高度,队列实现二叉树遍历 求二叉树的最大距离
    Java:JUnit4使用详解
    Java 常用数据结构对象的实现原理 集合类 List Set Map 哪些线程安全 (美团面试题目)
    JAVA里的布尔运算符-甲骨文面试题
    try catch finally 用法
    Java的JDBC事务详解
    Java开发中JDBC连接数据库代码和步骤
    WebUploader文件图片上传插件的使用
    webuploader解决不能重复上传问题及一些常见问题处理
    HTML5 input file控件使用accept过滤限制的文件类型以及在谷歌下打开很慢的问题
  • 原文地址:https://www.cnblogs.com/liangxiaofeng/p/6377802.html
Copyright © 2011-2022 走看看