package org.onetwo.common.web.view.tag;

import javax.servlet.jsp.JspException;

import org.apache.commons.lang3.StringUtils;
import org.onetwo.apache.utils.URLEncoder;
import org.onetwo.common.web.utils.WebHolder;
import org.springframework.web.servlet.tags.form.TagWriter;

@SuppressWarnings("serial")
public class LinkTag extends BaseBodyTag {
	
	private String href;
	private boolean keepQueryString = true;
	
	public LinkTag() {
		super("a");
	}

	@Override
	public void writeCustomTagAttributes(TagWriter tagWriter) throws JspException {
		String linkAddress = href;
		
		if(keepQueryString){
			String qs = WebHolder.getSpringContextHolderRequest().get().getQueryString();
			if(StringUtils.isBlank(qs)){
				writeHref(tagWriter, linkAddress);
				return ;
			}
			linkAddress += linkAddress.contains("?")?"&":"?";
			linkAddress += qs;
		}
		writeHref(tagWriter, linkAddress);
	}
	
	private void writeHref(TagWriter tagWriter, String linkAddress) throws JspException{
		linkAddress = URLEncoder.SAFE_PARAMS_ENCODER.encode(linkAddress);
		tagWriter.writeAttribute("href", linkAddress);
	}

	public void setHref(String href) {
		this.href = href;
	}

	public void setKeepQueryString(boolean keepQueryString) {
		this.keepQueryString = keepQueryString;
	}

}

@@ -405,16 +405,51 @@
 = true;%0A
+%09private boolean encodeUrl = true;%0A
 %09%0A%09publi
@@ -1004,16 +1004,34 @@
 eption%7B%0A
+%09%09if(encodeUrl)%7B%0A%09
 %09%09linkAd
@@ -1090,16 +1090,20 @@
 dress);%0A
+%09%09%7D%0A
 %09%09tagWri
@@ -1312,10 +1312,91 @@
 ng;%0A%09%7D%0A%0A
+%09public void setEncodeUrl(boolean encodeUrl) %7B%0A%09%09this.encodeUrl = encodeUrl;%0A%09%7D%0A%0A
 %7D%0A

add encodeUrl property for LinkTag