自定義防重複提交標籤

標籤類

package taglib.html;

import java.io.IOException;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

public class TokenFormTag extends TagSupport {

	private static final long serialVersionUID = 1L;
	private String method = null;
	private String action = null;
	private String name = null;
	private String enctype = null;
	public String getMethod() {
		return method;
	}
	public void setMethod(String method) {
		this.method = method;
	}
	@Override
	public int doEndTag() throws JspException {
		
		try {
			pageContext.getOut().println("</form>");
		} catch (IOException e) {
			e.printStackTrace();
		}
		return EVAL_PAGE;
	}
	@Override
	public int doStartTag() throws JspException {
		HttpSession session=((HttpServletRequest)pageContext.getRequest()).getSession();
		String token=UUID.randomUUID().toString().toUpperCase();
		session.setAttribute("FORMSUBMITTOKEN", token);
		StringBuilder sbuilder=new StringBuilder("");
		sbuilder.append("<form");
		if(method!=null){
		sbuilder.append(" method=\""+method+"\" ");
		}
		if(action!=null){
			sbuilder.append(" action=\""+action+"\" ");			
		}
		if(name!=null){
			sbuilder.append(" name=\""+name+"\" ");	
		}
		if(enctype!=null){
			sbuilder.append(" enctype=\""+enctype+"\" ");			
		}
		sbuilder.append(">\n");
		sbuilder.append("<input type=\"hidden\" name=\"formToken\" value=\""+token+"\" />\n");
		try {
			pageContext.getOut().println(sbuilder.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
		return EVAL_BODY_INCLUDE;
	}
	public String getAction() {
		return action;
	}
	public void setAction(String action) {
		this.action = action;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getEnctype() {
		return enctype;
	}
	public void setEnctype(String enctype) {
		this.enctype = enctype;
	}
}

標籤定義

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE taglib
  PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
  "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
  <tlib-version>1.0</tlib-version>
  <jsp-version>1.2</jsp-version>
  <short-name>mytag</short-name>
  <uri>http://www.chinasofti.com/html</uri>
  <display-name>JSTL core RT</display-name>
  <description>JSTL 1.0 core library</description>
  <tag>
    <name>form</name>
    <tag-class>taglib.html.TokenFormTag</tag-class>
    <body-content>JSP</body-content>
    <description>
		My HTML Form tag
    </description>
    <attribute>
        <name>name</name>
        <required>false</required>
        <rtexprvalue>false</rtexprvalue>	
    </attribute>
     <attribute>
        <name>action</name>
        <required>false</required>
        <rtexprvalue>false</rtexprvalue>	
    </attribute>
     <attribute>
        <name>method</name>
        <required>false</required>
        <rtexprvalue>false</rtexprvalue>	
    </attribute>
     <attribute>
        <name>enctype</name>
        <required>false</required>
        <rtexprvalue>false</rtexprvalue>	
    </attribute>  
  </tag>
</taglib>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章