SpringMVC学习笔记(三):handlerMapping的三种方式及Controller三种配置

方式一:根据beanName找到对应的Controller
前端jsp中代码

<a href="user.do?mapType=1">根据beanName找到对应的Controllera><br/>

springmvc-servlet.xml配置:


    <bean name="/user.do" class="com.lin.controller.UserController">bean>  

方式二:根据URL找到对应的Controller
前端jsp中代码

<a href="userInfo.do?mapType=2">根据URL找到对应的Controllera><br/>  

springmvc-servlet.xml配置:


<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="/userInfo.do">userControllerprop>
            props>
        property>
     bean>
     <bean id="userController"  class="com.lin.controller.UserController">bean> 

方式三:根据Controller类名找到对应的Controller
前端jsp中代码

 <a href="userController.do?mapType=3">根据Controller类名找到对应的Controllera><br/>

springmvc-servlet.xml配置:


 <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping">bean>
        <bean class="com.lin.controller.UserController">bean>

疑问:
不知道为什么我将其他两种的配合注释掉了,重启servers,另外两种方式还是有效的。

实例:
request.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>


<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP 'request.jsp' starting pagetitle>

    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    

  head>

  <body>

    <a href="user.do?mapType=1">根据beanName找到对应的Controllera><br/>
    <a href="userInfo.do?mapType=2">根据URL找到对应的Controllera><br/>    
    <a href="userController.do?mapType=3">根据Controller类名找到对应的Controllera><br/>    
    <p>${resp}p>
  body>
html>

UserController.java

package com.lin.controller;

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

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

public class UserController extends AbstractController {

    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest arg0,
            HttpServletResponse arg1) throws Exception {
        // 后台输出
        System.out.println("接收到request:" + arg0.getParameter("mapType"));
        //将前端传过来的类型又传回去显示
        ModelAndView mav = new ModelAndView("request");
        mav.addObject("resp","ok response type :" + arg0.getParameter("mapType"));
        return mav;
    }

}

web.xml


<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>SpringMVC03display-name>
  <welcome-file-list>
    <welcome-file>index.jspwelcome-file>
  welcome-file-list>

  <servlet>
    <servlet-name>springmvcservlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
  servlet>
  <servlet-mapping>
    <servlet-name>springmvcservlet-name>
    <url-pattern>*.dourl-pattern>
  servlet-mapping>
web-app>

springmvc-servlet.xml


<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
        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">

    

    
    

     
     

      
      <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping">bean>
        <bean class="com.lin.controller.UserController">bean>

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

beans>

你可能感兴趣的:(javaweb框架,javaweb)