• Spring MVC基本概念


    MVC工作原理:

    模型-视图-控制器(MVC)是一个以设计界面应用程序为基础的设计模式。它主要通过分离模型、视图及控制器在应用程序中的角色将业务逻辑从界面中解耦。
    通常,模型负责封装应用程序数据在视图层展示。视图仅仅只是展示这些数据,不包含任何业务逻辑。控制器负责接收来自用户的请求,并调用后台服务(manager或者dao)来处理业务逻辑。
    处理后,后台业务层可能会返回了一些数据在视图层展示。控制器收集这些数据及准备模型在视图层展示。MVC模式的核心思想是将业务逻辑从界面中分离出来,允许它们单独改变而不会相互影响

    SpringMVC的流程:

    1、 用户发送请求至前端控制器DispatcherServlet。

    2、 DispatcherServlet收到请求调用HandlerMapping处理器映射器。

    3、 处理器映射器找到具体的处理器(可以根据xml配置、注解进行查找),生成处理器对象及处理器拦截器(如果有则生成)一并返回给DispatcherServlet。

    4、 DispatcherServlet调用HandlerAdapter处理器适配器。

    5、 HandlerAdapter经过适配调用具体的处理器(Controller,也叫后端控制器)。

    6、 Controller执行完成返回ModelAndView。

    7、 HandlerAdapter将controller执行结果ModelAndView返回给DispatcherServlet。

    8、 DispatcherServlet将ModelAndView传给ViewReslover视图解析器。

    9、 ViewReslover解析后返回具体View。

    10、DispatcherServlet根据View进行渲染视图(即将模型数据填充至视图中)。

    11、 DispatcherServlet响应用户。

    需要的pom文件:

    <dependencies>
      	<dependency>
      		<groupId>org.springframework</groupId>
      		<artifactId>spring-webmvc</artifactId>
      		<version>3.2.4.RELEASE</version>
      	</dependency>
      	<dependency>
      		<groupId>javax.servlet</groupId>
      		<artifactId>servlet-api</artifactId>
      		<version>2.4</version>
      	</dependency>
      	<dependency>
          <groupId>javax.servlet</groupId>
          <artifactId>jstl</artifactId>
          <version>1.2</version>
        </dependency>
      </dependencies>
    

    application-context.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:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans   
           http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  
           http://www.springframework.org/schema/context   
           http://www.springframework.org/schema/context/spring-context-3.1.xsd">
    
      <context:component-scan base-package="com" />
      <context:annotation-config />
    </beans>

    在这个文件中webappWEB-INF下:web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="3.0" 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_3_0.xsd">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath:application-context.xml
        </param-value>
      </context-param>
      
    <filter>
        <filter-name>characterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
      </filter>
      <filter-mapping>
        <filter-name>characterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
      </filter-mapping>
    
      <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>
      <listener>
        <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
      </listener>
    
      <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>
          org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
      </servlet>
    
      <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>/*</url-pattern>
      </servlet-mapping>
      <display-name>Archetype Created Web Application</display-name>
      </web-app>
    

      spring-servlet.mxl

    <?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:context="http://www.springframework.org/schema/context"
        xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc"
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
           http://www.springframework.org/schema/beans/spring-beans.xsd 
           http://www.springframework.org/schema/context 
           http://www.springframework.org/schema/context/spring-context.xsd 
           http://www.springframework.org/schema/tx 
           http://www.springframework.org/schema/tx/spring-tx.xsd
              http://www.springframework.org/schema/mvc
           http://www.springframework.org/schema/mvc/spring-mvc.xsd">
           
    <context:component-scan base-package="com.*" />
      <mvc:annotation-driven />
    <!-- 
      <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>  
      <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>    
     -->
    
      <mvc:view-controller path="/" view-name="index"/>  
    
      <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="order" value="1" />
        <property name="prefix" value="/WEB-INF/view/" />
        <property name="suffix" value=".jsp" />
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
      </bean>
      
      </beans>
    

      测试:

    @Controller
    @RequestMapping("hello")
    public class HelloControl {
    	
    	@RequestMapping("test")
    	public void hello(HttpServletRequest request,HttpServletResponse response){
    		String name = request.getParameter("name");
    		response.setCharacterEncoding("utf-8");
    		response.setContentType("application/json");
    		try {
    			PrintWriter printwriter = response.getWriter();
    			printwriter.println("hello=="+name);
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    	}
    }
    

      不知道java为什么这么配置,就想asp.netMVC 直接弄好不就行了吗?

  • 相关阅读:
    [转]Xml Schema
    设计模式之Observer Pattern
    通过 C# 使用 J# 类库中的 Zip 类压缩文件
    An extender can't be in a different UpdatePanel than the control it extends
    关于AutoResetEvent和ManualResetEvent
    ref, out参数区别
    取整, 无条件进位, 无条件取整
    VB.NET语法基础
    XP防火墙,挡掉访问自己的IIS
    maybe useful for Add the solution to source control
  • 原文地址:https://www.cnblogs.com/sunliyuan/p/10719072.html
Copyright © 2020-2023  润新知