本站首页    管理页面    写新日志    退出


«August 2025»
12
3456789
10111213141516
17181920212223
24252627282930
31


公告
 本博客在此声明所有文章均为转摘,只做资料收集使用。

我的分类(专题)

日志更新

最新评论

留言板

链接

Blog信息
blog名称:
日志总数:1304
评论数量:2242
留言数量:5
访问次数:7595295
建立时间:2006年5月29日




[Java Open Source]Liferay Portal额外研究(5):对多分发命令Action的支持(方案一)
软件技术

lhwork 发表于 2006/9/4 18:05:11

作者:胡长城(银狐999)时间:2006年9月3日晚     Liferay默认提供的基于Struts Action扩展的PortletAction是不支持多分发命令的,也就是我们一般常用的DispatchAction。但在我们日常基于Struts处理的操作中,已经大量的沿用了DispatchAction处理方式,采用“cmd=queryall”诸如此类的方式。    本文就来给大家讲解如何通过扩展,让Liferay实现对多分发命令Action的支持。     首先让我们来看看Liferay是如何处理的:     在portlet.xml中,我们一般会配置如下:500)this.width=500'><portlet-class>com.liferay.portlet.StrutsPortlet</portlet-class>500)this.width=500'><init-param>500)this.width=500'>    <name>view-action</name>500)this.width=500'>    <value>/ext/reports/view_reports</value>500)this.width=500'></init-param>         这样Liferay面对一个Portlet请求的时候,会根据请求model来执行Portlet的doView或doEdit方式。当执行doView的时候就会请求其view-action所配置的Action URL所代表的Action来处理。      其处理流程大致是:Portlet类——〉RequestProcessor——〉StrutsAction处理类。      我们可以通过两种扩展方案来实现对多分发的支持:     方案(一):扩展Liferay的StrutsPortlet类,并写一个DispatchPortletAction类,这样不用扩展RequestProcessor实现。     方案(二):扩展RequestProcessor 与,并写一个DispatchPortletAction类,这样可以直接使用Liferay所提供的StrutsPortlet类。对于 RequestProcessor的扩展,在通过portal.properties文件中通过配置 “struts.portlet.request.processor”属性来设置。     接下来就两种方案做分别的详细讲解(本篇先讲方案一): 方案(一) 首先让我们写一个DispatchPortletAction 类,此类可以通过扩展Liferay本身的PortletAction实现,也可以通过扩展Struts本身的DispatchAction实现。本人是 选择后一种方式,这样扩展的代码量较少,都不要自己写execute方式,直接使用基类的即可。对于DispatchPortletAction 主要扩展dispatchMethod和getMethodName方法。注意在getMechodName方法中,还追加了从 request.getAttribute(parameter)获取方法名称,并注意unspecified方法,这个是在没有指明访问方法的时候默认 执行的,所以开发人员在后续写自己的Action一定要实现这个。500)this.width=500'>500)this.width=500'>public class DispatchPortletAction extends DispatchAction ...{500)this.width=500'>    protected ActionForward dispatchMethod(ActionMapping mapping,500)this.width=500'>            ActionForm form, HttpServletRequest request,500)this.width=500'>500)this.width=500'>            HttpServletResponse response, String name) throws Exception ...{500)this.width=500'>500)this.width=500'>        PortletConfig portletConfig = (PortletConfig) request500)this.width=500'>                .getAttribute(WebKeys.JAVAX_PORTLET_CONFIG);500)this.width=500'>500)this.width=500'>        RenderRequest renderRequest = (RenderRequest) request500)this.width=500'>                .getAttribute(WebKeys.JAVAX_PORTLET_REQUEST);500)this.width=500'>500)this.width=500'>        RenderResponse renderResponse = (RenderResponse) request500)this.width=500'>                .getAttribute(WebKeys.JAVAX_PORTLET_RESPONSE);500)this.width=500'>500)this.width=500'>500)this.width=500'>        if (name == null) ...{500)this.width=500'>            return this.unspecified(mapping, form, portletConfig,500)this.width=500'>                    renderRequest, renderResponse);500)this.width=500'>        }500)this.width=500'>500)this.width=500'>        Method method = null;500)this.width=500'>500)this.width=500'>        try ...{500)this.width=500'>            method = getMethod(name);500)this.width=500'>500)this.width=500'>500)this.width=500'>        } catch (NoSuchMethodException e) ...{500)this.width=500'>            String message = messages.getMessage("dispatch.method",500)this.width=500'>                    mapping.getPath(), name);500)this.width=500'>            log.error(message, e);500)this.width=500'>500)this.width=500'>            String userMsg = messages.getMessage("dispatch.method.user",500)this.width=500'>                    mapping.getPath());500)this.width=500'>            throw new NoSuchMethodException(userMsg);500)this.width=500'>        }500)this.width=500'>500)this.width=500'>        ActionForward forward = null;500)this.width=500'>500)this.width=500'>        try ...{500)this.width=500'>500)this.width=500'>            Object args[] = ...{ mapping, form, portletConfig, renderRequest,500)this.width=500'>                    renderResponse };500)this.width=500'>            forward = (ActionForward) method.invoke(this, args);500)this.width=500'>500)this.width=500'>500)this.width=500'>        } catch (ClassCastException e) ...{500)this.width=500'>            String message = messages.getMessage("dispatch.return",500)this.width=500'>                    mapping.getPath(), name);500)this.width=500'>            log.error(message, e);500)this.width=500'>            throw e;500)this.width=500'>500)this.width=500'>500)this.width=500'>        } catch (IllegalAccessException e) ...{500)this.width=500'>            String message = messages.getMessage("dispatch.error",500)this.width=500'>                    mapping.getPath(), name);500)this.width=500'>            log.error(message, e);500)this.width=500'>            throw e;500)this.width=500'>500)this.width=500'>500)this.width=500'>        } catch (InvocationTargetException e) ...{500)this.width=500'>            Throwable t = e.getTargetException();500)this.width=500'>500)this.width=500'>            if (t instanceof Exception) ...{500)this.width=500'>                throw ((Exception) t);500)this.width=500'>500)this.width=500'>            } else ...{500)this.width=500'>                String message = messages.getMessage("dispatch.error",500)this.width=500'>                        mapping.getPath(), name);500)this.width=500'>                log.error(message, e);500)this.width=500'>                throw new ServletException(t);500)this.width=500'>            }500)this.width=500'>        }500)this.width=500'>        return (forward);500)this.width=500'>    }500)this.width=500'>500)this.width=500'>    protected String getMethodName(ActionMapping mapping, ActionForm form,500)this.width=500'>            HttpServletRequest request, HttpServletResponse response,500)this.width=500'>500)this.width=500'>            String parameter) throws Exception ...{500)this.width=500'>500)this.width=500'>        String methodName = request.getParameter(parameter);500)this.width=500'>500)this.width=500'>        if (methodName == null || methodName.length() == 0) ...{500)this.width=500'>             methodName = (String) request.getAttribute(parameter);500)this.width=500'>        }500)this.width=500'>        return methodName;500)this.width=500'>    }500)this.width=500'>500)this.width=500'>    public ActionForward unspecified(ActionMapping mapping, ActionForm form,500)this.width=500'>            PortletConfig config, RenderRequest req, RenderResponse res)500)this.width=500'>500)this.width=500'>            throws Exception ...{500)this.width=500'>        return null;500)this.width=500'>    }500)this.width=500'>    private static Log log = LogFactory.getLog(DispatchPortletAction.class);500)this.width=500'>500)this.width=500'>    protected Class[] types = ...{ ActionMapping.class, ActionForm.class,500)this.width=500'>            PortletConfig.class, RenderRequest.class, RenderResponse.class };500)this.width=500'>}500)this.width=500'>        这样后续多分发Action在书写的时候,只需要定义不同的方法即可,但是方法的参数需要依照如下规范,如下一个queryAll的方法:public ActionForward queryAll(ActionMapping mapping, ActionForm form,            PortletConfig config, RenderRequest req, RenderResponse res)            throws Exception {    //业务处理//返回ActionForward即可}          在那些portlet配置文件的view-action属性中,是不能够增加参数的,比如你不能够采用 /ext/reports/view_reports?cmd=queryAll这种方式。所以我们需要在扩展的Portlet中做一些拦截。可能有人会说,我不需要在初始的view -action中增加参数。事实上这个的确不是强制,如果不追加参数,则会访问unspecified方法。但是对于Portlet的显示,其 Normal和Max页面显示,都会请求默认的view-action。所以我们需要在Portlet类实现上扩展,于是扩展了一个 DispachStrutsPortlet,如下:public class DispachStrutsPortlet extends StrutsPortlet {       public void doView(RenderRequest req, RenderResponse res)                                                                    throws IOException, PortletException {                //注意我的命令参数是cmdx,而不是通常的cmd。                String cmd = req.getParameter("cmdx");                if(cmd==null || cmd.length()==0){                        if (req.getWindowState().equals(WindowState. MAXIMIZED)) {                        req.setAttribute("cmdx","queryAll");                }                super.doView(req, res);        }} 如上面的实现,则表示,如果Portlet是Normal页面状态请求的时候,则在view-action的时候,则仅访问默认的unspecified方法;如果是Max页面状态,则执行queryAll方法。 有一个需要注意的地方,由于“cmd”参数已经被Liferay使用,所以我们需要用另外的变量来表示方法。这里我采用的是“cmdx”。


阅读全文(1744) | 回复(1) | 编辑 | 精华
 



发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)



站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.455 second(s), page refreshed 144774560 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号