zoukankan      html  css  js  c++  java
  • springMVC工作机制和框架搭建配置说明

     先说一下springMVC的工作机制

    1.springmvc把 所有的请求都提交给DispatcherServlet,它会委托应用系统的其他模块负责对请求进行真正的处理工作。

    2.DispatcherServlet查 询一个或多个HandlerMapping,找到处理请求的Controller.

    3.DispatcherServlet把 请求提交到目标Controller

    4.Controller进 行业务逻辑处理后,会返回一个ModelAndView

    5.Dispathcher查 询一个或多个ViewResolver视图解析器,找到ModelAndView对 象指定的视图对象

    6.视 图对象负责渲染返回给客户端。

    <?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:mvc="http://www.springframework.org/schema/mvc"
    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
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">


    <!--设置访问名  就是访问对应的名称的时候就是用项目名+/test/helloworld (这里针对的是实现Controller 接口覆写handleRequest方法的类)-->
    <bean name="/test/helloworld" class="controller.HelloworldController"></bean>
    <bean name="/test/my" class="controller.Mycontroller"></bean>


    <!--指定配置方法解析器 bean="ParamMethodResolver" () -->
    <bean name="/test/multi" class="controller.MultiController">
    <property name="methodNameResolver">
    <ref bean="ParamMethodResolver"></ref>
    </property>
    </bean>

    <!-- 多配置:访问控制器多个方法j解析配置 method请求的方法名称-->
    <bean name="ParamMethodResolver" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
    <property name="paramName" value="method"></property>
    </bean>

    <!-- 视图解析器  在对应的类中 return new ModelAndView("/multi","method",name); 会跳转到对应multi.jsp文件下,没有在WebRoot下的需加上包名--> 
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/"></property>
    <property name="suffix" value=".jsp"></property>
    </bean> 

    <!-- MVC标签 表示 静态资源 访问不经过springMVC框架 -->
    <mvc:resources location="/img/" mapping="/img/**"/>

    springMVC 3.0以后引入注解方式配置简化了很多(上面的配置都可以不用了)

    <!-- 注解扫描包 com.controller这个包下面的包使用注解有效 -->
    <context:component-scan base-package="com.controller"></context:comp1onent-scan>
    <!-- 开启注解驱动 -->
    <mvc:annotation-driven/>

    <!-- 视图解析器 -->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/"></property>
    <property name="suffix" value=".jsp"></property>
    </bean>


    </beans>

    上面spring3.0以前配置实例对应的类
    public class Mycontroller implements Controller {

    public ModelAndView handleRequest(HttpServletRequest request,
    HttpServletResponse response) throws Exception {
    System.out.println("------------hello world--------");
    String name="屌丝";
    Map<String,Object> map=new HashMap();
    map.put("map1", "haha");
    map.put("map2", "xixi");
    map.put("map3", "heihei");

    //相当于request中保存的name属性
    return new ModelAndView("/welcome","map",map);
    }

    }

    //一个controller控制器中写多个方法
    public class MultiController extends MultiActionController {
    //method1
    public ModelAndView add(HttpServletRequest request,
    HttpServletResponse response) throws Exception {
    System.out.println("------MultiController add----------");
    String name="add";
    return new ModelAndView("/multi","method",name);
    }

    //method2
    public ModelAndView update(HttpServletRequest request,
    HttpServletResponse response) throws Exception {
    System.out.println("------MultiController update----------");
    String name="update";
    return new ModelAndView("/multi","method",name);
    }
    }

    上面spring3.0以后配置实例对应的类

    @Controller
    @RequestMapping("/test")//访问的总包(根目录)
    public class Mycontller4 extends MultiActionController {

    @RequestMapping("/youHua")//访问方法名
    public ModelAndView youHua(){
    System.out.println("Mycontller4——youHua。。优化注解");
    String mulit="Mycontller4——youHua。。优化注解";
    return new ModelAndView("/annotation","mulit",mulit);
    }
    /*method=RequestMethod.GET
    * 只接收get提交方法的请求方试,不写这一句那么就不区分提交方式
    */
    @RequestMapping("/tiao",method=RequestMethod.GET)
    public String tiao(HttpServletRequest request){
    System.out.println("Mycontller4——tiao。。优化注解");
    String mulit="Mycontller4——youHua。。优化注解";
    request.setAttribute("mulit", mulit);
    return "/annotation";
    }

    web.xml文件中的配置


    <!-- 这个是spring MVC的入口 一个servlet
    spring-webmvc-3.2.0.RELEASE.jar 包下第一个
    -->
    <servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!--
    配置dispatcher的加载路劲
    classpath*表示加载config包下面的所有文件
    (如果这个配置文件放在lib文件下,springmvc启动后默认会自己去加载 ,下面的这一段就可以不用写了)
    <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:config/dispatcher-servlet.xml</param-value>
    </init-param>-->
    <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
    </servlet-mapping>

  • 相关阅读:
    [CF1299B] Aerodynamic
    [CF1338B] Edge Weight Assignment
    [CF689C] Mike and Chocolate Thieves
    [CF729C] Road to Cinema
    [CF735C] Tennis Championship
    [CF766C] Mahmoud and a Message
    [CF797C] Minimal string
    [CF798C] Mike and gcd problem
    [CF818D] Multicolored Cars
    《剑指Offer》面试题55:字符流中第一个不重复的字符
  • 原文地址:https://www.cnblogs.com/laotan/p/3637127.html
Copyright © 2011-2022 走看看