处理器和映射器的配置

目录

1、处理器和映射器的配置

1.1、用户类:

1.2、用户列表页面:jsp文件

2、基于注解形式实现

2.1、组件的配置文件,scources目录下的配置文件

2.2、基于注解的Handler

3、基于非注解形式实现

3.1、在各组件的配置文件中,分析处理器适配器

3.2、基于非注解形式的handler


1、处理器和映射器的配置

SpringMVC中对组件的配置形式主要有两种,注解形式和非注解形式

给定需求:展示用户列表

1.1、用户类:

public class User {
    private Integer id;
    private String  name;
    private String address;
    //省略
}

1.2、用户列表页面:jsp文件

 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
        <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
        <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
        <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

        <html>
             <head>
                <title>数据展示</title>
            </head>
            <body>
                <h1 align="center">${class} </h1>
                <table align="center" border="1">
                     <thead>
                        <tr>
                            <td>用户id</td>
                            <td>用户名</td>
                            <td>地址</td>
                        </tr>
                    </thead>
                    <tbody>
                        <%--c:forEach --%>
                        <c:forEach items="${users}" var="user">
                            <tr>
                                <td>${user.id}</td>
                                <td>${user.name}</td>
                                <td>${user.address}</td>
                            </tr>
                        </c:forEach>
        </tbody>
        </table>
        </form>

        </body>
        </html>

2、基于注解形式实现

2.1、组件的配置文件,scources目录下的配置文件

<?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: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/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">

    <!--配置处理器映射器-->
    <!--spring 3.1版本之前-->
    <!--<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>-->
    <!--spring 3.1版本之后-->
    <!--<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>-->

    <!--配置处理器适配器-->
    <!--spring 3.1版本之前-->
    <!--<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>-->
    <!--spring 3.1版本之后-->
    <!--<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>-->

    <!--springMVC中会架子啊框架默认的处理器映射器和处理器适配器-->
    <mvc:annotation-driven/>
    
    <!--配置视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        
    </bean>

</beans>

对于处理器映射器和处理器适配器等组件可以显性的声明类全路径,需要主要使用的spring版本,在spring3.1前后使用的类是 不同的。

也可以直接通过<mvc:annotation-driven/>读取框架提供的默认的组件

2.2、基于注解的Handler

@Controller
public class UserController {

    @RequestMapping("/userlist")
    public ModelAndView userList() {
        ArrayList <User> users = new ArrayList <>();
        users.add(new User(1,"张三","西安"));
        users.add(new User(2,"小红","西安"));
        users.add(new User(3,"小李","北京"));
        users.add(new User(4,"小张","陕西西安"));

        ModelAndView modelAndView = new ModelAndView();
        //数据填充
        modelAndView.addObject("users",users);
        //指定路径 全路径
        modelAndView.setViewName("userlist");

        return modelAndView;

    }

    @RequestMapping("/usertest")
    @ResponseBody //以JSON数据返回
    public String test() {
        return "Hello Tulun Java212";
    }

}

注解使用:@Controller注解 将类交给容器管理

@RequestMapping 给定URL,。通过URL找到具体处理业务逻辑

3、基于非注解形式实现

3.1、在各组件的配置文件中,分析处理器适配器

<?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: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/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">


    <!--配置处理器映射器-->
    <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>

    <!--配置处理器适配器-->
    <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>

    <!--视图解析器-->
    <!--配置视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

</beans>

处理器适配器类是SimpleControllerHandlerAdapter,通过源码可知,要能够被处理器适配器识别的类必须是Controller接口的具体实现类

3.2、基于非注解形式的handler

/**
 * 基于非注解形式的处理器要能被处理器适配器识别
 * 必须实现org.springframework.web.servlet.mvc.Controller接口
 * 该接口中的handleRequest需要实现
 */
public class User1Controller implements Controller {
    @Override
    public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
        ArrayList<User> users = new ArrayList <>();
        users.add(new User(1,"张三","西安"));
        users.add(new User(2,"小红","西安"));
        users.add(new User(3,"小李","北京"));
        users.add(new User(4,"小张","陕西西安"));

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("users",users);
        modelAndView.setViewName("userList");
        System.out.println("测试请求");

        return modelAndView;
    }
}

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值