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

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

Web.xml配置如下:



	
	LearnSpringMvc
	
	
		contextConfigLocation
		classpath:applicationContext.xml
	
	
	
		spring-mvc
		
		org.springframework.web.servlet.DispatcherServlet
		
		
			contextConfigLocation
			classpath:springMvc.xml
		
		1
	
	
		spring-mvc
		/
	
	
	
		org.springframework.web.context.ContextLoaderListener
	
	
	
		CharacterEncodingFilter
		org.springframework.web.filter.CharacterEncodingFilter
		
			encoding
			utf-8
		
	
	
		CharacterEncodingFilter
		/*
	

SpringMvc配置文件




	
	

	
	
		
		
		
	

SimpleUrlHandlerMapping

	
		
		 
			 
				simpleUrl 
			 
		 
		
		
			
				
			
		
		
		 
		 
		 	
		 		/simpleUrl1.do=simpleUrl
		 		/simpleUrl2.do=beanName
		 	
		 
		 
	
	
 
   
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

	
	
	

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

	
	
	

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

	
	
	
	
	

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

	
	
	
	
	
	
	
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 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"%>





<%=request.getAttribute("message") %>
<%=request.getAttribute("errorMessage") %>
${message} 
${errorMessage}









你可能感兴趣的:(Web,SpringMVC学习系列)