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;
        }
    }
  • 相关阅读:
    [leetcode-102-Binary Tree Level Order Traversal]
    c# 常规验证基类
    不错的silverlight教程
    js 遮罩层请稍后
    登陆冻结账户思路
    mvc json 日期问题的最简单解决方法
    EF 常见语句以及sql语句简单 后续继续添加
    Entity Framework edmx(mapping文件)
    asp.net服务器推送长连接
    数据字典根据组别再分类
  • 原文地址:https://www.cnblogs.com/aihuadung/p/10107678.html
Copyright © 2011-2022 走看看