struts的include标签的修改,使支持http形式

[b]本文需要读者对struts的标签源码比较熟悉。[/b]

由于公司的web应用根据不同的业务形式采用的独立部署。使各个业务的耦合度降低。使业务更加的灵活。
但是面临的是业务WEB_A应用需要调用业务WEB_B应用的页面。
[list]
[*]1:ajax的形式。
使用下来效果也不错,满足了要求。
但是面临的问题也来了。最最重要的是SEO,由于这块是异步加载的。由于WEB_A的主体是WEB_B所以,所以WEB_A基本上是页面无变动的。
[*]2:action中用java拿到页面的IO流。输出到页面上。
但是,这样的情况下,就会使开发要写多余的重复的代码。
[*]3:代码、页面写在WEB_A上。但是这样的话,就使的开发需要维护两个业务工程。
[*]4:[color=red]如有其他方案请联系我:)[/color]
[/list]

后来在研究struts的s:include标签的时候,感觉也可以用自定义标签来解决类似问题。
附上代码:
中间的标签的配置省略了。

/**
* 获取http请求内容
*
* @author shanzhu
* @version 1.0 2011-9-15
*/
package org.apache.struts2.views.jsp;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.components.Component;
import org.apache.struts2.components.IncludeHttp;

import com.opensymphony.xwork2.util.ValueStack;

public class IncludeHttpTag extends ComponentTagSupport{

/**
*
*/
private static final long serialVersionUID = 4811111257523224901L;
protected String value;
protected String charset; //编码
protected int conntimeout;
protected int requtimeout;


public Component getBean(ValueStack stack, HttpServletRequest req, HttpServletResponse res) {
return new IncludeHttp(stack, req, res);
}

protected void populateParams() {
super.populateParams();

((IncludeHttp) component).setValue(value);
((IncludeHttp) component).setCharset(charset);
((IncludeHttp) component).setConntimeout(conntimeout);
((IncludeHttp) component).setRequtimeout(requtimeout);
}


public void setValue(String value) {
this.value = value;
}

public void setCharset(String charset) {
this.charset = charset;
}

public void setConntimeout(int conntimeout) {
this.conntimeout = conntimeout;
}

public void setRequtimeout(int requtimeout) {
this.requtimeout = requtimeout;
}
}



/**
* includehttp的时候。处理value是http开头。
* 当使用这个标签的时候,会根据标签内的值取得url的io,写在页面上
*
* @author shanzhu
* @version 1.0 2011-9-15
*/
package org.apache.struts2.components;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.Writer;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.StrutsConstants;
import org.apache.struts2.components.Component;
import org.apache.struts2.views.annotations.StrutsTag;
import org.apache.struts2.views.annotations.StrutsTagAttribute;

import com.opensymphony.xwork2.inject.Inject;
import com.opensymphony.xwork2.util.ValueStack;


@StrutsTag(name="IncludeHttp", tldTagClass="org.apache.struts2.components.IncludeHttp", description="Include a servlet's output " +
"(result of http context)")
public class IncludeHttp extends Component {

private final String URL_AND = "&";
private final String URL_DENGYU = "=";
private final String URL_WENHAO = "?";
private final String STR_KONG = "";
private static final Log _log = LogFactory.getLog(IncludeHttp.class);

protected String value; //url
protected String charset; //以charset编码去取得远程的io
protected int conntimeout; //超时时间
protected int requtimeout; //超时时间
private String wenhao ;
private String strParmaeters = "";
private HttpServletRequest req;
private HttpServletResponse res;
private static String defaultEncoding;


public IncludeHttp(ValueStack stack, HttpServletRequest req, HttpServletResponse res) {
super(stack);
this.req = req;
this.res = res;
}

@Inject(StrutsConstants.STRUTS_I18N_ENCODING)
public static void setDefaultEncoding(String encoding) {
defaultEncoding = encoding;
}

public boolean end(Writer writer, String body) {
checkURL();
value = value + this.wenhao + strParmaeters;
String webContext = GetWebContent(value,charset,conntimeout,requtimeout);
return super.end(writer, webContext);
}
/**
* 对url进行检查拼装
*/
private void checkURL(){
checkUrlSameStatus();
checkParmater();
}

/**
* 对?进行检查
*/
private void checkUrlSameStatus(){
if (this.value.indexOf(this.URL_WENHAO)>-1) {
wenhao = STR_KONG;
}else {
wenhao = this.URL_WENHAO;
}
}
/**
* 对参数进行检查
*/
private void checkParmater(){
if (this.parameters==null || this.parameters.size()<=0) {
strParmaeters = STR_KONG;
}else {
Iterator<?> it = parameters.entrySet().iterator();
while (it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();
if (strParmaeters.indexOf("&")>-1) {
strParmaeters = strParmaeters + this.URL_AND+entry.getKey() + this.URL_DENGYU + ((List)entry.getValue()).get(0);
}else {
strParmaeters = strParmaeters +entry.getKey() + this.URL_DENGYU + ((List)entry.getValue()).get(0);
}

}
}
}




@StrutsTagAttribute(description="The jsp/servlet output to include", required=true)
public void setValue(String value) {
this.value = value;
}

/**
* 取得includehttp标签内使用的parameter标签的值
*/
@SuppressWarnings("unchecked")
public void addParameter(String key, Object value) {
// don't use the default implementation of addParameter,
// instead, include tag requires that each parameter be a list of objects,
// just like the HTTP servlet interfaces are (String[])
if (value != null) {
List currentValues = (List) parameters.get(key);

if (currentValues == null) {
currentValues = new ArrayList();
parameters.put(key, currentValues);
}
currentValues.add(value);
}
}

public void setCharset(String charset) {
if (isEmpty(charset)) {
charset = "utf-8";
}
this.charset = charset;
}

public void setConntimeout(int conntimeout) {
if (conntimeout<=0) {
conntimeout = 3000;
}
this.conntimeout = conntimeout;
}

public void setRequtimeout(int requtimeout) {
if (requtimeout<=0) {
requtimeout = 3000;
}
this.requtimeout = requtimeout;
}

boolean isEmpty(String str){
if (str == null || "".equals(str)) {
return true;
}
return false;
}



/*
* 取得指定URL的Web内容
*/
public static String GetWebContent(final String theURL,final String charset,final int connTimeout,final int requTimeout) {
String sTotalString = "";
URL l_url = null;
HttpURLConnection l_connection = null;
java.io.InputStream l_urlStream = null;
BufferedReader l_reader = null;
try {
l_url = new URL(theURL);
l_connection = (HttpURLConnection) l_url.openConnection();
l_connection.setConnectTimeout(connTimeout);
// 加入取内容超时处理
l_connection.setReadTimeout(requTimeout);
l_connection.connect();
l_urlStream = l_connection.getInputStream();
l_reader = new BufferedReader(new InputStreamReader(l_urlStream,charset));
int buffer_size = 1024;
char[] buffer = new char[buffer_size];
StringBuffer sb = new StringBuffer();
int readcount = 0;
while ((readcount = l_reader.read(buffer, 0, buffer_size)) > 0) {
sb.append(buffer, 0, readcount);
}
sTotalString = sb.toString();
l_reader.close();
l_urlStream.close();
l_connection.disconnect();
} catch (Exception e) {
System.out.println("error: exception in WebUtil: " + e.toString()
+ ":" + theURL);
} finally {
if (l_reader != null) {
try {
l_reader.close();
} catch (Exception e) {
}
}
if (l_urlStream != null) {
try {
l_urlStream.close();
} catch (Exception e) {
}
}
if (l_connection != null) {
try {
l_connection.disconnect();
} catch (Exception e) {
}
}
}

if (sTotalString==null) {
sTotalString = "";
}
return sTotalString;
}


}



这个时候。调用方仅仅需要使用标签即可。
举个例子:
<s:includehttp value="http://www.aaa.html?a=1">
<s:param name="b" value="'b'"></s:param>
</s:includehttp>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值