zoukankan      html  css  js  c++  java
  • Spring MVC的配置文件(XML)的几个经典案列

        1.既然是配置文件版的,那配置文件自然是必不可少,且应该会很复杂,那我们就以一个一个的来慢慢分析这些个经典案列吧!

       

            01.实现Controller

    /*
     * 控制器
     */
    public class MyController implements Controller{
    
        /*
         * 返回视图
         * (non-Javadoc)
         * @see org.springframework.web.servlet.mvc.Controller#handleRequest(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
         */
        public ModelAndView handleRequest(HttpServletRequest request,
                HttpServletResponse response) throws Exception {
            ModelAndView mv=new ModelAndView();
            mv.addObject("msg", "happy");
            mv.setViewName("jd");
            return mv;
        }
        
    }

           

    
        <!-- 外部资源 -->
        <!--<bean id="jd" class="org.springframework.web.servlet.view.RedirectView">
         <property name="url" value="http://www.jd.com"></property>
        </bean>-->
        
        <!-- 内部资源 -->
         <!--<bean id="internal" class="org.springframework.web.servlet.view.JstlView">
         <property name="url" value="/WEB-INFO/jsp/happy.jsp"></property>
        </bean>-->
        
        <!-- 引用外部资源 即将上面的外部与内部资源放到外面的配置文件中去-->
        <bean class="org.springframework.web.servlet.view.XmlViewResolver">
         <property name="location" value="classpath:MyView.xml"></property>
        </bean>

    <bean id="/hello.do" class="cn.happy.controller.MyController"></bean>

            外部配置文件

    <?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:aop="http://www.springframework.org/schema/aop"
        xmlns:mvc="http://www.springframework.org/schema/mvc" 
        xmlns:tx="http://www.springframework.org/schema/tx" 
         xsi:schemaLocation="
            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.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
             http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    
        <!-- 外部资源 -->
        <bean id="jd" class="org.springframework.web.servlet.view.RedirectView">
         <property name="url" value="http://www.jd.com"></property>
        </bean>
        
        <!-- 内部资源 -->
         <bean id="internal" class="org.springframework.web.servlet.view.JstlView">
         <property name="url" value="/WEB-INFO/jsp/happy.jsp"></property>
        </bean>
    
    </beans>

          02.处理 配置为"/"解决的三种方法

    <!-- 配置中央调度器 -->
       <servlet>
         <servlet-name>springmvc</servlet-name>
         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      
        <!--指定配置文件applicationContext的路径  -->
        <init-param>
         <param-name>contextConfigLocation</param-name>
         <param-value>classpath:applicationContext.xml</param-value>
        </init-param>
        
        <!--Tomcat启动的时候,Servlet对象已经到内存!!!! >0数字    0或者是负数,和你没有设置是一样的  -->
         <load-on-startup>1</load-on-startup>
       </servlet>
       
       <!-- 代替带调度器 -->
       <servlet-mapping>
         <servlet-name>springmvc</servlet-name>
         <url-pattern>/</url-pattern>
      </servlet-mapping>

         01.web.xml配置文件中书写

    <!--处理 配置为"/"解决的第一种 -->
      <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.JPG</url-pattern>
      </servlet-mapping>
      
      <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.png</url-pattern>
      </servlet-mapping>
      
      <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.css</url-pattern>
      </servlet-mapping>
      
      <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.js</url-pattern>
      </servlet-mapping>

            ApplicationContext.xml文件中

            <!-- 第二种 -->
           <!--<mvc:default-servlet-handler/>-->
          
          <!-- 第三种 -->
          <!--<mvc:resources location="/images/" mapping="/images/**"></mvc:resources>--> 
        

         03.继承AbstractController

          跟模版方法很相似,在handleRequest的方法基础上新添加自己的handleRequestInternal方法

    public class MyAbstract extends AbstractController{

    @Override
    protected ModelAndView handleRequestInternal(
    HttpServletRequest httpservletrequest,
    HttpServletResponse httpservletresponse) throws Exception {
    ModelAndView mv=new ModelAndView();
    mv.addObject("msg", "明天放假一天,不上课");
    //处理一道
    mv.setViewName("WEB-INF/jsp/happy.jsp");
    return mv;
    }

    }

     

       

          并且可以控制请求的方式、例如post或者get等

    <bean id="/hello.do" class="cn.happy.controller.MyAbstract">
          <property name="supportedMethods" value="POST,GET,"></property>
        </bean>

         04.继承MultiActionController(可以创建多个方法)

            

    public class MyMutilAction extends MultiActionController{
    
        public ModelAndView doFirst(HttpServletRequest request,
                HttpServletResponse response) throws Exception {
            
            ModelAndView mv=new ModelAndView();
            mv.addObject("msg", "我是doFrist");//reqeuset.setAttribute("key","value");
            mv.setViewName("WEB-INF/jsp/happy.jsp");
            return mv;
        }
        
        public ModelAndView doSecond(HttpServletRequest request,
                HttpServletResponse response) throws Exception {
            
            ModelAndView mv=new ModelAndView();
            mv.addObject("msg", "我是doSecond");
            mv.setViewName("WEB-INF/jsp/happy.jsp");
            return mv;
        }
    }

         可以自定义出外界访问的*.do

    <!--  适配器 -->
         <!--<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
                <property name="mappings">
                   <props>
                     <prop key="/hello.do">firstController</prop>
                   </props>
                </property>
             </bean>-->
             
         <!-- 配置处理器 -->
         <!--<bean id="firstController" class="cn.happy.controller.MyMutilAction">
              <property name="methodNameResolver" ref="nameResolver"></property>
         </bean>-->
         
         <!--<bean id="nameResolver" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
          <property name="paramName" value="actionName"></property>
         </bean>-->
  • 相关阅读:
    ubuntu下如何批量修改文件后缀名
    vanilla
    Ubuntu apt-get 彻底卸载软件包
    Kendall Rank(肯德尔等级)相关系数
    图像质量评估(IQA)
    conda常用命令
    在ubuntu中搜索文件或文件夹的方法
    libstdc++.so.6: version `GLIBCXX_3.4.21' not found
    迅雷磁力链接转BT种子工具
    springboot 集成mybatis plus3
  • 原文地址:https://www.cnblogs.com/bdpsc/p/6087170.html
Copyright © 2011-2022 走看看