Commits

Anonymous committed 3026590

implemented TokenTag functionality for Velocity

git-svn-id: http://svn.opensymphony.com/svn/webwork/trunk@66573baa09-0c28-0410-bef9-dab3c582ae83

Comments (0)

Files changed (1)

src/java/com/opensymphony/webwork/views/jsp/ui/TokenTag.java

     }
 
     /**
- * First looks for the token in the PageContext using the supplied name (or {@link TokenHelper#DEFAULT_TOKEN_NAME}
- * if no name is provided) so that the same token can be re-used for the scope of a request for the same name. If
- * the token is not in the PageContext, a new Token is created and set into the Session and the PageContext with
- * the name.
- * @return
- * @throws JspException
- */
+* First looks for the token in the PageContext using the supplied name (or {@link TokenHelper#DEFAULT_TOKEN_NAME}
+* if no name is provided) so that the same token can be re-used for the scope of a request for the same name. If
+* the token is not in the PageContext, a new Token is created and set into the Session and the PageContext with
+* the name.
+* @return
+* @throws JspException
+*/
     public int doEndTag() throws JspException {
         if (name == null) {
             name = TokenHelper.DEFAULT_TOKEN_NAME;
     }
 
     /**
- * Clears all the instance variables to allow this instance to be reused.
- */
+* Clears all the instance variables to allow this instance to be reused.
+*/
     public void release() {
         super.release();
         token = null;
     }
 
     public void render(Context context, Writer writer) throws Exception {
-        /**
- * todo - look for a better way of implementing this.  this smells bad.
- */
-        verifySession();
-        this.token = TokenHelper.setToken(name.toString());
+        if (name == null) {
+            name = TokenHelper.DEFAULT_TOKEN_NAME;
+        }
+
+        token = buildTokenForVelocity(context, name.toString());
 
         super.render(context, writer);
     }
         return myToken.toString();
     }
 
+    private String buildTokenForVelocity(Context context, String name) {
+        Object myToken = context.get(name);
+
+        if (myToken == null) {
+            verifySession();
+            ;
+            myToken = TokenHelper.setToken(name.toString());
+            context.put(name, myToken);
+        }
+
+        return myToken.toString();
+    }
+
     /**
- * This method checks to see if a HttpSession object exists in the context. If a session
- * doesn't exist, it creates a new one and adds it to the context.
- */
+* This method checks to see if a HttpSession object exists in the context. If a session
+* doesn't exist, it creates a new one and adds it to the context.
+*/
     private void verifySession() {
         Map session = ServletActionContext.getContext().getSession();