技术开发 频道

在Tapestry中使用redirect-after-post模式控制表单提交


IT168技术文档】 
    首先是定义注释类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(); }
    然后写注释的worker类
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的同样两个类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(); }

0
相关文章