码迷,mamicode.com
首页 > 编程语言 > 详细

springMVC非注解常用的"处理器映射器"、"适配器"、"处理器"

时间:2016-07-14 02:34:14      阅读:296      评论:0      收藏:0      [点我收藏+]

标签:

非注解处理器映射器
1. org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping
url 到bean name的映射
2. org.springframework.web.servlet.handler.SimpleUrlHandlerMapping
url 到bean id的映射

非注解适配器
1. org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter
  要求编写的Handler实现Controller接口
2. org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter.class

  要求编写的Handler实现HttpRequestHandler接口

处理器Handler
1. implements org.springframework.web.servlet.mvc.Controller
2. implements org.springframework.web.HttpRequestHandler

 

处理器代码:

ItemsController1因为和SimpleControllerHandlerAdapter适配,因此实现Controller接口

package wzd.pkg.ssm.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

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

public class ItemsController1 implements Controller {

    @Override
    public ModelAndView handleRequest(HttpServletRequest request,
            HttpServletResponse response) throws Exception {

        //调用service查找数据库,查询商品列表。
        
        
        
        //返回ModelAndView
        ModelAndView modelAndView=new ModelAndView();
        
        //相当于request的setAttribute,在jsp页面中通过itemsList取数据
        modelAndView.addObject("itemsList", "controller1 content");
        
        modelAndView.setViewName("/WEB-INF/jsp/items.jsp");
        
        return modelAndView;
    }

}

ItemsController2因为和SimpleControllerHandlerAdapter适配,因此实现Controller接口

package wzd.pkg.ssm.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

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

public class ItemsController2 implements Controller {

    @Override
    public ModelAndView handleRequest(HttpServletRequest request,
            HttpServletResponse response) throws Exception {

        //调用service查找数据库,查询商品列表。
        
        
        
        //返回ModelAndView
        ModelAndView modelAndView=new ModelAndView();
        
        //相当于request的setAttribute,在jsp页面中通过itemsList取数据
        modelAndView.addObject("itemsList", "controller2 content");
        
        modelAndView.setViewName("/WEB-INF/jsp/items.jsp");
        
        return modelAndView;
    }

}

ItemsController3因为和HttpRequestHandlerAdapter适配,因此实现RequestHandler接口

package wzd.pkg.ssm.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.HttpRequestHandler;

public class ItemsController3 implements HttpRequestHandler {

    @Override
    public void handleRequest(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        
        
        request.setAttribute("itemsList", "controller3 content");
        
        
        request.getRequestDispatcher("/WEB-INF/jsp/items.jsp").forward(request, response);
        
        
    }



}

  对应的web.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>GoodsFind</display-name>
  
  
  
  <!-- 配置前段控制器 -->
  <servlet>
      <servlet-name>springmvc</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:springmvc.xml</param-value>
      </init-param>
  </servlet>
  
  <servlet-mapping>
      <servlet-name>springmvc</servlet-name>
      <!-- 
          第一种:*.action,访问以.action结尾由DispathcerServlet进行解析
          第二种:/,所有访问的地址都由DispathcerServlet进行解析,对于静态文件的解析需要配置不让DispathcerServlet进行解析。
          使用此种方法可以实现RESTful风格的url。
          第三种:/*,这样配置不对
       -->
      <url-pattern>*.action</url-pattern>
  </servlet-mapping>
  
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

  springMVC的配置文件

<?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-3.2.xsd   
               http://www.springframework.org/schema/mvc
               http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
            http://www.springframework.org/schema/aop 
            http://www.springframework.org/schema/aop/spring-aop-3.2.xsd   
            http://www.springframework.org/schema/tx 
              http://www.springframework.org/schema/tx/spring-tx-3.2.xsd   
            http://www.springframework.org/schema/context 
            http://www.springframework.org/schema/context/spring-context-3.2.xsd">
    <!-- 配置handler -->
    <bean id="queryItems1" name="/queryItems1s.action" class="wzd.pkg.ssm.controller.ItemsController1"/>
    <bean id="queryItems2" name="/queryItems2s.action" class="wzd.pkg.ssm.controller.ItemsController2"/>
    <bean id="queryItems3" name="/queryItems3s.action" class="wzd.pkg.ssm.controller.ItemsController3"/>
    
    <!-- 处理器映射器 
    将bean的name作为url进行查找,需要在配置Handler时指定beanname(就是url)这是默认的处理器映射器
    -->
    <bean
        class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" 
    />
    <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
        <props>
        <!-- 对queryItems1进行url映射,url是 queryItems1.action和queryItems2.action -->
        <prop key="/queryItems1.action">queryItems1</prop>
        <prop key="/queryItems2.action">queryItems2</prop>
        <prop key="/queryItems3.action">queryItems3</prop>
        </props>
        </property>
    </bean>
    
    <!-- 处理器适配器 
        所有的处理器适配器都实现HandlerAdapter接口
    -->
    <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
    <bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter"/>
    
    <!-- 视图解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    </bean>
    
    
</beans>

  试图文件,一个jsp页面(items.jsp)。

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>items jsp</title>
</head>
<body>

这个是jsp页面。<br>

获取值:
<%=request.getAttribute("itemsList") %><br>



</body>
</html>

 

通过在浏览器中输入以下地址,将会正常访问items.jsp页面。

  http://localhost:8080/GoodsFind/queryItems1.action

  http://localhost:8080/GoodsFind/queryItems1s.action

  http://localhost:8080/GoodsFind/queryItems2.action

  http://localhost:8080/GoodsFind/queryItems2s.action

  http://localhost:8080/GoodsFind/queryItems3.action

  http://localhost:8080/GoodsFind/queryItems3s.action

其中:http://localhost:8080/GoodsFind/queryItems1.action http://localhost:8080/GoodsFind/queryItems2.action http://localhost:8080/GoodsFind/queryItems3.action都是通过BeanNameUrlHandlerMapping进行映射。

  http://localhost:8080/GoodsFind/queryItems1s.action http://localhost:8080/GoodsFind/queryItems2s.action http://localhost:8080/GoodsFind/queryItems3s.action是通过SimpleUrlHandlerMapping进行映射。

  

  http://localhost:8080/GoodsFind/queryItems1.action http://localhost:8080/GoodsFind/queryItems1s.action http://localhost:8080/GoodsFind/queryItems2.action http://localhost:8080/GoodsFind/queryItems2s.action对应的处理器是继承自Contrller。

  

http://localhost:8080/GoodsFind/queryItems3.action http://localhost:8080/GoodsFind/queryItems3s.action对应的处理器是继承自HttpRequestHandler。

 

springMVC非注解常用的"处理器映射器"、"适配器"、"处理器"

标签:

原文地址:http://www.cnblogs.com/wzd5230/p/5668746.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!