Controller层次结构:
处理器映射:
BeanNameUrlHandlerMapping
SimpleUrlHandlerMapping(推荐)
MultiActionController的配置方式:
默认使用InternalPathmethodNameResolver进行方法名的解析
还可以使用ParameterMethodNameResolver,PropertiesMethodNameResolver进行解析
使用MultiActionController实现多动作请求
MultiActionController中请求处理的方法签名
// 'anyMeaningfulName'指任意方法名
public [ModelAndView | Map | void] anyMeaningfulName(HttpServletRequest, HttpServletRequest [,HttpSession] [,AnyObject])
方法签名中除了可以指定HttpServletRequest与HttpServletRequest外,还可以指定 HttpSession与AnyObject,AnyObject可以是命令对象类型或异常类型,当为命令对象
类型时,可以接收参数信息。
使用ParameterMethodNameResolver实现对方法名的解析
一个例子
web.xml中DispatcherServlet的配置:
<!-- 配置加载service与dao,这里省去 ,这里所有配置都写在了classpath:applicationContext.xml。
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-dao.xml,classpath:applicationContext-service.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
-->
<!-- 配置DispatcherServlet -->
<servlet>
<servlet-name>example</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--
可以不配置init-param,默认加载example-servlet.xml
example与配置的DispatcherServlet名称相对应
-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>example</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
blogController类:
/**
* @author evan
* 请求处理的方法签名
* // 'anyMeaningfulName'指任意方法名
* public [ModelAndView | Map | void] anyMeaningfulName
* (HttpServletRequest, HttpServletRequest [,HttpSession] [,AnyObject])
* 方法签名中除了可以指定HttpServletRequest与HttpServletRequest外,还可以指定
* HttpSession与AnyObject,AnyObject可以是命令对象类型或异常类型,当为命令对象
* 类型时,可以接收参数信息。
*/
public class BlogController extends MultiActionController {
private BlogServiceImpl blogService;
public void setBlogService(BlogServiceImpl blogService) {
this.blogService = blogService;
}
public ModelAndView addBlog(HttpServletRequest request,
HttpServletResponse response,HttpSession session,Blog blog) throws Exception {
//打印sessionId与blog,验证是否已传递参数
System.out.println("sessionId ------>"+session.getId());
System.out.println("blog ------>"+blog);
blogService.execute();
ModelAndView modelAndView=new ModelAndView();
modelAndView.setViewName("success");
modelAndView.addObject("hello", "helloworld");
modelAndView.addObject("type","addBlog");
return modelAndView;
}
public ModelAndView showBlog(HttpServletRequest request,
HttpServletResponse response) throws Exception {
blogService.execute();
return new ModelAndView("showBlog", "type", "showBlog...");
}
public ModelAndView deleteBlog(HttpServletRequest request,
HttpServletResponse response) throws Exception {
blogService.execute();
return new ModelAndView("success", "type", "deleteBlog");
}
}
BlogServiceImpl类:
package cn.enjoylife.springmvc.service.impl;
/**
* @author evan
*
*/
public class BlogServiceImpl {
public void execute(){
System.out.println("execute......");
}
}
applicationContext.xml
<!-- 使用简单url处理器映射 -->
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<value>
/blog/blogManager.html=blogController
...
...
</value>
</property>
</bean>
<!-- 配置视图解析器 ,使用jstl视图-->
<bean id="jspViewResolver"
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>
<!-- 使用ParameterMethodNameResolver作用方法名解析器 -->
<bean id="methodNameResolver"
class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
<property name="paramName" value="method" />
</bean>
<!-- methodNameResolver用于配置所使用的方法名解析器 -->
<bean id="blogController" class="cn.enjoylife.springmvc.web.action.BlogController">
<property name="blogService" ref="blogService" />
<property name="methodNameResolver" ref="methodNameResolver" />
</bean>
<bean id="blogService" class="cn.enjoylife.springmvc.service.impl.BlogServiceImpl" />
访问方式:
http://localhost:8080/spring_mvc/blog/blogManager.html?method=deleteBlog 对应deleteBlog方法
http://localhost:8080/spring_mvc/blog/blogManager.html?method=addBlog&id=222 对应addBlog方法