4

preface: i'm currently using struts2-core-2.3.1.2, and upgrading isn't an option.

i'm trying to implement HTML5 required fields in my struts2 form. struts won't even render this:

<s:textfield name="x_serialNbr" id="i_sn" required />

and while it will render these:

<s:textfield name="x_serialNbr" id="i_sn" required="true" />
<s:textfield name="x_serialNbr" id="i_sn" required="required" />

the resulting HTML is not what i want:

<input type="text" name="x_serialNbr" value="" id="i_sn" />

after extensive googling, this post from over a year ago is the closest i can find to something that addresses my issue. it seems to indicate that this problem has been resolved in the current version of struts2, but as i said, i can't upgrade.

as far as i can see, my options are

  1. dynamically add "required" attributes to the appropriate fields on page load.
  2. roll my own validation
  3. ?? is there something i am missing? is there some documentation i've just glossed over?
Roman C
  • 49,761
  • 33
  • 66
  • 176
carrieks
  • 87
  • 6
  • 2
    If you cannot upgrade then you can change ftl templates to include `required` attribute. – Aleksandr M Aug 14 '14 at 11:54
  • that hadn't occurred to me. i'll have to do some research! – carrieks Aug 14 '14 at 13:35
  • Consider to upgrade, BTW. Every version before 2.3.15.3 is affected by a SERIOUS security issue, really (and not theoretically) dangerous. – Andrea Ligios Aug 26 '14 at 11:44
  • @AndreaLigios if i had any say in it, i would. i'm interested in learning more about the security issue, though. can you tell me more? – carrieks Aug 28 '14 at 12:48
  • Sure. The most dangerous is 018, the link in [this answer](http://stackoverflow.com/a/21603806/1654265). Then 020 / 021 are [annoying too](http://stackoverflow.com/a/23343142/1654265). – Andrea Ligios Aug 28 '14 at 12:58

1 Answers1

3

You can use plain html,but the value you should get either OGNL or EL

<input type="text" name="x_serialNbr" value="<s:property value='x_serialNbr'/>" id="i_sn" required="true">
<input type="text" name="x_serialNbr" value="${x_serialNbr}" id="i_sn" required="required">
Roman C
  • 49,761
  • 33
  • 66
  • 176