springMVC - 01

Spring Web FrameWork(MVC)

搭建环境

引入jar包

包结构如下所示

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

springMVC - 01_第1张图片
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.

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

springMVC - 01_第2张图片
clipboard.png
springMVC - 01_第3张图片
clipboard.png

WEB-INF包下创建Spring Bean Definition file

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

点击next 选择beans 选择4.3版本

选择mvc 选择context

如下图所示:

springMVC - 01_第4张图片
clipboard.png
新建完成后关闭 选择Spring Config Editor 打开

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



  SpringMVC
  
   
        Test
        org.springframework.web.servlet.DispatcherServlet
        
        1
    

    
        Test
        
        /
    
  
  
    index.html
  

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



    
    
    
    
    
    
    
    
    
    
    
    
    
        
        
        
           
         
        
       
    

建立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(Mapmap,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)