在Tapestry中使用redirect-after-post模式控制表单提交 博客分类: Tapestry4 Tapestry设计模式ApacheOOAjax
程序员文章站
2024-02-20 18:05:16
...
Tapestry中表单的提交有很多问题,刷新导致表单的重复提交、臭名昭著的dirty form warning、
显示不友好的URL等,这些都可以使用redirect-after-post模式来避免,也就是post表单之后将页面redirect一下,
这样地址栏显示的就是redirect之后的页面,刷新的也将是这个页面,而且redirect之后的页面一般为Page页面,
可以使用friendlyUrl来显示友好的url。<o:p></o:p>
要使用这个模式,不得不提到一个ILink接口,T4中可接受的listener方法中一个方法签名是这样的:<o:p></o:p>
java 代码
- public ILink listenerMethod(Parameter parameter);
这个方法的返回值为ILink,如果T4侦测到是这个返回值的话,将自动redirect到ILink指定的页面。
而且ILink可以为纯页面的Link也可以是带有参数的Link。纯页面Link也就是page service生成的link,
带参数的link为external service生成的link,具体link的生成代码如下:<o:p></o:p>
java 代码
- @InjectObject("engine-service:page")
- public abstract IEngineService getPageService();
- public ILink getPageLink(String pageName) {
- return getPageService().getLink(false, pageName);
- }
- @InjectObject("engine-service:external")
- public abstract IEngineService getExternalService();
- public ILink getExternalLink(String pageName, Object[] parameters) {
- ExternalServiceParameter esp = new ExternalServiceParameter(pageName,
- parameters);
- return getExternalService().getLink(false, esp);
- }
将上述方法放入到你的项目中自定义的MyBasePage(继承Tapestry的BasePage)中,然后让你的
页面类继承MyBasePage,这样就可以使用上述方法了。<o:p></o:p>
这样在页面提交时可以如下调用
java 代码
- public ILink onDelete(Long id){
- //your code to delete object
- return getPageLink(getPageName());//删除后重新redirect到本页面
- }
- 当然如果这个页面带参数,例如catgoryId=1,则代码如下:
- public ILink onDelete(Long id){
- //your code to delete object
- return getExternalLink(getPageName(),new Object[]{categoryId});
- //删除后重新redirect到本页面,并带一个参数
- }
上述方式通过继承来实现方法调用,感觉不是很好,我们可以利用Tapestry的易扩展特性来扩展它,
首先想到的就是注释方式来注入,例如我想实现如下注入
java 代码
- @InjectPageLink(“Home“)
- Public abstract ILink getHomeLink();
- @InjectExternalLink(“Category“)
- Public abstract ILink getCategoryLink(Long categoryId);
这样即去掉了页面的继承,也显得比较直接。
首先是定义注释类
java 代码
- import java.lang.annotation.ElementType;
- import java.lang.annotation.Retention;
- import java.lang.annotation.RetentionPolicy;
- import java.lang.annotation.Target;
- @Target( { ElementType.METHOD })
- @Retention(RetentionPolicy.RUNTIME)
- public @interface InjectPageLink {
- String value();
- }
<o:p></o:p>
然后写注释的worker类
<o:p></o:p>
java 代码
- import java.lang.reflect.Method;
- import java.lang.reflect.Modifier;
- import org.apache.hivemind.ApplicationRuntimeException;
- import org.apache.hivemind.Location;
- import org.apache.hivemind.service.BodyBuilder;
- import org.apache.hivemind.service.MethodSignature;
- import org.apache.tapestry.annotations.AnnotationUtils;
- import org.apache.tapestry.annotations.MethodAnnotationEnhancementWorker;
- import org.apache.tapestry.engine.ILink;
- import org.apache.tapestry.enhance.EnhancementOperation;
- import org.apache.tapestry.spec.IComponentSpecification;
- public class InjectPageLinkAnnotationWorker implements
- MethodAnnotationEnhancementWorker {
- public void performEnhancement(EnhancementOperation op,
- IComponentSpecification spec, Method method, Location location) {
- if (!method.getReturnType().equals(ILink.class))
- throw new ApplicationRuntimeException(
- "InjectPage annotation must return ILink");
- InjectPageLink injectPageLink = method
- .getAnnotation(InjectPageLink.class);
- String pageName = injectPageLink.value();
- BodyBuilder builder = new BodyBuilder();
- builder.begin();
- builder
- .addln(
- "return getPage().getRequestCycle().getInfrastructure().getServiceMap().getService(
- org.apache.tapestry.Tapestry.PAGE_SERVICE).getLink(false,\"{0}\");",
- pageName);
- builder.end();
- op.addMethod(Modifier.PUBLIC, new MethodSignature(method), builder
- .toString(), location);
- if (isGetter(method))
- op.claimReadonlyProperty(AnnotationUtils.getPropertyName(method));
- }
- boolean isGetter(Method method) {
- return method.getName().startsWith("get")
- && method.getParameterTypes().length == 0;
- }
- }
External的同样两个类<o:p></o:p>
java 代码
- import java.lang.annotation.ElementType;
- import java.lang.annotation.Retention;
- import java.lang.annotation.RetentionPolicy;
- import java.lang.annotation.Target;
- @Target( { ElementType.METHOD })
- @Retention(RetentionPolicy.RUNTIME)
- public @interface InjectExternalLink {
- String value();
- }
<o:p></o:p>
Work类java 代码
- import java.lang.reflect.Method;
- import java.lang.reflect.Modifier;
- import org.apache.hivemind.ApplicationRuntimeException;
- import org.apache.hivemind.Location;
- import org.apache.hivemind.service.BodyBuilder;
- import org.apache.hivemind.service.MethodSignature;
- import org.apache.tapestry.Tapestry;
- import org.apache.tapestry.annotations.AnnotationUtils;
- import org.apache.tapestry.annotations.MethodAnnotationEnhancementWorker;
- import org.apache.tapestry.engine.ILink;
- import org.apache.tapestry.enhance.EnhancementOperation;
- import org.apache.tapestry.spec.IComponentSpecification;
- public class InjectExternalLinkAnnotationWorker implements
- MethodAnnotationEnhancementWorker {
- public void performEnhancement(EnhancementOperation op,
- IComponentSpecification spec, Method method, Location location) {
- if (!method.getReturnType().equals(ILink.class))
- throw new ApplicationRuntimeException(
- "injectExternalLink annotation must return ILink");
- InjectExternalLink injectExternalLink = method
- .getAnnotation(InjectExternalLink.class);
- String pageName = injectExternalLink.value();
- BodyBuilder builder = new BodyBuilder();
- builder.begin();
- Class[] parameterTypes = method.getParameterTypes();
- int paramCount = Tapestry.size(parameterTypes);
- if (paramCount > 0) {
- if (parameterTypes[0].isArray()) {
- builder
- .addln(
- "return getPage().getRequestCycle().getInfrastructure().getServiceMap().
- getService(org.apache.tapestry.Tapestry.EXTERNAL_SERVICE).getLink(false, new org.apache.tapestry.engine.ExternalServiceParameter(\"{0}\",$1));",
- pageName);
- } else {
- builder
- .addln(
- "java.lang.Object[] params = new java.lang.Object[{0}];",
- paramCount);
- for (int i = 0; i < paramCount; i++) {
- builder.add("params[{0}] = ", i);
- if (parameterTypes[i].isPrimitive())
- builder.add("($w) ");
- // Parameter $0 is this, so $1 is the first real parameter.
- builder.addln("${0};", i + 1);
- }
- builder
- .addln(
- "return getPage().getRequestCycle().getInfrastructure().getServiceMap().getService(org.apache.tapestry.Tapestry.EXTERNAL_SERVICE).getLink(false, new org.apache.tapestry.engine.ExternalServiceParameter(\"{0}\",params));",
- pageName);
- }
- }
- builder.end();
- op.addMethod(Modifier.PUBLIC, new MethodSignature(method), builder
- .toString(), location);
- if (isGetter(method))
- op.claimReadonlyProperty(AnnotationUtils.getPropertyName(method));
- }
- boolean isGetter(Method method) {
- // We already know the return type is String
- return method.getName().startsWith("get")
- && method.getParameterTypes().length == 0;
- }
- }
<o:p></o:p>
类写好了,然后就是贡献到Tapestry中<o:p></o:p>
xml 代码
- <module id="your.model.annotation" version="1.0.0" package="your.package.annotations">
- <contribution configuration-id="tapestry.annotation.MethodWorkers">
- <worker annotation="InjectPageLink" object="instance:InjectPageLinkAnnotationWorker"/>
- <worker annotation="InjectExternalLink" object="instance:InjectExternalLinkAnnotationWorker"/>
- <!---->contribution>
- <!---->module>
<o:p></o:p>
好了将你的module放入项目hivemodule.Xml作为子module。<o:p></o:p>Ok,enjoy it!
<o:p></o:p>
<o:p> </o:p>