top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Struts: Question about Form Validation Using XML example

+2 votes
516 views

I can't see what's going on behind the scenes in this example so I'm asking for help in order to understand the Strtus process.

I run the example, leave empty "First Name" to make validation fail, and submit. Then go back to index:

http://localhost:8084/form_xml_validation

and click on edit. But "First name" is reset with the original value. It should be empty because Person instance is managed statically. In fact, other examples (preparable, exclude params) behave as expected, and return an empty String for the filed left empty.

The thing here is than I'm not understanding how Struts works here. I would appreciate if you help me with this.

Example is here:
http://struts.apache.org/release/2.3.x/docs/form-validation-using-xml.html

Code for checkout here:
http://svn.apache.org/repos/asf/struts/sandbox/trunk/struts2examples/

Examples doing as expected:
http://struts.apache.org/release/2.3.x/docs/exclude-parameters.html
http://struts.apache.org/release/2.3.x/docs/preparable-interface.html

posted Oct 2, 2013 by Sonu Jindal

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote

Maybe your browser is filling the field and it has nothing to do with Struts validation? Check that in anonymous mode.

answer Oct 3, 2013 by Bob Wise
Same behavior in private mode.
I believe it has to do with Prepare (preparable and exclude-paras use  Preparable) and interceptors stack, but I'm not sure. Still a mystery  for me, because the code is almost identical.
Similar Questions
+1 vote

I am using struts 2.2.3 and doing client side validation using xml. I want to do validation for a field whose name is an ognl expression I have something like this :

Field is required

On debugging I can see It is adding field error properly. But i have a javascript which shows the field in red highlighted color and also shows messages that "fieldname is required". However fieldname is not coming in the message and it is not showing the required field in red highlighted color.

The same javascript code works fine for normal fields which have static name. Please can someone help in this, as I am new to struts ?

0 votes

I'm trying to validate form data before submitting it to the web server but the code giving error.
I'm not able to find where is the problem in code.
so plz correct it....

<script type="text/javascript">
<!--
function validateEmail()
{

   var email_id = document.Form.emaill.value;
   atpos = emailID.indexOf("@");
   dotpos = emailID.lastIndexOf(".");
   if (atpos >1 : ( dotpos - atpos > 2 )) 
   {
       alert("Please enter correct email ID")
       document.myForm.EMail.focus() ;
       return false;
   }
   return( true );
}
//-->
</script>
0 votes

In the context of an tag, for instance for a login action, we previously always coded forms as :

 ....

And in the struts.xml we have been using :

content.login
 content.menu
 summary_input
 welcome

This has worked in the past and allowed us to not only use the action="login" to target the execute method of the action, but also to build links to, for instance, login_checkStatus.action to target other, specific methods within the action. I am not sure this Is a common/best practice, I had no prior struts2 experience before joining this company and this is how a lot of the existing functionality is coded. (If this is wrong/there is a better way, please let me know)

Now randomly, this fails to generate the proper action url in the html (it is missing the '.action', which results in a 404 once you post the form)

The only workaround we have found so far is to add the '.action' suffix to the
s:form's action attribute. Before it was always added for us by struts.

So now we are having success with

 ...

I can only assume that this has been affected by the changes related to S2-015 and the wildcard action matching.

0 votes

i need to have multiple on single web page,

(at top panel, our site always provide parts search box/)
(at middle panel, could have other form such as checkout section, login section, etc)

when any single form's validation triggered, then and displayed at all form's textfields place,

how to solve it? or that is struts 2 bug? please help...

...