04SpringMvc_映射器_BeanNameUrlHanderMapping


這篇文章我們講的是映射器,映射器的作用是什么樣的請求交給Action,如果我們沒有在xml配置文件中進行配置,默認的就是BeanNameUrlHanderMapping.

我們講一個案例增加用戶的案例.

案例結構圖如下:

 

對上面的案例結構進行說明:

   1. UserAction.java是增加用戶的Action。

   2.SpringMvc_002.xml是對應這個案例的配置文件。

  3.Spirngmvc.xml是總的配置文件。

  4.index.jsp是項目默認的訪問頁面。

  5.succss.jsp是成功的訪問頁面。

 

第一步:寫UserAction.java.

 代碼如下:

package com.guigu.shen.Action2;

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 UserAction implements Controller {


    @Override
    public ModelAndView handleRequest(HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("message", "增加了一個用戶");
        modelAndView.setViewName("success");
        
        return modelAndView;
    }

}

第二步:寫SpringMvc_002.xml

<?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:aop="http://www.springframework.org/schema/aop"
      xmlns:tx="http://www.springframework.org/schema/tx"
      xmlns:mvc="http://www.springframework.org/schema/mvc"
      xsi:schemaLocation="
      
      http://www.springframework.org/schema/beans 
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
      
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-3.0.xsd
       
      http://www.springframework.org/schema/aop 
      http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
      
      http://www.springframework.org/schema/tx
      http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    
      http://www.springframework.org/schema/mvc
      http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"
>
     <!-- 控制器(程序員)(必須配置) -->
<bean name="/adduser.action" class="com.guigu.shen.Action2.UserAction"></bean>
 <!-- 如果Action匯總書寫的是視圖邏輯名稱,那么視圖解析器就必須配置(解釋一下什么是視圖邏輯名稱:就是類似Struts2中的,"success")
               如果Action中配置的是視圖真實名稱,那么視圖解析器就可選配置(解釋一下什么是視圖真實名稱,就是"/jsp/success.jsp")
 -->
 <bean  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
 <!-- 配置路徑前綴 -->
 <property name="prefix" value="/jsp/"></property>
 <!-- 配置路徑后綴 -->
 <property name="suffix" value=".jsp"></property>
 <!-- 上面的配置方法其實就是前綴+視圖邏輯名+后綴=真實路徑 -->
 </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:context="http://www.springframework.org/schema/context"
      xmlns:aop="http://www.springframework.org/schema/aop"
      xmlns:tx="http://www.springframework.org/schema/tx"
      xmlns:mvc="http://www.springframework.org/schema/mvc"
      xsi:schemaLocation="
      
      http://www.springframework.org/schema/beans 
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
      
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-3.0.xsd
       
      http://www.springframework.org/schema/aop 
      http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
      
      http://www.springframework.org/schema/tx
      http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    
      http://www.springframework.org/schema/mvc
      http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"
>
<import resource="com/guigu/shen/Action2/springmvc_002.xml"/>
</beans>

第四步:寫index.jsp

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

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
    <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">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
  </head>
  
  <body>
    This is my JSP page. <br>
    <a href="${pageContext.request.contextPath}/adduser.action">增加用戶</a>
  </body>
</html>

對於上面的<a href="${pageContext.request.contextPath}/adduser.action">增加用戶</a>我要重點解釋一下。這么寫的話,用火狐調試出來的訪問路徑是:

http://127.0.0.1:8080/SpringMvc_10day_self/adduser.action。但是如果改為<a href="/adduser.action">那么訪問路徑是:http://127.0.0.1:8080/adduser.action

${pageContext.request.contextPath}這句話是訪問絕對路徑。也就是相當於/SpringMvc_10day_self。

 

 

第五步:寫success.jsp

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

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
    <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">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
  </head>
  
  <body>
    Success. <br>
    ${message} 
  </body>
</html>

第六步:配置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_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>SpringMvc_10day_self</display-name>
  <servlet>
  <!--這個名字可以隨便取得,但是這個名字取了之后,以后在 WEB-INF下面創建SpirngMVC的配置文件是,命名必須以這個開頭,
  
  所以這里取名叫做DispatcherServlet,那么之后的xml文件取名必須為DispatcherServlet-servlet.xml(一個字都不能差)
  
  -->
  <servlet-name>DispatcherServlet</servlet-name>
  <servlet-class> org.springframework.web.servlet.DispatcherServlet</servlet-class>
 <!-- 通知DispatcherServlet去指定目錄下找到springmvc.xml配置文件 -->
 <!-- 
 注意這里的  <param-name>contextConfigLocation</param-name>一個字母都不能有錯
 一旦有錯就會去WEB-INF下面去找
  -->
          <init-param>
               <param-name>contextConfigLocation</param-name>
              <param-value>classpath:springmvc.xml</param-value>
          </init-param>
 </servlet>
 <servlet-mapping>
   <servlet-name>DispatcherServlet</servlet-name>
    <url-pattern>*.action</url-pattern>
 
 </servlet-mapping>
 
  <welcome-file-list>
   
    <welcome-file>index.jsp</welcome-file>
  
  </welcome-file-list>
</web-app>

 

調試:

運行結果:對的。

 

總結一下執行的流程:

瀏覽器發送請求。然后默認進入了index.xml。然后點擊增加用戶的按鈕,發送了一個Action請求,因為web.xml中的配置,該請求被DispatcherServlet攔截下來。

然后找到了springmvc_002.xml配置文件,創建了里面的bean實例(com.guigu.shen.Action2.UserAction;InternalResourceViewResolver)。接着根據

映射器(BeanNameUrlHandlerMapping)找到了/adduser.action請求對應的UserAction對象,然后執行里面的handleRequest方法,最后再根據視圖解析器(InternalResourceViewResolver)找到success.jsp頁面。並把數據顯示出來。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM