码迷,mamicode.com
首页 > 移动开发 > 详细

04SpringMvc_映射器_BeanNameUrlHanderMapping

时间:2016-08-07 00:55:59      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:

这篇文章我们讲的是映射器,映射器的作用是什么样的请求交给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页面。并把数据显示出来。

 

04SpringMvc_映射器_BeanNameUrlHanderMapping

标签:

原文地址:http://www.cnblogs.com/shenxiaoquan/p/5745227.html

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