VelocityはRequestの要求URLを取得する.

4943 ワード

プロジェクトはVelocityを使って主体内容をレンダリングします.大きな枠はやはりJSPを使います.
全体的にはJSP+Velocityです.
JSPの使用を要求し、非同期にVMを使用する.
何を使っていますか?Velocityはfreemarkerを使いません.プロジェクトが古いので、修正が面倒くさいです.
今は需要があります.プロジェクトの生産とUATが使っている画像の経路は違っています.
このようにしてVelocityテンプレートをレンダリングするには、どのバージョンのリソースを使用するかを判断する属性が必要です.
JSPはとても良い判断をしています.HttpServletRequestを得るために、直接にRequest URLの方法を取ればいいです.
しかし、VelocityではRequestは取得できません.URLをパラメータとして渡すべきですか?
その修正コードの量は想像できません.
ほとんどのVelocityテンプレートには共通のヘッダがあります.このヘッダには共通の情報が保存されています.
この頭にURLが入ってきたらいいです.
資料を見て、ネット上でレセプトのアイデアを紹介してくれました.
コマンドでHttpServletRequestを取得し、変数にURLを保存すればいいです.次のクラスを設計しました.
package com.itheima.util;

import java.io.IOException;
import java.io.Writer;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
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.springframework.web.servlet.support.RequestContext;

/**
 *     ,   velocity    .
 *     url       
 * @author   
 * @time 2017 7 11    3:25:37 
 */
public class UrlObtain extends Directive {
	//              ,
	private boolean canSecurity = true;
	
	@Override
	public String getName() {
		return "UrlObtain";
	}

	// Block      #end   Line   
	@Override
	public int getType() {
		return LINE;
	}

	@Override
	public boolean render(InternalContextAdapter context,
            Writer writer, Node node )
			throws IOException, ResourceNotFoundException, ParseErrorException,
			MethodInvocationException {
		//         
		String requestUrl = "";
		int count = node.jjtGetNumChildren();
		//        url
		if(count > 0) {
			Node jjtGetChild = node.jjtGetChild(0);
			if(jjtGetChild != null) {
				requestUrl =  (String) jjtGetChild.value(context);
			}
		}
		//      
		if(count > 1) {
			Node jjtGetChild = node.jjtGetChild(1);
			if(jjtGetChild != null) {
				String securityValue =  (String) jjtGetChild.value(context);
				canSecurity = Boolean.valueOf(securityValue);
			}
		}
		RequestContext requestContext = (RequestContext) context.get("rc");
		if(requestContext == null) {
			throw new ResourceNotFoundException("please open velocity requestContextAttribute set value rc");
		}
		
		//          url,          ,     .
		if(StringUtils.isEmpty(requestUrl) && !canSecurity) {
			throw new IOException("Either set the default URL, or open the reflect");
		}
		HttpServletRequest request = null;
		if(canSecurity) {
			
			try {
				
				Method method = requestContext.getClass().getDeclaredMethod("getRequest", null);
				method.setAccessible(true);
				request = (HttpServletRequest) method.invoke(requestContext, null);
				
			} catch (NoSuchMethodException e) {
				e.printStackTrace();
			} catch (SecurityException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			}
		}
		
		//     url
		if(request != null) {
			requestUrl = request.getRequestURL().toString();
		}
		
		context.put("RequestURL", requestUrl);
		//requestContext.getModel().put("RequestURL", requestUrl);
		
		return true;
	}
	

}
󑧙萼UrlObtainは二つのパラメータを持っています.デフォルトではurlアドレスと反射(デフォルトでは使用)結果はRequest URL変数に保存されます.
リキッドURL変数は、反射を使用しないとデフォルトurlアドレスとなります.
璣璖菗はデフォルトurlを伝えず、反射を禁止すると、異常を投げかける.
萼菘UrlObtainはビュー解像器を開いてrequest Contact Attribute属性を開いてrcと名づけます.
シシシシシシシシシシシシ不ゾ出エ.
萼葃UrlObtain(「wn.baidu.com」)
このように、Request URLは現在のURLにアクセスできます.
反射してRequestを得ることができるなら、Reponseももらえます.
また、RequestとReponseを変数としてVelocityテンプレートに送ることもできます.
注意したいのは、ビュー解像度の設定です.
	
		
		
		
		
		
		
		
		
		
			
				test
			
		
		
		
		
	
end