zoukankan      html  css  js  c++  java
  • springMVC_04controller四种配置总结

    一.通过url对应bean,加粗部分为必须有的

    <bean class=" org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>
        <!-- 配置handlerAdapter ,这个配置可以没有,但是配置请求和处理器时,name名称必须要规范 -->
        <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>
        <!-- 配置渲染器 -->
        <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
            <!-- 将视图名 渲染后视图的前缀 -->
            <property name="prefix" value="/WEB-INF/jsp/"/>
            <!-- 渲染后视图的后缀 -->
            <property name="suffix" value=".jsp"/>
            <!-- 例:视图名为:hello   渲染后:/WEB-INF/jsp/hello.jsp 该页面-->
        </bean>
        <!-- 配置请求和处理器 -->
        <bean name="/hello.do" class="com.ahd.controller.HelloController"></bean>

    二.为url分配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:p="http://www.springframework.org/schema/p"
        xmlns:context="http://www.springframework.org/schema/context"
        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">
        
        
        <!-- 配置渲染器 -->
        <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
            <!-- 将视图名 渲染后视图的前缀 -->
            <property name="prefix" value="/WEB-INF/jsp/"/>
            <!-- 渲染后视图的后缀 -->
            <property name="suffix" value=".jsp"/>
            <!-- 例:视图名为:hello   渲染后:/WEB-INF/jsp/hello.jsp 该页面-->
        </bean>
        <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
            <property name="mappings">
                <props>
                    <!-- key值为url,value为对应处理器的id -->
                    <prop key="/hello.do">HelloController</prop>
                </props>
            </property>
        </bean>
        <bean id="HelloController" class="com.ahd.controller.HelloController"></bean>

    三.为url匹配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:p="http://www.springframework.org/schema/p"
        xmlns:context="http://www.springframework.org/schema/context"
        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">
        
        
        <!-- 配置渲染器 -->
        <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
            <!-- 将视图名 渲染后视图的前缀 -->
            <property name="prefix" value="/WEB-INF/jsp/"/>
            <!-- 渲染后视图的后缀 -->
            <property name="suffix" value=".jsp"/>
            <!-- 例:视图名为:hello   渲染后:/WEB-INF/jsp/hello.jsp 该页面-->
        </bean>    
        <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"></bean>
        <!-- 请求为hello*.do,都将被处理 称之为匹配 -->
        <bean id="helloController" class="com.ahd.controller.HelloController"></bean>
    </beans>

    四.使用注解

    配置文件

    <?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"
        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">
            
        <!-- 配置渲染器 -->
        <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
            <!-- 将视图名 渲染后视图的前缀 -->
            <property name="prefix" value="/WEB-INF/jsp/"/>
            <!-- 渲染后视图的后缀 -->
            <property name="suffix" value=".jsp"/>
            <!-- 例:视图名为:hello   渲染后:/WEB-INF/jsp/hello.jsp 该页面-->
        </bean>
    <!—配置扫描指定包的注解--!>
        <context:component-scan base-package="com.ahd.controller"></context:component-scan>
        
    </beans>

    控制器类HelloController

    package com.ahd.controller;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.servlet.ModelAndView;
    
    @Controller
    public class HelloController {
        @RequestMapping("hello")
        public ModelAndView hello(HttpServletRequest req,HttpServletResponse resp){
            ModelAndView mv=new ModelAndView();
            
            mv.addObject("msg", "hello,welcome to use annotation");
            mv.setViewName("hello");
            return mv;
        }
    }
  • 相关阅读:
    Google-C++编码规范中文版.pdf
    100个gdb小技巧(v1.0).pdf
    NSIS 3.0 发布,Windows 安装程序制作工具
    python爬取各类文档方法归类汇总
    【转】openwrt中ubus
    OpenWrt源码分析之ubus
    详解C语言中的fopen()函数和fdopen()函数
    IPsec技术介绍(转)
    mxml 详解
    Delphi IDE Theme Editor, Delphi IDE 主题编辑器,支持D7~Rad Studio 10.3 RIO及Lazarus
  • 原文地址:https://www.cnblogs.com/aihuadung/p/10107678.html
Copyright © 2011-2022 走看看