SpringMvc系列之SpringMvc常用请求映射器实例

  1. 云栖社区>
  2. 博客>
  3. 正文

SpringMvc系列之SpringMvc常用请求映射器实例

木叶之荣 2016-05-29 21:21:00 浏览780
展开阅读全文

本片文章用来介绍常用的SpringMvc常用的请求映射器。

Web.xml配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
	
	<display-name>LearnSpringMvc</display-name>
	<!-- 配置Spring上下文配置文件 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext.xml</param-value>
	</context-param>
	<!-- 配置SpringMVC Servlet -->
	<servlet>
		<servlet-name>spring-mvc</servlet-name>
		<!-- SpringMVC 分发器 -->
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<!-- 更改SpringMVC配置文件的名字 默认的是spring-mvc-servlet.xml -->
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:springMvc.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>spring-mvc</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
	<!-- 配置监听器 用来载入上下文配置文件 -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<!-- 编码过滤器 -->
	<filter>
		<filter-name>CharacterEncodingFilter</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>utf-8</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>CharacterEncodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
</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:tx="http://www.springframework.org/schema/tx"
	xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	                    http://www.springframework.org/schema/beans/spring-beans-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/mvc
	                    http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
	                    http://www.springframework.org/schema/context 
                        http://www.springframework.org/schema/context/spring-context-3.2.xsd"
	default-autowire="byName">

	<!-- 加载静态资源 -->
	<mvc:resources location="/static/" mapping="/static/**" />

	<!-- 视图解析器 常用的还有 VelocityViewResolver、FreeMarkerViewResolver -->
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="viewClass"
			value="org.springframework.web.servlet.view.JstlView" />
		<property name="prefix" value="/WEB-INF/jsp/" />
		<property name="suffix" value=".jsp" />
	</bean>
</beans>

SimpleUrlHandlerMapping

	<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
		<!-- 第一种配置方式 -->
		<property name="mappings"> 
			<props> 
				<prop key="/simpleUrl.do">simpleUrl</prop> 
			</props> 
		</property> 
		<!-- 第二种配置方式 -->
		<property name="urlMap">
			<map>
				<entry key="/simpleUrl1.do" value-ref="simpleUrl"/>
			</map>
		</property>
		
		 <!-- 第三种配置方式  -->
		 <property name="mappings">
		 	<value>
		 		/simpleUrl1.do=simpleUrl
		 		/simpleUrl2.do=beanName
		 	</value>
		 </property>
		 
	</bean>
	<bean name="simpleUrl" class="com.zkn.learnspringmvc.controller.SimpleUrlController" /><pre name="code" class="java">

package com.zkn.learnspringmvc.controller;

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

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

/**
 * 测试SimpleURL
 * @author zkn 2016-05-23 
 *
 */

public class SimpleUrlController extends AbstractController{

	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ModelAndView mv = new ModelAndView();
		mv.addObject("message", "这是SimpleUrlController");
		mv.addObject("errorMessage", "这是SimpleUrlController");
		mv.setViewName("home");
		return mv;
	}
}

ControllerBeanNameHandlerMapping

	<!-- bean名字控制器 和bean的名字有关系和类的名字没有关系 可以和ControllerClassNameHandlerMapping 
		相互对比记忆 -->
	<bean
		class="org.springframework.web.servlet.mvc.support.ControllerBeanNameHandlerMapping" />
	<bean name="beanName" class="com.zkn.learnspringmvc.controller.BeanNameController" />

package com.zkn.learnspringmvc.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 BeanNameController extends AbstractController{

	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ModelAndView mv = new ModelAndView();
		mv.addObject("message", "这是BeanNameController");
		mv.addObject("errorMessage", "这是第二个首页错误页面");
		mv.setViewName("home");
		return mv;
	}
}

ControllerClassNameHandlerMapping

	<!-- 类名称控制器 注意访问的时候是 /classname 全部是小写的。Controller不用写。 和bean的名字没关系,只和类的名字有关系。 -->
	<bean
		class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />
	<bean name="className" class="com.zkn.learnspringmvc.controller.ClassNameController" />

package com.zkn.learnspringmvc.controller;

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

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

/**
 * 类名字映射处理器
 * 访问路径为:http://localhost:8080/LearnSpringMvc/classname
 * @author wb-zhangkenan
 *
 */

public class ClassNameController extends AbstractController{

	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ModelAndView mv = new ModelAndView();
		mv.addObject("message", "这个是ClassNameController");
		mv.setViewName("home");
		return mv;
	}

	
}

BeanNameUrlHandlerMapping

	<!-- 请求处理映射器 beanName映射器 将Controller包装为HandlerExecutionChain -->
	<bean
		class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />
	<!-- 调用Controller中的处理器方法 -->
	<bean
		class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />
	<bean name="/beanUrlName"
		class="com.zkn.learnspringmvc.controller.BeanNameURLController"></bean>

package com.zkn.learnspringmvc.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 BeanNameURLController extends AbstractController{

	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ModelAndView mv = new ModelAndView();
		mv.addObject("message", "BeanNameURLController");
		mv.addObject("errorMessage", "这是第二个首页错误页面");
		mv.setViewName("home");
		return mv;
	}

}

RequestMappingHandlerMapping

	<!-- SpringMVC注解驱动 -->
	<mvc:annotation-driven />
	<!-- 注解扫描 Spring Bean -->
	<context:component-scan base-package="com.zkn.learnspringmvc.controller" />
	<!-- DefaultAnnotationHandlerMapping从3.1之后被标注为过期,3.1之后用 RequestMappingHandlerMapping -->
	<bean
		class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" />
	<bean
		class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter" />
package com.zkn.learnspringmvc.controller;

import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * 
 * @author zkn 2016-05-16
 *
 */
@Controller
public class HomeController {
	/**
	 * 注解第一种写法
	 * @param modelMap
	 * @return
	 */
	@RequestMapping(value="/home1.do")
	public String home(Model modelMap){
		modelMap.addAttribute("message", "这个是首页!!!");
		modelMap.addAttribute("errorMessage", "这个是首页!!!");
		return "home";
	}
	/**
	 * 注解第二种写法
	 * @param modelMap
	 * @return
	 */
	@RequestMapping(value="/home2.do")
	public ModelAndView home(){
		ModelAndView mv = new ModelAndView();
		mv.addObject("message", "这是第二个首页");
		mv.addObject("errorMessage", "这是第二个首页错误页面");
		mv.setViewName("home");
		return mv;
	}
	/**
	 * 注解第三种写法
	 * @param modelMap
	 * @return
	 */
	@RequestMapping(value="/home3.do")
	public String home2(Map<String, Object> map){
		map.put("message", "这是home3.do");
		map.put("errorMessage", "home3.do");
		return "home";
	}
}
jsp页面
<%@page language="java" contentType="text/html;charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<script type="text/javascript" src="static/js/jquery-2.1.4.min.js"></script>
</head>
<body>
<%=request.getAttribute("message") %>
<%=request.getAttribute("errorMessage") %>
${message} 
${errorMessage}
</body>
</html>







网友评论

登录后评论
0/500
评论
木叶之荣
+ 关注