1、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_3_0.xsd"
id="WebApp_ID"
version="3.0">
<servlet>
<servlet-name>springMvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc/springMvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springMvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
</web-app>
2、spring-mvc/springMvc.xml定义如下:
其中: <import resource="classpath:spring-mvc/springMvc2.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:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
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 ">
<import resource="classpath:spring-mvc/springMvc2.xml"/>
<!-- 自定义处理器 -->
<bean id="simpleController"
name="/simpleController2.action"
class="com.wangBeat.module.simpleController"/>
<!-- 1、BeanNameUrlHandlerMapping 处理器映射器的 name 名字中必须要有 /
(该映射器是使用处理器的 name 值来作为url映射的)
即根据 "http://loaclhost:8080/springMvc/simpleController2.action"
来找到处理器 com.wangBeat.module.simpleController -->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
<!-- 处理器适配器 -->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
<!-- 这个类用于Spring MVC视图解析 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"/>
</beans>
3、自定义处理器
为什么我自定义的处理器要
实现 Controller 这个接口呢?
因为:我在 xml 中 使用的是
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
这个适配器;点开源码如下:
可以看到在调用之前,我所用的这个处理器适配器会先校验,我自定义的处理器是否是 Controller的子类;所以要实现这个Controller 接口。
package com.wangBeat.module;
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 simpleController implements Controller {
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
ModelAndView ModelAndView=new ModelAndView();
ModelAndView.addObject("param", "wzs");
ModelAndView.setViewName("/WEB-INF/test.jsp");
return ModelAndView;
}
}
4、test.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
name:${requestScope.param}
</body>
</html>