Is it possible to set a struts token with a GUID as a token name?

本秂侑毒 提交于 2019-12-08 02:25:31

问题


I'm trying to set a struts token in a form with a unique name for each form. Is there way to set up the token in a way like <s:token name="<some random generated characters>". I was planning to use the TokenHelper.generateGUID() to generate the token name, if possible. I've tried setting a variable using <s:set var="tokenName" value="<%=TokenHelper.generateUID()%>"/>, then setting the token using <s:token name="${tokenName}"/>. I'm getting tld error about setting the in the tag. Here is the general code flow of the form.

here are the things that i've tried, but got the same result.

   <%@ page import="org.apache.struts2.util.TokenHelper" %>

   <s:form action="actionName_method" name="actionName" method="post">
     <s:token name="<%=TokenHelper.generateGUID()%>"/>
     <s:hidden ....
     .... rest of the fields go here ....
     <s:submit value="save" name="submit"/>
   </s:form>

Another one I tried is,

    <%@ page import="org.apache.struts2.util.TokenHelper" %>
    <s:set var="tokenName" value="${f:generateGUID()}"/>
    <!-- I defined generateTokenName as a tld function using the TokenHelper class -->
           <s:form action="actionName_method" name="actionName" method="post">
             <s:token name="${tokenName}"/>
             <s:hidden ....
             .... rest of the fields go here ....
             <s:submit value="save" name="submit"/>
           </s:form>

Here is the my definition of the function f:generateGUID() in the tld file.

    <function>
        <description>This will generate the a unique tokenName</description>
        <name>generateGUID</name>
        <function-class>org.apache.struts2.util.TokenHelper</function-class>
        <function-signature>java.lang.String generateGUID()</function-signature>
    </function>

Thank you in advance.


回答1:


Yes, it's possible to set a token name with

<s:token name="%{tokenName}"/>

It will generate two hidden fields one for the token name and another for the token value. Make sure the value of the first field corresponds to the name of the second field.

The action property tokenName is initialized like

tokenName = TokenHelper.generateGUID();

or

tokenName = UUID.randomUUID().toString();

Also make sure the form is using POST method.



来源:https://stackoverflow.com/questions/24214205/is-it-possible-to-set-a-struts-token-with-a-guid-as-a-token-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!