html5 required fields with struts2: a lost cause?

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?

Answer

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

Leave a Reply

Your email address will not be published. Required fields are marked *