一、定义标签类
package com.taglib.pageparameter;
import javax.servlet.ServletRequest;
import javax.servlet.jsp.tagext.TagSupport;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
/**
* <p>Title: 参数传递标签</p>
* <p>Description: 统一处理页面间的参数传递</p>
* @author Administrator
*
*/
public class ddd extends TagSupport {
private String paraNames = null; //参数串,以WebConfig.SEPARATE分隔
public void setParaNames(String paraNames){
this.paraNames = paraNames ;
}
public String getParaNames(){
return this.paraNames;
}
public int doStartTag(){
if(this.paraNames==null)
return EVAL_BODY_INCLUDE;
String[] paras = this.paraNames.split(WebConfig.SEPARATE);
this.transactPara(pageContext,paras);
return EVAL_BODY_INCLUDE ;
}
public int doEndTag(){
return EVAL_PAGE ;
}
/**
* 处理参数传递
* 规则:
* 1,如果不能在request范围内找到相应name的参数,设置值为WebConfig.NULL=""
* 2,如果atrribute,parameter中同时存在同名的参数,以attribute中为主
* 3,将所有jsp页面所需的参数转换为attribute保存,在jsp页面中通过<bean:write name="attributeName" />调用
* 4,涉及处理的参数包括:String[] args
* @param pageContext PageContext
* @param args String[]
*/
public void transactPara(PageContext pageContext,String[] args){
for (int i = args.length ; --i >= 0;) {
ServletRequest request = pageContext.getRequest() ;
Object attr=(String)request.getAttribute(args[i]);
String para=request.getParameter(args[i]);
if(attr==null)
request.setAttribute(args[i], para==null?WebConfig.NULL:para);
}
}
}
相关类
package com.taglib.pageparameter;
public class WebConfig{
public static final String SEPARATE=";";//前台web参数间隔离符号
public final static String NULL = ""; //页面间参数传递,name为空时设置的缺省值
}
二、标签定义tld文件
PageParameter.tld
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN"
"http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">
<taglib>
<tlibversion>1.0</tlibversion>
<jspversion>1.1</jspversion>
<shortname>PageParameter</shortname>
<info></info>
<tag>
<name>ParameterTransact</name>
<tagclass>com.taglib.pageparameter.PageParameterTag</tagclass>
<bodycontent>empty</bodycontent>
<info></info>
<attribute>
<name>paraNames</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
三、在web.xml中加载标签
四、jsp页面调用
<%@ page import="package com.taglib.pageparameter.WebConfig" %>
<%@ taglib uri="/WEB-INF/Archive/PageParameter.tld" prefix="PageParameter" %>
<PageParameter:ParameterTransact paraNames='<%="urlSQL"+WebConfig.SEPARATE+"page"%>'/>