html5 required fields with struts2: a lost cause? Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of html5 required fields with struts2: a lost cause? without wasting too much if your time.

The question is published on by Tutorial Guruji team.

preface: i’m currently using struts2-core-, 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?


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">
We are here to answer your question about html5 required fields with struts2: a lost cause? - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji