tapestry自定义基本IEngineService

news/2024/8/26 18:52:39

tapestry自定义基本IEngineService

                                      

 

   tapestry的SeviceLink组件能调用9种基本的service(ActionService, AssetService, DirectService, EngineServiceInnerProxy, EngineServiceOuterProxy, ExternalService, HomeService, PageService, ResetService, RestartService)。如果这9种基本service不能满足需求,那可以使用自定义service来代替基本service,下面将讲述如何创建自定义service:

   举例:有这样一个需求,要求用户对出的操作,会注销掉session中的数据,并且删除用户的cookie。
    首先,新建立一个类,实现IEngineService接口。

public class LogoutService implements IEngineService {
    /** @since 4.0 *hivemind注射基本服务/
    private Log _log;

    /** @since 4.0 *hivemind注射基本服务/
    private HttpServletRequest _request;

    /** @since 4.0 *hivemind注射基本服务/
    private HttpServletResponse _response;

    /** @since 4.0 *hivemind注射基本服务/
    private LinkFactory _linkFactory;

    /** @since 4.0 */
    private String _servletPath;
   
   
    @SuppressWarnings("unchecked")
    public ILink getLink(boolean post, Object parameter)
    {
        //该服务允许带参数,所以屏蔽以下两句,若不允许带参数取消注释
        // if (parameter != null)
        // throw new IllegalArgumentException(EngineMessages.serviceNoParameter(this));

        Map parameters = new HashMap();
        //key必须为ServiceConstants所指定的key,否则会有异常
        parameters.put(ServiceConstants.SERVICE, getName());
        //其中parameter参数为,@ServiceLink组件的parameters
        parameters.put(ServiceConstants.PARAMETER,parameter);
        return _linkFactory.constructLink(this, post, parameters, true);
    }

   
    public void service(IRequestCycle cycle) throws IOException
    {
        String serviceName = cycle.getParameter(ServiceConstants.SERVICE);
        System.out.println(serviceName);
        HttpSession session = _request.getSession(false);
       
        //注销清除cookie
        CookieSource cookieSource=cycle.getInfrastructure().getCookieSource();
        //获得@ServiceLink组件的parameters
        Object[] obj=_linkFactory.extractListenerParameters(cycle);
        for (Object object : obj) {
            cookieSource.removeCookieValue((String)object);
        }
       
        if (session != null)
        {
            try
            {
                session.invalidate();
            }
            catch (IllegalStateException ex)
            {
                _log.warn("Exception thrown invalidating HttpSession.", ex);

                // Otherwise, ignore it.
            }
        }

        String url = cycle.getAbsoluteURL(_servletPath);

        _response.sendRedirect(url);
    }

    //指定该service的名字
    public String getName()
    {
        return "logout";
    }

    /** @since 4.0 */
    public void setLog(Log log)
    {
        _log = log;
    }

    /** @since 4.0 */
    public void setRequest(HttpServletRequest request)
    {
        _request = request;
    }

    /** @since 4.0 */
    public void setResponse(HttpServletResponse response)
    {
        _response = response;
    }

    /** @since 4.0 */
    public void setLinkFactory(LinkFactory linkFactory)
    {
        _linkFactory = linkFactory;
    }

    /** @since 4.0 */
    public void setServletPath(String servletPath)
    {
        _servletPath = servletPath;
    }
}

hivemind配置如下:
      <service-point id="LogoutService" interface="org.apache.tapestry.engine.IEngineService">
        <invoke-factory service-id="hivemind.BuilderFactory" model="singleton" >
            <construct class="impl.FenixLogoutService" >
                <set-service property="request" service-id="tapestry.globals.HttpServletRequest" />
                <set-service property="response" service-id="tapestry.globals.HttpServletResponse" />
                <set-object property="servletPath" value="app-property:org.apache.tapestry.servlet-path" />
                <set-object property="linkFactory" value="infrastructure:linkFactory" />
            </construct>
        </invoke-factory>
    </service-point>

    <contribution configuration-id="tapestry.services.ApplicationServices">
        <service name="logout" object="service:LogoutService"/>
    </contribution>

使用就简单了:
<a jwcid="@ServiceLink" service="logout" parameters="ognl:cookieList">退出</a>


http://www.niftyadmin.cn/n/1996046.html

相关文章

一个失败的案例

最近有一个新的项目团队在开发一个新的功能&#xff0c;并且决定尝试用Scrum&#xff0c;非常有幸被邀请加入&#xff0c;担任ScrumMaster。一开始的时候一切似乎都很顺利&#xff0c;我们召开了一次项目的启动会议&#xff0c;请负责的产品经理明确对结果的期望&#xff0c;并…

static_cast揭密

static_cast揭密 本文讨论static_cast<> 和 reinterpret_cast<>。 介绍大多程序员在学C前都学过C&#xff0c;并且习惯于C风格&#xff08;类型&#xff09;转换。当写C&#xff08;程序&#xff09;时&#xff0c;有时候我 们在使用static_cast<>和reinter…

POI操作Excel一些中文问题的解决方法

1. 设置工作表名 &#xff08;下文中提到的workbook为类org.apache.poi.hssf.usermodel.HSSFWorkbook的一个实例&#xff09; 如果使用workbook.createSheet(sheetname)或者使用workbook.setSheetName(1, "中文")&#xff0c;默认会使用ENCODING_COMPRESSED_U…

02-CSS基础与进阶-day9_2018-09-12-21-27-10

z-index 当对多个元素设置定位时,重叠的定位元素可以通过z-index调整堆叠顺序 其值可以为0 正数 负数 特点 1 z-index默认值为0 取值越大 定位元素在层叠元素上越局上 2 z-index取值一样&#xff0c;后来居上 3 z-index值不能加单位 4 只有定位元素才有该属性&#xff0c;其余如…

讨论static_cast 和 reinterpret_cast

讨论static_cast 和 reinterpret_cast 作者&#xff1a;Sam NG 译者&#xff1a;小刀人 原文链接&#xff1a;What static_cast<> is actually doing 本文讨论static_cast<> 和 reinterpret_cast<>。 介绍大 多程序员在学C前都学过C&#xff0c;并且…

awk调用date命令

创建文件date.awk&#xff1a; $8 107582685 { cmd "date %Y-%m-%d-%H -d \"" $11 "\""while (cmd | getline line) {print line}close(cmd)} 调用命令: awk -f date.awk *.log | sort | uniq -dc | sort -nr sort参数: -n // 以数值来排…

VC6调用WebService

VC6调用WebService 作者&#xff1a;未知 来源&#xff1a;月光软件站 加入时间&#xff1a;2005-2-28 月光软件站 下面是个控制台的样例Toolkit3.0 终于给出VC6的样例了&#xff0c;1.0只能看到VB和ASP的 #include <stdio.h> #import "msxml4.dll" using…

Jakata Poi HSSF:纯java的Excel解决方案

Jakata Poi HSSF&#xff1a;纯java的Excel解决方案JCoder 转贴 (参与分&#xff1a;32257&#xff0c;专家分&#xff1a;1325) 发表&#xff1a;2005-07-15 15:57 版本&#xff1a;1.0 阅读&#xff1a;2121次 微软在桌面系统上的成功&#xff0c;令我们不得不大量使…