读书人

spring 三 mvc中的controller小结

发布时间: 2012-09-21 15:47:26 作者: rapoo

spring 3 mvc中的controller小结
本文小结下spring 3 MVC中常见的几个controller相关的

1 MultiActionController
比如在一个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;

public class CustomerController extends MultiActionController{

public ModelAndView add(HttpServletRequest request,
HttpServletResponse response) throws Exception {

return new ModelAndView("CustomerPage", "msg","add() method");

}

public ModelAndView delete(HttpServletRequest request,
HttpServletResponse response) throws Exception {

return new ModelAndView("CustomerPage", "msg","delete() method");

}

public ModelAndView update(HttpServletRequest request,
HttpServletResponse response) throws Exception {

return new ModelAndView("CustomerPage", "msg","update() method");

}

public ModelAndView list(HttpServletRequest request,
HttpServletResponse response) throws Exception {

return new ModelAndView("CustomerPage", "msg","list() method");

}

}

<beans ...>

<bean
/>

<bean />

</beans>

下面的形成匹配:
/customer/add.htm > add()
/customer/delete.htm > delete()
/customer/update.htm > update()
/customer/list.htm > list()
而InternalPathMethodNameResolver是MultiActionController的默认实现,
<beans ...>
<bean
/>

<bean value="test" />
<property name="suffix" value="Customer" />
</bean>
</property>
</bean>
</beans>
增加了前缀和后缀,比如:
/customer/add.htm > testaddCustomer()
/customer/delete.htm > testdeleteCustomer()
/customer/update.htm > testupdateCustomer()
/customer/list.htm > testlistCustomer()
如果用annotation则更方便了,上面的变为:
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class CustomerController{

@RequestMapping("/customer/add.htm")
public ModelAndView add(HttpServletRequest request,
HttpServletResponse response) throws Exception {

return new ModelAndView("CustomerAddView");

}
2 PropertiesMethodNameResolver
这个可以配合multication去自定义匹配:
如:
<beans ...>

<bean
/>

<bean />

<bean value="action"/>
</bean>
</property>
</bean>

</beans>
/customer/*.htm?action=add > add() method
/customer/whatever.htm?action=add > add() method
/customer/*.htm?action=update > update() method
/customer/*.htm?action=delete > delete() method
/customer/*.htm?action=list > list() method


4 ParameterizableViewController
一般要经过CONTROLLER才能返回一个VIEW,比如:
public class WelcomeController extends AbstractController{

@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request,
HttpServletResponse response) throws Exception {

ModelAndView model = new ModelAndView("WelcomePage");
return model;

}

}
但现在实际不用了:
<beans xmlns="http://www.springframework.org/schema/beans"
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-2.5.xsd">

<bean
value="WelcomePage" />
</bean>

当访问http://localhost/welcome.htm后,直接跳转到WelcomePage.jsp页面了

读书人网 >VC/MFC

热点推荐