Search code examples
javajspcheckboxstruts2struts-tags

How to render checkbox without hidden field in Struts 2


When we create Struts2 checkbox using below code

<s:checkbox name="checkMe" fieldValue="true" label="Check Me for testing"/>

It gives below HTML representation (I am referring to https://www.mkyong.com/struts2/struts-2-scheckbox-checkbox-example/)

<input type="checkbox" name="checkMe" value="true" id="xx_checkMe"/>
<input type="hidden" id="__checkbox_xx_checkMe" name="__checkbox_checkMe" value="true"/>
<label for="resultAction_checkMe" class="checkboxLabel">Check Me for testing</label>

Is there anyway to eliminate creating hidden parameter because it is causing security vulnerability for my application


Solution

  • It's used a freemarker template checkbox.ftl to generate html output. You can copy from the source archive and modify this template, then use template and templateDir attributes to point to the custom template.

    Or use archived template, but you need to add velocity and velocity-tools dependencies.

    <s:checkbox name="checkme" template="checkbox.vm" templateDir="template/archive"/>