spring的portlet改进(二)支持CURD标注的groovy充当controller
spring的portlet改进(二)支持CURD标注的groovy充当controller
既然在普通的servlet的controller里面,我们已经实现了标注的groovy controller
理论上来说,也可以实现portlet的controller里面也支持groovy的annotation。
1、easygroovyplugin的改进
下面需要对我们的easygroovyplugin做一下重构和改进,不仅要支持servlet的controller,也同时要支持portlet的controller。
以前的easygroovyplugin里面的proxy类是ProxyAwareAnnotationMethodHandlerAdapter集成自
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter;这个基类
来自于servlet.mvc里面的,整个proxy类内容如下:
package com.sillycat.easygroovyplugin.servlet.proxy;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.aop.TargetSource;
import org.springframework.aop.framework.Advised;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter;
public class ProxyAwareAnnotationMethodHandlerAdapter extends
AnnotationMethodHandlerAdapter {
@Override
public ModelAndView handle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {
handler = unwrapHandler(handler);
return super.handle(request, response, handler);
}
@Override
public boolean supports(Object handler) {
handler = unwrapHandler(handler);
return super.supports(handler);
}
private Object unwrapHandler(Object handler) {
if (handler instanceof Advised) {
try {
TargetSource targetSource = ((Advised) handler)
.getTargetSource();
return targetSource.getTarget();
} catch (Exception x) {
throw new RuntimeException(x);
}
} else {
return handler;
}
}
}
其实portlet.mvc包里面的东东和servlet.mvc的东东其实是很类似的,所以我参考上面的类,写了
package com.sillycat.easygroovyplugin.portlet.proxy;
import javax.portlet.PortletRequest;
import javax.portlet.PortletResponse;
import org.springframework.aop.TargetSource;
import org.springframework.aop.framework.Advised;
import org.springframework.web.portlet.ModelAndView;
import org.springframework.web.portlet.mvc.annotation.AnnotationMethodHandlerAdapter;
public class ProxyAwareAnnotationMethodHandlerAdapter extends
AnnotationMethodHandlerAdapter {
public ModelAndView doHandle(PortletRequest request,
PortletResponse response, Object handler) throws Exception {
handler = unwrapHandler(handler);
return super.doHandle(request, response, handler);
}
@Override
public boolean supports(Object handler) {
handler = unwrapHandler(handler);
return super.supports(handler);
}
private Object unwrapHandler(Object handler) {
if (handler instanceof Advised) {
try {
TargetSource targetSource = ((Advised) handler)
.getTargetSource();
return targetSource.getTarget();
} catch (Exception x) {
throw new RuntimeException(x);
}
} else {
return handler;
}
}
}
主要的区别就是继承的基类不同了,portlet的是:
org.springframework.web.portlet.mvc.annotation.AnnotationMethodHandlerAdapter;
2、easyportlet的配置修改
applicationContext-easyportlet.xml的修改如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="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/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.sillycat.com/schema/groovy http://www.sillycat.com/schema/groovy/groovy.xsd">
<bean id="propertyConfigurer"
/>
<bean id="viewResolver"
/>
<property name="prefix" value="/WEB-INF/portlets/easyportlet/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- Default ExceptionHandler -->
<bean id="defaultExceptionHandler"
value="10" />
<property name="defaultErrorView" value="error" />
<property name="exceptionMappings">
<props>
<prop key="javax.portlet.UnavailableException">
unavailable
</prop>
<prop key="java.lang.Exception">error</prop>
</props>
</property>
</bean>
<bean />
</property>
</bean>
<bean id="handlerAdapter"
/>
<lang:defaults refresh-check-delay="5" />
<groovy:scan source-pattern="/groovy/*.groovy" />
<bean id="addressManager"
/>
</beans>
配置中没有再配置JAVA的controller了,而是直接在路径上去scan groovy的文件。将原来的controller文件稍作修改,jsp页面的内容没有改变
AddressEditController.groovy文件如下:
package com.sillycat.easyportlet.web;
import java.util.List;
import javax.portlet.ActionResponse;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.beans.factory.annotation.Autowired;
import com.sillycat.easyportlet.model.Address;
import com.sillycat.easyportlet.services.AddressManager;
@Controller
@RequestMapping("EDIT")
class AddressEditController {
@Autowired
AddressManager addressManager
@RequestMapping
// default (action=list)
public String listAddresses(Model model) {
List<Address> addresses = addressManager.list()
//List<Address> addresses = null
model.addAttribute("addresses", addresses)
return "addressList"
}
@RequestMapping(params = "action=add")
// render phase
public String showAddressForm(Model model) {
Address address = null
address = new Address()
model.addAttribute("address", address)
return "addressAdd"
}
@RequestMapping(params = "action=edit")
// render phase
public String editAddress(@RequestParam("id") Integer id, Model model) {
model.addAttribute("address", addressManager.get(id))
return "addressEdit"
}
@RequestMapping(params = "action=save")
// action phase
public void populateAddress(Address address, BindingResult result,
ActionResponse response) {
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")
public void removeAddress(@RequestParam("id") Integer id,
ActionResponse response) {
addressManager.delete(id)
response.setRenderParameter("action", "list")
}
}
AddressRedirectController.groovy文件如下:
package com.sillycat.easyportlet.web;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.beans.factory.annotation.Autowired;
import com.sillycat.easyportlet.model.Address;
import com.sillycat.easyportlet.services.AddressManager;
@Controller
@RequestMapping("VIEW")
class AddressRedirectController {
@Autowired
AddressManager addressManager
@RequestMapping
// default render (action=list)
public String listAddresses(Model model) {
List<Address> addresses = addressManager.list()
//List<Address> addresses = null
model.addAttribute("addresses", addresses)
return "addressListView"
}
@RequestMapping(params = "action=view")
// render phase
public String viewAddress(@RequestParam("id") Integer id, Model model) {
Address address = null
address = addressManager.get(id)
model.addAttribute("address", address)
return "addressView"
}
}
这样我们就将portlet从java controller改造成了groovy controller了。