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.
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.