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>