package org.qfox.jestful.form;

import org.qfox.jestful.core.BeanContainer;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
import java.util.Enumeration;

/**
 * Created by yangchangpei on 17/8/21.
 */
public class TokenInput extends TagSupport {
    private static final long serialVersionUID = 8901103747024632711L;

    @Override
    public int doStartTag() throws JspException {
        try {
            BeanContainer beanContainer = (BeanContainer) pageContext.getServletContext().getAttribute(BeanContainer.ROOT_BEAN_CONTAINER_CONTEXT_ATTRIBUTE);
            TokenManager tokenManager = beanContainer.get(TokenManager.class);
            String token = tokenManager.create();

            if (getValue("type") == null) setValue("type", "hidden");
            if (getValue("name") == null) setValue("name", "form-token");
            if (getValue("value") == null) setValue("value", token);
            StringBuilder sb = new StringBuilder();
            sb.append("<input");

            Enumeration<String> names = getValues();
            while (names.hasMoreElements()) {
                String name = names.nextElement();
                Object value = getValue(name);
                if (value == null) continue;
                else sb.append(" ");
                sb.append(name).append("=").append("\"").append(value.toString()).append("\"");
            }

            sb.append("/>");
            pageContext.getOut().write(sb.toString());
            return SKIP_BODY;
        } catch (Exception e) {
            throw new JspException(e);
        }
    }

}

@@ -145,48 +145,123 @@
 ext.
-TagSupport;%0Aimport java.util.Enumeration
+DynamicAttributes;%0Aimport javax.servlet.jsp.tagext.TagSupport;%0Aimport java.util.LinkedHashMap;%0Aimport java.util.Map
 ;%0A%0A/
@@ -349,16 +349,45 @@
 Support 
+implements DynamicAttributes 
 %7B%0A    pr
@@ -452,16 +452,105 @@
 2711L;%0A%0A
+    private final Map%3CString, Object%3E attributes = new LinkedHashMap%3CString, Object%3E();%0A%0A
     @Ove
@@ -926,187 +926,226 @@
 if (
-getValue(%22type%22) == null) setValue(%22type%22, %22hidden%22);%0A            if (getValue(%22name%22) == null) setValue(%22name%22, %22form-token%22);%0A            if (getValue(%22value%22) == null) setValue
+!attributes.containsKey(%22name%22)) attributes.put(%22name%22, %22form-token%22);%0A            if (!attributes.containsKey(%22type%22)) attributes.put(%22type%22, %22hidden%22);%0A            if (!attributes.containsKey(%22value%22)) attributes.put
 (%22va
@@ -1158,16 +1158,17 @@
 token);%0A
+%0A
         
@@ -1261,89 +1261,66 @@
     
-Enumeration%3CString%3E names = getValues();%0A            while (names.hasMoreElements
+for (Map.Entry%3CString, Object%3E entry : attributes.entrySet
 ()) 
@@ -1355,25 +1355,20 @@
 e = 
-names.nextElement
+entry.getKey
 ();%0A
@@ -1398,16 +1398,22 @@
 value = 
+entry.
 getValue
@@ -1413,20 +1413,16 @@
 etValue(
-name
 );%0A     
@@ -1811,10 +1811,276 @@
 %0A    %7D%0A%0A
+    @Override%0A    public void setDynamicAttribute(String uri, String localName, Object value) throws JspException %7B%0A        attributes.put(localName, value);%0A    %7D%0A%0A    @Override%0A    public void release() %7B%0A        attributes.clear();%0A        super.release();%0A    %7D%0A
 %7D%0A

Add Anti Form Repeat Submit Supports