一、定義標(biāo)簽類
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: 參數(shù)傳遞標(biāo)簽</p>
?* <p>Description: 統(tǒng)一處理頁面間的參數(shù)傳遞</p>
?* @author Administrator
?*
?*/
public class ddd extends TagSupport {
? private String paraNames = null;? //參數(shù)串,以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 ;
? }
?
? /**
?? * 處理參數(shù)傳遞
?? * 規(guī)則:
?? * 1,如果不能在request范圍內(nèi)找到相應(yīng)name的參數(shù),設(shè)置值為WebConfig.NULL=""
?? * 2,如果atrribute,parameter中同時(shí)存在同名的參數(shù),以attribute中為主
?? * 3,將所有jsp頁面所需的參數(shù)轉(zhuǎn)換為attribute保存,在jsp頁面中通過<bean:write name="attributeName" />調(diào)用
?? * 4,涉及處理的參數(shù)包括: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);
??? }
? }
}
相關(guān)類
package com.taglib.pageparameter;
public class WebConfig{
???public static final String SEPARATE=";";//前臺(tái)web參數(shù)間隔離符號(hào)
???public final static String NULL = "";? //頁面間參數(shù)傳遞,name為空時(shí)設(shè)置的缺省值
}
二、標(biāo)簽定義tld文件
PageParameter.tld
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN"
"<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中加載標(biāo)簽
四、jsp頁面調(diào)用
<%@ page import="package com.taglib.pageparameter.WebConfig" %>
<%@ taglib uri="/WEB-INF/Archive/PageParameter.tld" prefix="PageParameter" %>
<PageParameter:ParameterTransact paraNames='<%="urlSQL"+WebConfig.SEPARATE+"page"%>'/>