This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Struts and the fly likes Struts Validator not validating on the server-side Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Struts Validator not validating on the server-side" Watch "Struts Validator not validating on the server-side" New topic
Author

Struts Validator not validating on the server-side

Brian Pembroke
Greenhorn

Joined: Jan 21, 2003
Posts: 6
I'm using the dynamaic validator action form to validate my input fields. The client-side validation works great. The server-side validation does not seem to work. I would disable the javascript on the browser to test the server-side validation. The message bean on the jsp will never display any messages. I'm using Struts 1.1-b3. Any help with the problem would be greatly appreciated. Snipplets of my code below:

Struts-config
.
.
.
<!-- Article Form DynaBean -->
<form-bean
name="logonForm"
type="org.apache.struts.action.DynaValidatorActionForm">
<form-property
name="username"
type="java.lang.String"/>
<form-property
name="password"
type="java.lang.String"/>
</form-bean>
.
.
.
<action
path="/logonSubmit"
type="LogonAction"
name="logonForm"
scope="request"
validate="true"
input="/logon.jsp">
<forward
name="success"
path="/logon.jsp"/>
</action>
.
.
.
<!-- ========== Validator plug-in setting settings =================== -->
<plug-in className="org.apache.struts.validator.ValidatorPlugIn" >
<set-property property="pathnames"
value="/WEB-INF/validator-rules.xml,
/WEB-INF/validation.xml" />
</plug-in>
Validation.xml
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE form-validation PUBLIC
"-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.0//EN"
"http://jakarta.apache.org/commons/dtds/validator_1_0.dtd">
<form-validation>
<!-- b -->
<formset>
<!-- c -->
<form name="logonForm">
<!-- d,e -->
<field property="username" depends="required,mask">
<!-- f -->
<msg name="mask" key="logon.username.maskmsg" />
<!-- g -->
<arg0 key="logon.username.displayname" />
<!-- h,i -->
<var>
<var-name>mask</var-name>
<var-value>^[a-zA-Z0-9]*$</var-value>
</var>
</field>
<!-- J -->
<field property="password" depends="required,minlength">
<arg0 key="logon.password.displayname" />
<var>
<var-name>minlength</var-name>
<var-value>5</var-value>
</var>
</field>
</form>
<!-- ... -->
</formset>
</form-validation>
Logon Action
package com.anico.slwbsill;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.DynaActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionServlet;

public final class LogonAction extends Action {

/**
* Login the user.
* The event is logged if the debug level is >= Constants.DEBUG.
*
* @param mapping The ActionMapping used to select this instance
* @param actionForm The ActionForm bean for this request (if any)
* @param request The HTTP request we are processing
* @param response The HTTP response we are creating
*
* @exception IOException if an input/output error occurs
* @exception ServletException if a servlet exception occurs
*/
public ActionForward perform(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
// Obtain username and password from web tier
DynaActionForm logonForm = (DynaActionForm) form;
String username = ((String)logonForm.get("username"));
String password = ((String)logonForm.get("password"));
System.out.println ("Username: " + username);
System.out.println ("Password: " + password);
// Save our logged-in user in the session,
// because we use it again later.
HttpSession session = request.getSession();
session.setAttribute(Constants.USER_KEY, form);
// Log this event, if appropriate
if (servlet.getDebug() >= Constants.DEBUG) {
StringBuffer message =
new StringBuffer("LogonAction: User '");
message.append(username);
message.append("' logged on in session ");
message.append(session.getId());
servlet.log(message.toString());
}
// Return success
return (mapping.findForward(Constants.SUCCESS));
}
} // End LogonAction

Logon.jsp
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<!-- b -->
<HTML><HEAD><TITLE>Sign in, Please!</TITLE></HEAD>
<BODY>
<logic:messagesPresent>
<UL>
<html:messages id="error">
<LI><bean:write name="error"/></LI>
</html:messages>
</UL>
</logic:messagesPresent>
<html:form action="logonSubmit.do" focus="username">
<TABLE border="0" width="100%">
<TR><TH align="right">Username:</TH>
<TD align="left"><html:text property="username"/></TD>
</TR>
<TR><th align="right">Password:</TH>
<TD align="left"><html assword property="password"/></TD>
</TR>
<TR>
<TD align="right"><html:submit property="submit" value="Submit"/></TD>
<TDalign="left"><html:reset/></TD>
<TD align="left"><html:cancel
</TABLE>
</html:form>
[ January 23, 2003: Message edited by: Brian Pembroke ]
Kalyan Reddy
Greenhorn

Joined: Jul 28, 2004
Posts: 3
Hi,

If you already got the answer please donot consider this.

This validation can be done in two ways.


1)In the struts-config.xml

Instead of

<form-bean
name="logonForm"
type="org.apache.struts.action.DynaValidatorActionForm">


use

<form-bean
name="logonForm"
type="org.apache.struts.action.DynaValidatorForm">



(OR)

2)In the validation.xml change

<form name="logonForm">

to

<form name="/logonSubmit"> ( ie; the action path for which the form is declared )


with Regards,
KalyanReddy
somkiat puisungnoen
Ranch Hand

Joined: Jul 04, 2003
Posts: 1312
Hi Kalyan Reddy ,

Please post example of validate data in server side.

Thank you.


SCJA,SCJP,SCWCD,SCBCD,SCEA I
Java Developer, Thailand
Kalyan Reddy
Greenhorn

Joined: Jul 28, 2004
Posts: 3
I am validating a textfield for an valid email id, with a reqular expression in validation.xml file.

validation.xml has

<form name="/processSimple">
<field
property="symbol"
depends="required,mask">
<msg name="required" key="error.symbol.required"/>
<msg name="mask" key="error.symbol.invalid"/>
<var>
<var-name>mask</var-name>
<var-value>^[a-z0-9`!#\$%&\*\+\/=\?\^\']+((\.|\-|_)[a-z0-9`!#\$%&\*\+\/=\?\^\']+)*@[a-z0-9\-]+([\.][a-z0-9\-]+)+$</var-value>
</var>
</field>
</form>


Struts-config.xml contains

<form-bean name="simpleForm" type="org.apache.struts.validator.DynaValidatorActionForm">

<form-property name="symbol" type="java.lang.String"/>

</form-bean>



<action path="/processSimple"
name="simpleForm"
validate="true"
input="/jsp/index.jsp"
type="example.SimpleAction">
<forward name="success" path="/jsp/success.jsp"/>
<forward name="failure" path="/jsp/failure.jsp"/>
<forward name="cancel" path="/jsp/Welcome.jsp"/>
</action>

<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property property="pathnames" value="/WEB-INF/validation.xml"/>
</plug-in>

I have given the example for the problem raised
somkiat puisungnoen
Ranch Hand

Joined: Jul 04, 2003
Posts: 1312
I would disable the javascript on the browser to test the server-side validation. The message bean on the jsp will never display any messages. I'm using Struts 1.1-b3. Any help with the problem would be greatly appreciated. Snipplets of my code below:


1. Disable javascript of validation
don't type <html:javascript formName="someform" /> in jsp page.


The message bean on the jsp will never display any messages.


please comment <logic:messagesPresent> , it's will not work.
Kalyan Reddy
Greenhorn

Joined: Jul 28, 2004
Posts: 3
I have'nt used <html:javascript formName="someform" /> in my jsp page.

we should use either

<logic:messagesPresent>

or

<html:errors/> in the jsp page, for printing the error messages .

If we use <logic:messagesPresent>, it does'nt mean that we are using client side validation.It is server side validation only.

I think you are using validate="true" atrribute in action tag in struts-config.xml, first of all to invoke the validate method.

The validate() method will return Object of ActionErrors class if any validation errors are present. The <logic:messagesPresent> or <html:errors/> will Check this ActionErrors for printing the error messages.

If we are not using the <logic:messagesPresent> or <html:errors/> how does the jsp read the ActionErrors Object returned by the validate method.

thanks,
forum guest
Greenhorn

Joined: Mar 24, 2003
Posts: 16
I would NOT change the form name in the validation.xml to the action path. That should be the declared name of the form.

Try to replace

<logic:messagesPresent>
<UL>
<html:messages id="error">
<LI><bean:write name="error"/></LI>
</html:messages>
</UL>
</logic:messagesPresent>


with just <html:errors />
ingmar kellner
Greenhorn

Joined: Mar 28, 2005
Posts: 1
Hello all,

I try to get a simple form validated by the validator but I cannot get a checkbox validated.
While the textfield is checked correctly the checkbox is ignored by the validator. What surprises me is that if I enable JavaScript the error is reported correctly by a pop-up.

I searched the internet for a while but could not find out if this is a known bug or how to get it to work.

Section from the validation.xml:

<field property="textfield" depends="required">
<msg name="required" key="error.textfield.required"/>
</field>

<field property="checkbox" depends="required">
<msg name="required" key="error.checkbox.required"/>
</field>

Any suggestions are welcome!

Ingmar
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
This problem can be solved by setting a default value for the checkbox property in the reset() method of the form.

See this url for more information.

http://struts.apache.org/faqs/newbie.html#checkbox


Merrill
Consultant, Sima Solutions
Anil Sharma
Greenhorn

Joined: Nov 07, 2005
Posts: 15
<!-- Article Form DynaBean -->
<form-bean
name="logonForm"
type="org.apache.struts.action.DynaValidatorActionForm">
<form-property
name="username"
type="java.lang.String"/>
<form-property
name="password"
type="java.lang.String"/>
</form-bean>

here you must use "org.apache.struts.validator.DynaValidatorActionForm" instead of "org.apache.struts.action.DynaValidatorActionForm".Since "DynaValidatorActionForm" is in "org.apache.struts.validator" package, not in "org.apache.struts.action" package.

Regards,
Anil Sharma


Anil Sharma<br />SCJP 1.4, SCWCD 1.4(Preparing)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Struts Validator not validating on the server-side