• 初识springmvc_01


    1.DispatcherServlet:前端控制器,用于接收请求。(即spring mvc是基于servlet实现的所以要注意多线程问题)

    2.HandlerMapping接口:DefaultAnnotationHandlerMapping :HandlerMapping接口的实现, 用于处理请求的映射。即前端请求找到对应的controller

    3.HandlerAdapter接口:AnnotationMethodHandlerAdapter: HandlerAdapter接口的实现,用于处理请求的映射。根据前端请求找到controller的方法

    4.ViewResolver接口:InternalResourceViewResolver : ViewResolver接口的实现,用于把ModelAndView的逻辑视图名解析为具体的View。

    前端请求过来,通过DefaultAnnotationHandlerMapping(注解) 找到对应的Controller,然后通过AnnotationMethodHandlerAdapter找到处理请求的方法,方法处理完后,通过InternalResourceViewResolver返回到哪个视图;

    配置文件:

    <?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:p="http://www.springframework.org/schema/p"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:util="http://www.springframework.org/schema/util" 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/util
      http://www.springframework.org/schema/util/spring-util.xsd
      http://www.springframework.org/schema/context 
      http://www.springframework.org/schema/context/spring-context.xsd
      http://www.springframework.org/schema/mvc
      http://www.springframework.org/schema/mvc/spring-mvc.xsd">
      
      
      
          <!-- 匹配请求和Controller有两种写法 -->
      
      
          <!-- 第一种请求直接对应Controller类(默认使用) -->
        <!-- BeanNameUrlHandlerMapping -->
        <!--
        <bean id="/hello" class="com.controller.HelloController"></bean>
        <bean id="/hello_2" class="com.controller.HelloController_2"></bean>
         -->
    
    
        <!-- 第二种Controller类标注自己的id,然后通过HandlerMapping集体对应 -->
        <!-- SimpleUrlHandlerMapping -->
        <!--
        <bean id="helloController" class="com.controller.HelloController"></bean>
        <bean id="helloController_2" class="com.controller.HelloController_2"></bean>
        <bean id="simpleUrlHandlerMapping"
            class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
            <property name="mappings">
                 <props>
                         <prop key="/helloTest_2">helloController_2</prop>
                         <prop key="/helloTest">helloController</prop>
                 </props>
            </property>
        </bean>
        -->
        
        <!-- 一个Controller写多个方法 -->
        <bean id="paramMethodResolver" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
            <property name="paramName" value="action"></property>    
        </bean>
         <bean name="/multi" class="com.controller.MultiController">
            <property name="methodNameResolver">
                <ref bean="paramMethodResolver"/>
            </property>
        </bean>
        
        
    
        <!-- 注解映射方式 -->
        <mvc:annotation-driven />
        <!-- 如果当前请求为“/”时,则转发到“/helloworld/index” -->
        <mvc:view-controller path="/" view-name="forward:/annotationController/helloworld" />
        <!-- 开启controller注解支持 -->
        <!-- use-default-filters="false" 只扫描指定的注解 -->
        <context:component-scan base-package="com.controller"
            use-default-filters="false">
            <context:include-filter type="annotation"
                expression="org.springframework.stereotype.Controller" />
        </context:component-scan>
    
        
        <!-- 视图解析器 -->
        <bean
            class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="viewClass"
                value="org.springframework.web.servlet.view.JstlView" />
            <property name="contentType" value="text/html" />
            <property name="prefix" value="/WEB-INF/views/" />
            <property name="suffix" value=".jsp" />
        </bean>
        <!-- 支持上传文件 -->
        <bean id="multipartResolver"
            class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
            <!-- 设置上传文件的最大尺寸为1MB -->
            <property name="maxUploadSize">
                <value>1048576</value>
            </property>
            <property name="defaultEncoding">
                <value>UTF-8</value>
            </property>
        </bean>
    </beans>

    Controller实现:

    package com.controller;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.springframework.web.servlet.ModelAndView;
    import org.springframework.web.servlet.mvc.AbstractController;
    
    
    /**
     *    一个类一个Controller,一个请求
     */
    public class HelloController extends AbstractController{
    
        @Override
        protected ModelAndView handleRequestInternal(HttpServletRequest arg0,
                HttpServletResponse arg1) throws Exception {
            
            System.out.println("............HelloController.................");
            
            return new ModelAndView("index");
        }
    
    }
    package com.controller;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.springframework.web.servlet.ModelAndView;
    import org.springframework.web.servlet.mvc.AbstractController;
    
    
    public class HelloController_2 extends AbstractController{
    
        @Override
        protected ModelAndView handleRequestInternal(HttpServletRequest arg0,
                HttpServletResponse arg1) throws Exception {
            
            
            System.out.println("............HelloController_2.................");
            
            return new ModelAndView("index");
        }
    
    }

    一个类一个Controller,可以对应多个请求:

    package com.controller;
    
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
    
    
    /**
     *    一个类一个Controller,可以对应多个请求
     *在一个Controller类中定义多个方法,并根据使用者的请求来执行当中的某个方法,
     *相当于Struts的DispatcherAction。
     *使用MultiActionController需要配个MethodNameResolver实例,
     *默认使用ParameterMethodNameResolver,他会根据所给的网址中,
     *最后请求的文件名称来判断执行Controller中的哪一个方法
     *
     *MultiActionController 也实现了AbstractController类,实现了handleRequestInternal方法
     *    @Override
        protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)
                throws Exception {
            try {
                //通过请求找到方法名
                String methodName = this.methodNameResolver.getHandlerMethodName(request);
                //然后调用具体的方法(注意****参数:request,response********)
                return invokeNamedMethod(methodName, request, response);
            }
            catch (NoSuchRequestHandlingMethodException ex) {
                return handleNoSuchRequestHandlingMethod(ex, request, response);
            }
        }
     */
    public class MultiController extends MultiActionController{
        
        
        public String add(HttpServletRequest request,
                HttpServletResponse response){
            System.out.println("-----------add------------");
            return "index";
        }
        
        public String delete(HttpServletRequest request,
                HttpServletResponse response){
            System.out.println("------------delete--------------");
            return "index";
        }
        
        public String update(HttpServletRequest request,
                HttpServletResponse response){
            System.out.println("---------------update---------------");
            return "index";
        }    
    }

    基于注解实现Controller:

    package com.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.servlet.ModelAndView;
    
    @Controller
    @RequestMapping(value = "annotationController")
    public class AnnotationController {
    
        @RequestMapping(value = "/helloworld", method = { RequestMethod.GET })
        protected ModelAndView helloworld() {
            System.out.println("............HelloController_3.................");
            return new ModelAndView("index");
        }
    
    }

    WebContent/views/下的视图:

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
        hello word!!!
    </body>
    </html>
    想的都是好
  • 相关阅读:
    深度学习之视频人脸识别系列二:人脸检测与对齐
    终极指南:构建用于检测汽车损坏的Mask R-CNN模型(附Python演练)
    数据挖掘和机器学习的面试问题
    SVM多核学习方法简介
    RookeyFrame 线上 添加Model
    RookeyFrame Bug 线上创建的DLL被删除了 模块无法删除 临时解决
    RookeyFrame Bug 编号显示 系统自动生成 的问题,有时候依旧会显示text文本框
    C# 跨域 请求带cookie
    RookeyFrame 隐藏 首次加载菜单 的伸缩动画
    RookeyFrame 加载 自定义JS
  • 原文地址:https://www.cnblogs.com/freezone/p/5175568.html
Copyright © 2020-2023  润新知