zoukankan      html  css  js  c++  java
  • Spring MVC 的xml一些配置

    1.可以自动加载注解驱动,通过注解找到对应Controller

        <!-- spring MVC 注解驱动 -->
        <mvc:annotation-driven></mvc:annotation-driven>
        <!-- 配置自动扫描包 -->
        <context:component-scan base-package="com.cc8w.Controller"></context:component-scan>
     
         <!-- 配置视图解析器 -->
        <bean  
            class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
            <property name="prefix" value="/WEB-INF/view/"></property>  
            <property name="suffix" value=".jsp"></property>  
        </bean> 
    package com.cc8w.Controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    @Controller
    @RequestMapping("/HelloWorld")
    public class HelloWorld {
    
        @RequestMapping("/hi.do")
        public String hi(String names,Model model)
        {
            System.out.println("hi.dodo");
            model.addAttribute("hi", "hi123456");
            return "hi";
        }
    }

    显示结果正确.

    2.配置HandlerMapping,根据beanName找到对应Controller

     web.xml配置

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
      <display-name>Spring-web02</display-name>
      <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
      </welcome-file-list>
      <!-- The front controller of this Spring Web application, responsible for handling all application requests -->
        <servlet>
            <servlet-name>springDispatcherServlet</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:applicationContext.xml</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>
    
        <!-- Map all requests to the DispatcherServlet for handling -->
        <servlet-mapping>
            <servlet-name>springDispatcherServlet</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
    </web-app>

    applicationContext.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:aop="http://www.springframework.org/schema/aop"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
            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-4.3.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
    
        <!-- 配置HandlerMapping 根据beanname找到对应Controller -->
         <bean class="org.springframework.web.servlet.mvc.support.ControllerBeanNameHandlerMapping"></bean>
         
         <!--配置beanname找到对应的Controller -->
         <bean name = "/dogcontrollerdog" class = "com.cc8w.Controller.Dog"></bean>
         
         <!-- 配置视图解析器 -->
        <bean  
            class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
            <property name="prefix" value="/WEB-INF/view/"></property>  
            <property name="suffix" value=".jsp"></property>  
        </bean> 
        
    
    </beans>

    测试类(这里一定要继承org.springframework.web.servlet.mvc.AbstractController接口)

    package com.cc8w.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 Dog extends AbstractController {
         public Dog()
         {
            System.out.println("13213"); 
         }
    
        @Override
        protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
            // TODO Auto-generated method stub
            System.out.println("456--访问到了---"); 
            
            ModelAndView mav = new ModelAndView("dog");
            mav.addObject("hi", "467 th");
            return mav;
        }
    }

    模版目录和结果

    3.配置HandlerMapping,根据URL找到对应Controller(主要是applicationContext.xml或<bean-name>-Servlet.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:aop="http://www.springframework.org/schema/aop"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
            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-4.3.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
    
    
        <!-- 配置HandlerMapping 根据简单URL找到对应Controller -->
         <bean class = "org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
             <property name="mappings">
                 <props>
                     <prop key="/dogcontroller">dog</prop>
                 </props>
             </property>
         </bean>
         <!--bean类 -->
         <bean id = "dog"  class = "com.cc8w.Controller.Dog"></bean>
     
         
         <!-- 配置视图解析器 -->
        <bean  
            class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
            <property name="prefix" value="/WEB-INF/view/"></property>  
            <property name="suffix" value=".jsp"></property>  
        </bean> 
        
    
    </beans>

    上面的类,这个spring MVC配置文件,可以找到控制器,就不截 结果了.

    4.配置HandlerMapping,通过控制器类名访问Controller,访问时类名首字母小写 小写

    <?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:aop="http://www.springframework.org/schema/aop"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
            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-4.3.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
    
    
        <!-- 配置HandlerMapping 通过控制器类名访问Controller,访问时类名首字母小写 -->
        <bean class = "org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"></bean>
         
         <!--bean类 -->
         <bean  class = "com.cc8w.Controller.Dog"></bean>
     
         
         <!-- 配置视图解析器 -->
        <bean  
            class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
            <property name="prefix" value="/WEB-INF/view/"></property>  
            <property name="suffix" value=".jsp"></property>  
        </bean> 
        
    
    </beans>

     http://localhost:8080/Spring-web02/dog 这样访问...

    注意, 还呀配置下日志.  

    https://www.cnblogs.com/fps2tao/p/12809867.html

  • 相关阅读:
    买房的贷款时间是否是越长越好?https://www.zhihu.com/question/20842791
    asp.net cookie and session
    leelazero and google colab
    download file by python in google colab
    physical processor, core, logical processor
    通过powershell操作eventlog
    openxml in sql server
    get the page name from url
    How to Execute Page_Load() in Page's Base Class?
    Difference between HttpContext.Request and Request
  • 原文地址:https://www.cnblogs.com/fps2tao/p/7274173.html
Copyright © 2011-2022 走看看