- HttpRequestHandler 请求处理器
- 处理器映射
BeanNameUrlHandlerMappingBeanNameUrlHandlerMapping
SimpleUrlHandlerMapping - Controller 控制器接口
- MultiActionController 多动作控制器类
- UrlBasedViewResolver 基于地址的视图解析器(将不带后缀的名称字符串,自动解析成站点下有效的视图资源)
- InternalResourceViewResolver 内部资源视图解析器
HttpRequestHandler Demo:
package com.controller;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.HttpRequestHandler;
//组件1:HttpRequestHandler
public class HelloController implements HttpRequestHandler {
@Override
public void handleRequest(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
System.out.println("请求过来了");
resp.sendRedirect("index.jsp");
}
}
处理器映射配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
">
<!-- 组件2:处理器映射 -->
<!-- 1:BeanNameUrlHandlerMapping -->
<!--
<bean name="/hello" class="com.controller.HelloController"></bean>
<bean name="/he*" class="com.controller.HelloController"></bean>
-->
<!-- 2:SimpleUrlHandlerMapping -->
<!-- 区别是不用写路径符了 -->
<bean name="hello" class="com.controller.HelloController"></bean>
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/he*">hello</prop>
</props>
</property>
</bean>
</beans>
Controller Demo
package com.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
//组件三:Controller
public class HelloController2 implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest req,
HttpServletResponse resp) throws Exception {
ModelAndView mv = new ModelAndView();
//此接口,实现了最简单的页面跳转和数据封装的操作
mv.setViewName("index.jsp");
mv.addObject("name", "bwf");
return mv;
}
}
MultiActionController Demo
package com.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import com.entity.Person;
//组件4:多动作控制器
public class MyController extends MultiActionController {
//默认匹配执行的方法
/* @Override
protected ModelAndView handleRequestInternal(HttpServletRequest request,
HttpServletResponse response) throws Exception {
System.out.println("myController调用了吗?");
return null;
}*/
public ModelAndView m1(HttpServletRequest request,HttpServletResponse response){
System.out.println("执行方法1");
return null;
}
public ModelAndView m2(HttpServletRequest request,HttpServletResponse response){
System.out.println("执行方法2");
return null;
}
public void m3(HttpServletRequest request,HttpServletResponse response){
System.out.println("执行方法3");
String name = request.getParameter("name");
System.out.println(name);
}
public void m4(HttpServletRequest request,HttpServletResponse response,String name){
System.out.println("执行方法4");
System.out.println(name);//空,无法直接传字符串
}
public void m5(HttpServletRequest request,HttpServletResponse response,Person p){
System.out.println("执行方法5");
System.out.println(p.getName());//它可以将参数自动封装到类中
}
public ModelAndView m6(HttpServletRequest request,HttpServletResponse response,Person p){
System.out.println("执行方法6");
ModelAndView mv = new ModelAndView();
mv.setViewName("../index.jsp");
mv.addObject("name", p.getName());
return mv;
}
}
配置文件内容:
<!-- 地址栏访问的时候是a4/m1?param=p1¶m=p2... -->
<bean name="/a4/*" class="com.controller.MyController"></bean>
UrlBasedViewResolver Demo
ViewResolverDemo.java
package com.controller;
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 ViewResolverDemo implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest arg0,
HttpServletResponse arg1) throws Exception {
ModelAndView mv = new ModelAndView();
mv.setViewName("index");
//通过视图配置,自动映射到根目录下的index.jsp,好处不明显,如果是站点下多级目录就明显了
return mv;
}
}
配置内容:
<!-- 配置视图解析器 1-->
<!-- UrlBasedViewResolver:接口ViewResolver的实现,用于根据请求的URL路径返回相应的视图,
该视图需为抽象类AbstractUrlBasedView的实现,包括子类,如InternalResourceView和JstlView等 -->
<bean class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<!-- <property name="prefix" value="/WEB-INF/"></property> 这地方后面一个斜杠必须写上 -->
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
<property name="viewClass" value="org.springframework.web.servlet.view.InternalResourceView"></property>
</bean>
<bean name="/viewdemo" class="com.controller.ViewResolverDemo"></bean>
InternalResourceViewResolver Demo
ViewResolverDemo2.java
package com.controller;
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 ViewResolverDemo2 implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest arg0,
HttpServletResponse arg1) throws Exception {
ModelAndView mv = new ModelAndView();
mv.setViewName("index");
return mv;
}
}
配置内容:
<!-- 配置视图解析器2 -->
<!-- InternalResourceViewResolver:UrlBasedViewResolver的子类,通常用于查找 JSP和JSTL等视图-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<bean name="/viewdemo2" class="com.controller.ViewResolverDemo2"></bean>