mvc非注解的处理器映射器和适配器

spring3.1之前使用org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping注解映射器。

spring3.1之后使用org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping注解映射器。

spring3.1之前使用org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter注解适配器。

spring3.1之后使用org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter注解适配器。

 

 

org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter

要求编写的Handler实现 Controller接口。

 

org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter

要求编写的Handler实现 HttpRequestHandler接口

 

 

 

方式二:

1.springmvc.xml配置

多个映射器可以并存,前端控制器判断url能让哪些映射器映射,就让正确的映射器处理。

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

    
    <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">bean>

    
    <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="/b.do">indexControllerprop>
                <prop key="/c.do">index2Controllerprop>
            props>
        property>
    bean>
    <bean id="indexController" class="controller.IndexController"/>
    <bean id="index2Controller" class="controller.Index2Controller"/>
    
    
    <bean id="myController" name="/hello.do" class="controller.HelloController">bean>

    
    <bean        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsps/">property>
        <property name="suffix" value=".jsp">property>
    bean>

beans>

 

 

 

2.Controller层代码:

package controller;

import org.springframework.web.HttpRequestHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class IndexController implements HttpRequestHandler {
    @Override
    public void handleRequest(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setAttribute("hello","this is a HttpRequestHandler");
        req.getRequestDispatcher("/WEB-INF/jsps/index.jsp")
                .forward(req,resp);
    }
}

或者:

package controller;

import org.springframework.web.HttpRequestHandler;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class IndexController implements Controller {
    @Override
    public ModelAndView handleRequest(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        ModelAndView mv=new ModelAndView();
        mv.addObject("hello","Welcome To MVC !");
        mv.setViewName("first");
        return mv;
    }
}

3.测试

mvc非注解的处理器映射器和适配器_第1张图片

 

你可能感兴趣的:(mvc非注解的处理器映射器和适配器)