You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by vikrant S <sh...@gmail.com> on 2009/11/16 15:42:40 UTC

Struts2 Multiple validation for single field

Hi all ,
I am doing server side validation using validate method inside my action
class.I need to validate the textfield  whether it is a null and if not it
should be numeric.
My jsp page is as follows:- 

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<%@taglib uri="/struts-tags" prefix="s" %>
<html>
<head>
   <s:head />
   <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
   <title>Login Page</title>
   </head>
   <body >
    <s:form action="Login">
    <s:textfield label="Load" size="12"  name="load"/>
    <s:textfield label="Duration" size="12" name="time"/> 
   <s:submit theme="simple" action="start"  value="Start" />
   <s:submit theme="simple" action="stop"  value="Stop" />
  <s:submit theme="simple" action="change" value="Change" />
   </s:form>
   </body>
</html>

and my action class is as follows
import com.opensymphony.xwork2.ActionSupport;
public class RemoteClientAction extends ActionSupport {
	 public void validate() {
        if (getLoad().length() == 0) {
            addFieldError("load", "Load is required");
          }
         if (getTime().length() == 0) {
            addFieldError("time", "Duration is required");          
        }
	 }
	public String getLoad() {
		return load;
	}
	public void setLoad(String load) {
		this.load = load;
	}
	public String getTime() {
		return time;
	}
	public void setTime(String time) {
		this.time = time;
	}
	private String load;
	private String time;
       public String execute(){
      return SUCCESS;
}

Here I am able to validate for not null fields. But I also want to validate
it for the field if not null should be numeric. How Can I do this??
Please Suggest me possible solution in order to validate both things.
-- 
View this message in context: http://old.nabble.com/Struts2-Multiple-validation-for-single-field-tp26373102p26373102.html
Sent from the Struts - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


RE: Struts2 Multiple validation for single field

Posted by Neil Aggarwal <ne...@JAMMConsulting.com>.
Vikrant:

> If I am declaring it as int how should I check for null.

OK, I think I see what you are asking.

If you change your field to an int field and the
user  does not fill in the value, you will
get zero.  If they put in zero, you will get zero
and there is no way to tell the difference.

Is that the problem?

I think you are going to have to do it manually.

Set your field as a String and then in your
validate method, check if it is null or a valid
int value.  If it is not what you want, throw
an ActionError.

	Neil


--
Neil Aggarwal, (281)846-8957, http://UnmeteredVPS.net
Host your struts app on a CentOS VPS for only $25/month!
Unmetered bandwidth, 7 day no risk trial, Google Checkout


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Struts2 Multiple validation for single field

Posted by vikrant S <sh...@gmail.com>.
If I am declaring it as int how should I check for null. Can u explain me
please?
-- 
View this message in context: http://old.nabble.com/Struts2-Multiple-validation-for-single-field-tp26373102p26373588.html
Sent from the Struts - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


RE: Struts2 Multiple validation for single field

Posted by vikrant S <sh...@gmail.com>.
Thanks Niel For Your Help !!!! 
-- 
View this message in context: http://old.nabble.com/Struts2-Multiple-validation-for-single-field-tp26373102p26373908.html
Sent from the Struts - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


RE: Struts2 Multiple validation for single field

Posted by Neil Aggarwal <ne...@JAMMConsulting.com>.
Vikrant:

> 20:50:58,950 WARN  [OgnlValueStack] Error setting value
> ognl.MethodFailedException: Method "setLoad" failed for object
> com.tcs.infra.workload.RemoteClientAction@161ef9c

This is no big deal since it is just a warning.

If you want to keep your field as an int, you can
implement a method setLoad(String) in your class
that does nothing.

	Neil

--
Neil Aggarwal, (281)846-8957, http://UnmeteredVPS.net
Host your struts app on a CentOS VPS for only $25/month!
Unmetered bandwidth, 7 day no risk trial, Google Checkout


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


RE: Struts2 Multiple validation for single field

Posted by vikrant S <sh...@gmail.com>.
Ya Now its working fine. I am running this application on jboss server. While
the application is running I am getting lot of errors in the console such as
follows:-
Any suggestions Please in order Not to get such kind of errors

20:50:58,950 WARN  [OgnlValueStack] Error setting value
ognl.MethodFailedException: Method "setLoad" failed for object
com.tcs.infra.workload.RemoteClientAction@161ef9c
[java.lang.NoSuchMethodException: setLoad([Ljava.lang.String;)]
	at ognl.OgnlRuntime.callAppropriateMethod(OgnlRuntime.java:823)
	at ognl.OgnlRuntime.setMethodValue(OgnlRuntime.java:964)
	at
ognl.ObjectPropertyAccessor.setPossibleProperty(ObjectPropertyAccessor.java:75)
	at ognl.ObjectPropertyAccessor.setProperty(ObjectPropertyAccessor.java:131)
	at
com.opensymphony.xwork2.ognl.accessor.ObjectAccessor.setProperty(ObjectAccessor.java:28)
	at ognl.OgnlRuntime.setProperty(OgnlRuntime.java:1656)
	at
com.opensymphony.xwork2.ognl.accessor.CompoundRootAccessor.setProperty(CompoundRootAccessor.java:50)
	at ognl.OgnlRuntime.setProperty(OgnlRuntime.java:1656)
	at ognl.ASTProperty.setValueBody(ASTProperty.java:101)
	at ognl.SimpleNode.evaluateSetValueBody(SimpleNode.java:177)
	at ognl.SimpleNode.setValue(SimpleNode.java:246)
	at ognl.Ognl.setValue(Ognl.java:476)
	at com.opensymphony.xwork2.ognl.OgnlUtil.setValue(OgnlUtil.java:192)
	at
com.opensymphony.xwork2.ognl.OgnlValueStack.setValue(OgnlValueStack.java:155)
	at
com.opensymphony.xwork2.ognl.OgnlValueStack.setValue(OgnlValueStack.java:143)
	at
com.opensymphony.xwork2.interceptor.ParametersInterceptor.setParameters(ParametersInterceptor.java:273)
	at
com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:187)
	at
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
	at
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
	at
com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)
	at
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
	at
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
	at
com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:148)
	at
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
	at
org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:93)
	at
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
	at
org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:235)
	at
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
	at
com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:89)
	at
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
	at
com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:128)
	at
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
	at
org.apache.struts2.interceptor.ProfilingActivationInterceptor.intercept(ProfilingActivationInterceptor.java:104)
	at
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
	at
org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:267)
	at
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
	at
com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:126)
	at
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
	at
com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:138)
	at
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
	at
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
	at
com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:148)
	at
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
	at
org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
	at
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
	at
com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:128)
	at
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
	at
com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:176)
	at
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
	at
org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52)
	at
org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:468)
	at
org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:395)
	at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
	at
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
	at
org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
	at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
	at
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
	at
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:235)
	at
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
	at
org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:190)
	at
org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:92)
	at
org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.process(SecurityContextEstablishmentValve.java:126)
	at
org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:70)
	at
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
	at
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
	at
org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158)
	at
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
	at
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:330)
	at
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:829)
	at
org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:601)
	at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
	at java.lang.Thread.run(Thread.java:636)
/-- Encapsulated exception ------------\
java.lang.NoSuchMethodException: setLoad([Ljava.lang.String;)
	at ognl.OgnlRuntime.callAppropriateMethod(OgnlRuntime.java:810)
	at ognl.OgnlRuntime.setMethodValue(OgnlRuntime.java:964)
	at
ognl.ObjectPropertyAccessor.setPossibleProperty(ObjectPropertyAccessor.java:75)
	at ognl.ObjectPropertyAccessor.setProperty(ObjectPropertyAccessor.java:131)
	at
com.opensymphony.xwork2.ognl.accessor.ObjectAccessor.setProperty(ObjectAccessor.java:28)
	at ognl.OgnlRuntime.setProperty(OgnlRuntime.java:1656)
	at
com.opensymphony.xwork2.ognl.accessor.CompoundRootAccessor.setProperty(CompoundRootAccessor.java:50)
	at ognl.OgnlRuntime.setProperty(OgnlRuntime.java:1656)
	at ognl.ASTProperty.setValueBody(ASTProperty.java:101)
	at ognl.SimpleNode.evaluateSetValueBody(SimpleNode.java:177)
	at ognl.SimpleNode.setValue(SimpleNode.java:246)
	at ognl.Ognl.setValue(Ognl.java:476)
	at com.opensymphony.xwork2.ognl.OgnlUtil.setValue(OgnlUtil.java:192)
	at
com.opensymphony.xwork2.ognl.OgnlValueStack.setValue(OgnlValueStack.java:155)
	at
com.opensymphony.xwork2.ognl.OgnlValueStack.setValue(OgnlValueStack.java:143)
	at
com.opensymphony.xwork2.interceptor.ParametersInterceptor.setParameters(ParametersInterceptor.java:273)
	at
com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:187)
	at
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
	at
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
	at
com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)
	at
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
	at
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
	at
com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:148)
	at
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
	at
org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:93)
	at
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
	at
org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:235)
	at
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
	at
com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:89)
	at
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
	at
com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:128)
	at
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
	at
org.apache.struts2.interceptor.ProfilingActivationInterceptor.intercept(ProfilingActivationInterceptor.java:104)
	at
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
	at
org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:267)
	at
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
	at
com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:126)
	at
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
	at
com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:138)
	at
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
	at
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
	at
com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:148)
	at
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
	at
org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
	at
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
	at
com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:128)
	at
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
	at
com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:176)
	at
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
	at
org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52)
	at
org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:468)
	at
org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:395)
	at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
	at
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
	at
org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
	at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
	at
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
	at
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:235)
	at
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
	at
org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:190)
	at
org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:92)
	at
org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.process(SecurityContextEstablishmentValve.java:126)
	at
org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:70)
	at
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
	at
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
	at
org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158)
	at
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
	at
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:330)
	at
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:829)
	at
org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:601)
	at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
	at java.lang.Thread.run(Thread.java:636)
\--------------------------------------/

-- 
View this message in context: http://old.nabble.com/Struts2-Multiple-validation-for-single-field-tp26373102p26373749.html
Sent from the Struts - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


RE: Struts2 Multiple validation for single field

Posted by Neil Aggarwal <ne...@JAMMConsulting.com>.
Vikrant:

> Here I am able to validate for not null fields. But I also 
> want to validate
> it for the field if not null should be numeric. How Can I do this??
> Please Suggest me possible solution in order to validate both things.

Have you tried making your fields int instead of String?

	Neil


--
Neil Aggarwal, (281)846-8957, http://UnmeteredVPS.net
Host your struts app on a CentOS VPS for only $25/month!
Unmetered bandwidth, 7 day no risk trial, Google Checkout


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org