我现在自定一个directive,可以拿到一个context,但是貌似这个context中无法获取request。请问有没有办法获取这个request?
听说@红薯 是velocity大牛,特求帮助!
详细代码如下:
package com.mugu.framework.web.view.velocity.directive; import java.io.IOException; import java.io.Writer; import org.apache.velocity.context.InternalContextAdapter; import org.apache.velocity.exception.MethodInvocationException; import org.apache.velocity.exception.ParseErrorException; import org.apache.velocity.exception.ResourceNotFoundException; import org.apache.velocity.runtime.directive.Directive; import org.apache.velocity.runtime.parser.node.Node; import org.apache.velocity.tools.view.ViewToolContext; public abstract class AbstractDirective extends Directive { @Override public boolean render(InternalContextAdapter internalContext, Writer writer, Node node) throws IOException, ResourceNotFoundException, ParseErrorException, MethodInvocationException { ViewToolContext context = (ViewToolContext)internalContext.getInternalUserContext(); return doRender(internalContext, context, writer, node); } protected abstract boolean doRender(InternalContextAdapter internalContext,ViewToolContext context, Writer writer, Node node) throws IOException, ResourceNotFoundException, ParseErrorException, MethodInvocationException ; }
我自己定义得指令是:
package com.mugu.framework.web.view.velocity.directive; import java.io.IOException; import java.io.Writer; import org.apache.velocity.context.InternalContextAdapter; import org.apache.velocity.exception.MethodInvocationException; import org.apache.velocity.exception.ParseErrorException; import org.apache.velocity.exception.ResourceNotFoundException; import org.apache.velocity.runtime.parser.node.Node; import org.apache.velocity.runtime.parser.node.SimpleNode; import org.apache.velocity.tools.view.ViewToolContext; import org.tuckey.web.filters.urlrewrite.UrlRewriteWrappedResponse; public class UrlDirective extends AbstractDirective { @Override public String getName() { return "url"; } @Override public int getType() { return LINE; } @Override protected boolean doRender(InternalContextAdapter internalContext, ViewToolContext context, Writer writer, Node node) throws IOException, ResourceNotFoundException, ParseErrorException, MethodInvocationException { // get url SimpleNode sn = (SimpleNode) node.jjtGetChild(0); String url = (String)sn.value(internalContext); // get context path String contextPath = context.getRequest().getContextPath(); if("/".equals(contextPath)){ contextPath = ""; } if(context.getResponse() instanceof UrlRewriteWrappedResponse){ url = context.getResponse().encodeURL(url); }else{ url = context + url; } writer.write(url); return true; } }
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
暂无简介
文章 0 评论 0
接受
发布评论
评论(1)
详细代码如下:
我自己定义得指令是: