Controller/AbstractController,他们适合于简单的请求, 不包含或很少有请求参数的情况
public class TestController implements Controller{ public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { Book book = new Book(); return new ModelAndView("test", "book", book); } } public class TestAbsController extends AbstractController{ protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception { String param = request.getParameter("paramName"); //do something... return new ModelAndView("testAbs.jsp"); } }
<bean name="/test.do" class="controller.TestController" /> <bean name="/testAbs.do" class="controller.TestAbsController" />
表单控制器 SimpleFormController,这个控制器能够做基于单表单的一些动作: 当以get方式请求该controller的时候,会显示表单页面; 当以post方式提交请求的时候,能够自动把form封装成command对象,还可以对表单进行验证,表单提交成功后会转向成功页面.在 实战springmodules common validator一文中使用该控制器。
文件名映射控制器:UrlFilenameViewController,直接把请求的url映射为view名。
<bean name="/welcome.do" class="org.springframework.web.servlet.mvc.UrlFilenameViewController" />
当请求/welcome.do的时候,该控制器直接去寻找视图welcome, 根据viewResolver:
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean>
它将把/WEB-INF/jsp/welcome.jsp返回给用户。
多动作控制器MultiActionController: 把一组相关的请求放到一个controller里面.
比如有一组与Member相关的请求,可以查询,修改, 增加,删除Member.
public class MemberController extends MultiActionController{ private MemberDao memberDao; public MemberDao getMemberDao() { return memberDao; } public void setMemberDao(MemberDao memberDao) { this.memberDao = memberDao; } public ModelAndView search(HttpServletRequest request, HttpServletResponse response){ String name = request.getParameter("name"); List list = memberDao.findByName(name); return new ModelAndView("memberList", "model", list); } public ModelAndView addMember(HttpServletRequest request, HttpServletResponse response){ Member m = new Member(); m.setName(request.getParameter("name")); m.setSex(request.getParameter("sex")); //...... memberDao.save(m); return new ModelAndView("memberDetail", "model", m); } public ModelAndView deleteMember(HttpServletRequest request, HttpServletResponse response){ return new ModelAndView("deleteSuccess"); } //......略 }
于是有一个问题:客户端请求MemberController的时候,MemberController到底调用哪个方法来处理请求呢? 这个工作由methodNameResolver(方法名解析器)来解决, 每个MultiActionController都可以配置一个methodNameResolver, methodNameResolver指定一个请求的参数名,由这个参数名的值的不同来决哪个方法.
<bean name="/member.do" class="controller.MemberController"> <property name="memberDao" ref="memberDao" /> <property name="methodNameResolver" ref="methodNameResolver" /> </bean> <bean id="methodNameResolver" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver"> <property name="paramName" value="command" /> </bean>
那么在客户端:
http://localhost:8080/spring/member.do?command=search&&name=sun 将调用search方法
http://localhost:8080/spring/member.do?command=addMember&&name=sunxing007&&sex=male 将调用addMember方法.
还有向导控制器,功能也很强大,差不多够用了,不说了.