美文网首页
springMVC - 01

springMVC - 01

作者: 50ef8076d671 | 来源:发表于2017-07-16 20:15 被阅读0次

    Spring Web FrameWork(MVC)

    搭建环境

    引入jar包

    包结构如下所示

    7个jar包( springMVC的最小配置)

    clipboard.png

    帮助文档中有明确的提示:

    The Spring Web model-view-controller (MVC) framework is designed around aispatcherServlet that dispatches requests to handlers, with configurable handler mappings, view resolution, locale, time zone and theme resolution as well as support for uploading files. The default handler is based on the @Controller and @RequestMapping annotations, offering a wide range of flexible handling methods. With the introduction of Spring 3.0, the @Controller mechanism also allows you to create RESTful Web sites and applications, through the @PathVariable annotation and other features.

    Upon initialization of a DispatcherServlet,Spring MVC looks for a file named [servlet-name]-servlet.xml in the WEB-INF directory of your web application and creates the beans defined there, overriding the definitions of any beans defined with the same name in the global scope.

    整个实现过程 帮助文档也有说明:

    clipboard.png clipboard.png

    WEB-INF包下创建Spring Bean Definition file

    取名Test-servlet.xml
    (这里的取名必须要跟servlet的取名保持一致)

    点击next 选择beans 选择4.3版本

    选择mvc 选择context

    如下图所示:

    clipboard.png
    新建完成后关闭 选择Spring Config Editor 打开

    读取核心配置文件(Dispatcherservlet)
    在web.xml文件内进行如下配置:

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
      <display-name>SpringMVC</display-name>
      <!-- 配置SpringMVC环绕DispatcherServlet -->
       <servlet>
            <servlet-name>Test</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <!-- 启动服务的时候  就对该servlet进行初始化操作  值越小  实例化优先级越高 -->
            <load-on-startup>1</load-on-startup>
        </servlet>
    
        <servlet-mapping>
            <servlet-name>Test</servlet-name>
            <!-- 所有的请求都会被DispatcherServlet进行处理,甚至   / 也是请求 -->
            <url-pattern>/</url-pattern>
        </servlet-mapping>
      
      <welcome-file-list>
        <welcome-file>index.html</welcome-file>
      </welcome-file-list>
    </web-app>
    

    WEB-INF下创建Test-servlet文件(因为我在web.xml文件中写的是Test)
    注意:写好视图解析器后 demo文件内的访问就必须书写标识来进行请求转发和重定向了

    <?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:mvc="http://www.springframework.org/schema/mvc"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd"
    >
        <!-- 启动SpringMVC注解识别 -->
        <mvc:annotation-driven />
        <!-- 扫描哪个层的注解 -->
        <!-- 如果想对多层进行扫描 使用 , 分隔 -->
        <!-- 也可以使用通配符 如 "com.*.controller" 可以访问com下的controller -->
        <!-- 通配符访问只能适用于一层 如"com.shxt.model.controller" 不能通过上述语句进行访问 -->
        <!-- 使用 com.**.controller 使其可以访问com包下任意层的controller -->
        <context:component-scan base-package="com.shxt.controller" />
        <!-- jsp视图解析器 -->
        <!-- 通过解析器可以简化在controller中的代码    -->
        <!-- 首先创建spring提供的试图解析器对象 -->
        <!-- 等同于InternalResourceViewResolver   InternalResourceViewResolver = newInternalResourceViewResolver(); -->
        <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <!-- 添加前缀 "/WEB-INF/" -->
            <property name="prefix" value="/WEB-INF/"/>
            <!-- 添加后缀 ".jsp" -->
            <property name="suffix" value=".jsp"></property>   
            <property name="viewClass" value=" org.springframework.web.servlet.view.JstlView"/> 
            <!-- 提高有限度配置   可配可不配 -->
           <!-- <property name="order" value="10"/> -->
        </bean>
    </beans>
    

    建立demo文件进行测试和下一步配置:

    package com.shxt.controller;
    
    import java.util.Map;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.servlet.ModelAndView;
    import org.springframework.web.servlet.view.UrlBasedViewResolver;
    //  标注  告知其是一个控制器   相当于一个servlet
    @Controller
    public class Demo {
    
        //映射的路径   当访问"/wukong"时就可以找到testMethod()这个方法
        @RequestMapping(value="/Test")
        public ModelAndView TestMethod(Map<String ,Object>map,User user){
            /* 在此处传递的形参   会默认被进行实例化操作 */
            System.out.println(map+"  is  "+user);
            return null;
        }
    
        /**
         * controller To Jsp
         */
    
        /**
         *  默认情况下使用请求转发完成页面跳转
         * 存在四种写法
         * @return
         */
        @RequestMapping(value="/sys/RequesTest")
        public ModelAndView RequesTestMethod(){
            //第一种
            /*  ModelAndView mav = new ModelAndView();
                mav.setViewName("/WEB-INF/view/RequestTest.jsp");
             */
            //第二种
            /*  ModelAndView mav = new ModelAndView("/WEB-INF/view/RequestTest.jsp");
             */
            //第三种
            /*  ModelAndView mav = new ModelAndView("forward:/WEB-INF/view/RequestTest.jsp");
             */
            //第四种
            ModelAndView mav = new ModelAndView(UrlBasedViewResolver.FORWARD_URL_PREFIX+"/WEB-INF/view/RequesTest.jsp");
            return mav;
        }
    
        /**
         *  重定向到页面操作
         * 有两种操作
         * 注意重定向不能找到WEB-INF下的内容
         * @return
         */
        @RequestMapping(value="/sys/RedirectTest")
        public ModelAndView RedirectTestMethod(){
            //第一种
            /*  ModelAndView mav = new ModelAndView("redirect:/view/RedirectTest.jsp");
             */
            //第二种
            ModelAndView mav = new ModelAndView(UrlBasedViewResolver.REDIRECT_URL_PREFIX+"/view/RedirectTest.jsp");
            return mav;
        }
    
        /**
         * controller To controller By Request
         * 一定要注意路径问题
         * 注解@RequestMapping后面的字符串内容
         * 要在请求转发中写完整
         * 约定俗成的要求:
         * controller跳转到controller
         * 需要写明  forward:
         * 或者
         * UrlBasedViewResolver.FORWARD_URL_PREFIX
         * 提高代码阅读性
         */
        @RequestMapping(value="/CTC/Request_OneTest")
        public ModelAndView CTC_Request_TestMethod01(){
            ModelAndView mav = new ModelAndView("forward:/CTC/Request_view");
            return mav;
        }
    
        @RequestMapping(value="/CTC/Request_view")
        private ModelAndView CTC_Request_TesView(){
            ModelAndView mav = new ModelAndView("/WEB-INF/view/View.jsp");
            return mav;
        }
    
        @RequestMapping(value="/CTC/Request_TwoTest")
        private ModelAndView CTC_Request_TestMethod02(){
            ModelAndView mav = new ModelAndView("/CTC/view");
            return mav;
        }
    
    
    
        /**
         * controller To controller By Redirect
         */
        @RequestMapping(value="/CTC/Redirect_OneTest")
        public ModelAndView CTC_Redirect_TestMethod01(){
            ModelAndView mav = new ModelAndView("redirect:/CTC/Redirect_TwoTest");
            return mav;
        }
    
        @RequestMapping(value="/CTC/view")
        private ModelAndView CTC_Redirect_TesView(){
            ModelAndView mav = new ModelAndView("/view/View.jsp");
            return mav;
        }
    
        @RequestMapping(value="/CTC/Redirect_TwoTest")
        private ModelAndView CTC_Redirect_TestMethod02(){
            ModelAndView mav = new ModelAndView(UrlBasedViewResolver.REDIRECT_URL_PREFIX+"/CTC/view");
            return mav;
        }
        /**
         * 使用视图解析器后   就必须书写标识来进行请求转发和重定向了
         * UrlBasedViewResolver.FORWARD_URL_PREFIX或者forward:
         * UrlBasedViewResolver.REDIRECT_URL_PREFIX或者redirect:
         */
        @RequestMapping(value="/CTC/Request_OneTest")
        public ModelAndView CTC_Request_TestMethod03(){
            ModelAndView mav = new ModelAndView("forward:/CTC/Request_TwoTest");
            return mav;
        }
        @RequestMapping(value="/CTC/viewCopy")
        private ModelAndView CTC_Redirect_TesViewCopy(){
            ModelAndView mav = new ModelAndView("/view/View");
            return mav;
        }
        @RequestMapping(value="/CTC/Request_TwoTest")
        private ModelAndView CTC_Request_TestMethod04(){
            ModelAndView mav = new ModelAndView("/CTC/viewCopy");
            return mav;
        }
    }

    相关文章

      网友评论

          本文标题:springMVC - 01

          本文链接:https://www.haomeiwen.com/subject/pqrxkxtx.html