spring的portlet改进(二)支持CURD标注的groovy充当controller

spring的portlet改进(二)支持CURD标注的groovy充当controller

既然在普通的servlet的controller里面,我们已经实现了标注的groovycontroller

理论上来说,也可以实现portlet的controller里面也支持groovy的annotation。

1、easygroovyplugin的改进

下面需要对我们的easygroovyplugin做一下重构和改进,不仅要支持servlet的controller,也同时要支持portlet的controller。

以前的easygroovyplugin里面的proxy类是ProxyAwareAnnotationMethodHandlerAdapter集成自

org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter;这个基类

来自于servlet.mvc里面的,整个proxy类内容如下:

packagecom.sillycat.easygroovyplugin.servlet.proxy;

importjavax.servlet.http.HttpServletRequest;

importjavax.servlet.http.HttpServletResponse;

importorg.springframework.aop.TargetSource;

importorg.springframework.aop.framework.Advised;

importorg.springframework.web.servlet.ModelAndView;

importorg.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter;

publicclassProxyAwareAnnotationMethodHandlerAdapterextends

AnnotationMethodHandlerAdapter{

@Override

publicModelAndViewhandle(HttpServletRequestrequest,

HttpServletResponseresponse,Objecthandler)throwsException{

handler=unwrapHandler(handler);

returnsuper.handle(request,response,handler);

}

@Override

publicbooleansupports(Objecthandler){

handler=unwrapHandler(handler);

returnsuper.supports(handler);

}

privateObjectunwrapHandler(Objecthandler){

if(handlerinstanceofAdvised){

try{

TargetSourcetargetSource=((Advised)handler)

.getTargetSource();

returntargetSource.getTarget();

}catch(Exceptionx){

thrownewRuntimeException(x);

}

}else{

returnhandler;

}

}

}

其实portlet.mvc包里面的东东和servlet.mvc的东东其实是很类似的,所以我参考上面的类,写了

packagecom.sillycat.easygroovyplugin.portlet.proxy;

importjavax.portlet.PortletRequest;

importjavax.portlet.PortletResponse;

importorg.springframework.aop.TargetSource;

importorg.springframework.aop.framework.Advised;

importorg.springframework.web.portlet.ModelAndView;

importorg.springframework.web.portlet.mvc.annotation.AnnotationMethodHandlerAdapter;

publicclassProxyAwareAnnotationMethodHandlerAdapterextends

AnnotationMethodHandlerAdapter{

publicModelAndViewdoHandle(PortletRequestrequest,

PortletResponseresponse,Objecthandler)throwsException{

handler=unwrapHandler(handler);

returnsuper.doHandle(request,response,handler);

}

@Override

publicbooleansupports(Objecthandler){

handler=unwrapHandler(handler);

returnsuper.supports(handler);

}

privateObjectunwrapHandler(Objecthandler){

if(handlerinstanceofAdvised){

try{

TargetSourcetargetSource=((Advised)handler)

.getTargetSource();

returntargetSource.getTarget();

}catch(Exceptionx){

thrownewRuntimeException(x);

}

}else{

returnhandler;

}

}

}

主要的区别就是继承的基类不同了,portlet的是:

org.springframework.web.portlet.mvc.annotation.AnnotationMethodHandlerAdapter;

2、easyportlet的配置修改

applicationContext-easyportlet.xml的修改如下:

<?xmlversion="1.0"encoding="UTF-8"?>

<beansxmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"

xmlns:util="http://www.springframework.org/schema/util"xmlns:lang="http://www.springframework.org/schema/lang"

xmlns:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context"

xmlns:groovy="http://www.sillycat.com/schema/groovy"

xsi:schemaLocation="

http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd

http://www.springframework.org/schema/utilhttp://www.springframework.org/schema/util/spring-util-2.5.xsd

http://www.springframework.org/schema/langhttp://www.springframework.org/schema/lang/spring-lang-2.5.xsd

http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-2.5.xsd

http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsd

http://www.sillycat.com/schema/groovyhttp://www.sillycat.com/schema/groovy/groovy.xsd">

<beanid="propertyConfigurer"

class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"

lazy-init="false">

<propertyname="locations">

<list>

<value>classpath*:easyportlet.properties

</value>

</list>

</property>

</bean>

<context:component-scanbase-package="com.sillycat.easyportlet.web"/>

<beanid="viewResolver"

class="org.springframework.web.servlet.view.InternalResourceViewResolver">

<propertyname="viewClass"

value="org.springframework.web.servlet.view.JstlView"/>

<propertyname="prefix"value="/WEB-INF/portlets/easyportlet/"/>

<propertyname="suffix"value=".jsp"/>

</bean>

<!--DefaultExceptionHandler-->

<beanid="defaultExceptionHandler"

class="org.springframework.web.portlet.handler.SimpleMappingExceptionResolver">

<propertyname="order"value="10"/>

<propertyname="defaultErrorView"value="error"/>

<propertyname="exceptionMappings">

<props>

<propkey="javax.portlet.UnavailableException">

unavailable

</prop>

<propkey="java.lang.Exception">error</prop>

</props>

</property>

</bean>

<beanclass="org.springframework.web.portlet.mvc.annotation.DefaultAnnotationHandlerMapping">

<propertyname="interceptors">

<bean

class="org.springframework.web.portlet.handler.ParameterMappingInterceptor"/>

</property>

</bean>

<beanid="handlerAdapter"

class="com.sillycat.easygroovyplugin.portlet.proxy.ProxyAwareAnnotationMethodHandlerAdapter"/>

<lang:defaultsrefresh-check-delay="5"/>

<groovy:scansource-pattern="/groovy/*.groovy"/>

<beanid="addressManager"

class="com.sillycat.easyportlet.services.impl.AddressManagerImpl"/>

</beans>

配置中没有再配置JAVA的controller了,而是直接在路径上去scangroovy的文件。将原来的controller文件稍作修改,jsp页面的内容没有改变

AddressEditController.groovy文件如下:

packagecom.sillycat.easyportlet.web;

importjava.util.List;

importjavax.portlet.ActionResponse;

importorg.springframework.stereotype.Controller;

importorg.springframework.ui.Model;

importorg.springframework.validation.BindingResult;

importorg.springframework.web.bind.annotation.RequestMapping;

importorg.springframework.web.bind.annotation.RequestParam;

importorg.springframework.beans.factory.annotation.Autowired;

importcom.sillycat.easyportlet.model.Address;

importcom.sillycat.easyportlet.services.AddressManager;

@Controller

@RequestMapping("EDIT")

classAddressEditController{

@Autowired

AddressManageraddressManager

@RequestMapping

//default(action=list)

publicStringlistAddresses(Modelmodel){

List<Address>addresses=addressManager.list()

//List<Address>addresses=null

model.addAttribute("addresses",addresses)

return"addressList"

}

@RequestMapping(params="action=add")

//renderphase

publicStringshowAddressForm(Modelmodel){

Addressaddress=null

address=newAddress()

model.addAttribute("address",address)

return"addressAdd"

}

@RequestMapping(params="action=edit")

//renderphase

publicStringeditAddress(@RequestParam("id")Integerid,Modelmodel){

model.addAttribute("address",addressManager.get(id))

return"addressEdit"

}

@RequestMapping(params="action=save")

//actionphase

publicvoidpopulateAddress(Addressaddress,BindingResultresult,

ActionResponseresponse){

if(address!=null){

if(address.getId()!=null&&address.getId().intValue()!=0){

addressManager.update(address)

}else{

addressManager.create(address)

}

}

response.setRenderParameter("action","list")

}

@RequestMapping(params="action=delete")

publicvoidremoveAddress(@RequestParam("id")Integerid,

ActionResponseresponse){

addressManager.delete(id)

response.setRenderParameter("action","list")

}

}

AddressRedirectController.groovy文件如下:

packagecom.sillycat.easyportlet.web;

importjava.util.List;

importorg.springframework.beans.factory.annotation.Autowired;

importorg.springframework.stereotype.Controller;

importorg.springframework.ui.Model;

importorg.springframework.web.bind.annotation.RequestMapping;

importorg.springframework.web.bind.annotation.RequestParam;

importorg.springframework.beans.factory.annotation.Autowired;

importcom.sillycat.easyportlet.model.Address;

importcom.sillycat.easyportlet.services.AddressManager;

@Controller

@RequestMapping("VIEW")

classAddressRedirectController{

@Autowired

AddressManageraddressManager

@RequestMapping

//defaultrender(action=list)

publicStringlistAddresses(Modelmodel){

List<Address>addresses=addressManager.list()

//List<Address>addresses=null

model.addAttribute("addresses",addresses)

return"addressListView"

}

@RequestMapping(params="action=view")

//renderphase

publicStringviewAddress(@RequestParam("id")Integerid,Modelmodel){

Addressaddress=null

address=addressManager.get(id)

model.addAttribute("address",address)

return"addressView"

}

}

这样我们就将portlet从javacontroller改造成了groovycontroller了。

相关推荐