Validation Problems

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Validation Problems

DavidCAIT
Hello,

I am using Struts 2.0.11 with Sun One WebServer 6.1 and Java 1.5.0_11 and my code throws a java.lang.ExceptionInInitializerError exception whenever I try to use the Struts 2 Validator. Here is an example:

input.jsp:

<%@ page language="java" %>
<%@ taglib uri="/struts-tags" prefix="s" %>
<html>
<body>

<s:form action="submit">
    <s:textfield label="Enter a string:" name="field"/>
    <s:submit/>
</s:form>
</body>
</html>

finished.jsp:

<html><body>

Done!

</body></html>

myAction.java:

import com.opensymphony.xwork2.ActionSupport;
public class myAction extends ActionSupport {

String field;

public String getField() {
return field; }

public void setField(String value) {
field = value; }

public String execute() {
return "success";
} }

myAction-validation.xml (stored in the same folder as myAction.class):

<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//EN"
"http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">

<validators>
        <field name="field">
                <field-validator type="requiredString">
                        <message>The field is required.</message>
                </field-validator>
        </field>
</validators>

struts.xml file:

<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
    <constant name="struts.action.extension" value="html"/>
   <package name="default" extends="struts-default">
        <interceptors>
            <interceptor-stack name="defaultStack">
    <interceptor-ref name="validation">
           </interceptor-ref>
            </interceptor-stack>
        </interceptors>
        <action name="submit" class="myAction">
             <result name="success">finished.jsp</result>
        </action>
    </package>
</struts>

web.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>
    <filter>
        <filter-name>struts</filter-name>
        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

Whenever I go to input.jsp and submit the form with an empty field, I get the following exception:

failure ( 6100): for host 127.0.0.1 trying to POST /myAction.html, service-j2ee reports: StandardWrapperValve[default]: WEB2792: Servlet.service() for servlet default threw exception
        javax.servlet.ServletException: WEB2663: Filter execution threw an exception
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:280)
        at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:509)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:218)
        at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:509)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:209)
        at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:509)
        at com.iplanet.ias.web.connector.nsapi.NSAPIProcessor.process(NSAPIProcessor.java:157)
        at com.iplanet.ias.web.WebContainer.service(WebContainer.java:579)
        ----- Root Cause -----
        java.lang.ExceptionInInitializerError
        at com.opensymphony.xwork2.validator.ValidatorFileParser.addValidatorConfigs(ValidatorFileParser.java:192)
        at com.opensymphony.xwork2.validator.ValidatorFileParser.parseActionValidatorConfigs(ValidatorFileParser.java:72)
        at com.opensymphony.xwork2.validator.AnnotationActionValidatorManager.loadFile(AnnotationActionValidatorManager.java:361)
        at com.opensymphony.xwork2.validator.AnnotationActionValidatorManager.buildClassValidatorConfigs(AnnotationActionValidatorManager.java:252)
        at com.opensymphony.xwork2.validator.AnnotationActionValidatorManager.buildValidatorConfigs(AnnotationActionValidatorManager.java:340)
        at com.opensymphony.xwork2.validator.AnnotationActionValidatorManager.getValidators(AnnotationActionValidatorManager.java:69)
        at com.opensymphony.xwork2.validator.AnnotationActionValidatorManager.validate(AnnotationActionValidatorManager.java:138)
        at com.opensymphony.xwork2.validator.AnnotationActionValidatorManager.validate(AnnotationActionValidatorManager.java:113)
        at com.opensymphony.xwork2.validator.AnnotationActionValidatorManager.validate(AnnotationActionValidatorManager.java:100)
        at com.opensymphony.xwork2.validator.ValidationInterceptor.doBeforeInvocation(ValidationInterceptor.java:142)
        at com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:148)
        at org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:48)
        at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:86)
        at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
        at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
        at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
        at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
        at com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:123)
        at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
        at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
        at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
        at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
        at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:167)
        at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:86)
        at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
        at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java

When I remove myAction-validation.xml and re-run my example, it runs without crashing and displays the finished.jsp page. Whenever I have myAction-validation.xml present, it produces the exception. I also tried re-coding my project using Annotation based validation, but that also produced a similar exception.

I have the following jars in my library folder:

xwork-2.0.4.jar
struts2-core-2.0.11.jar
struts2-spring-plugin-2.0.11.jar
servlet.jar
dom4j-1.6.1.jar
commons-logging-1.1.1.jar
commons-collections-3.2.jar
commons-beanutils-1.7.0.jar
asm-1.5.3.jar
antlr-2.7.6.jar
ognl-2.6.11.jar
spring-2.5.4.jar
spring-webmvc-2.5.4.jar
standard-1.1.2.jar

Please let me know if you have any ideas about how to fix this. Thanks,

David
Reply | Threaded
Open this post in threaded view
|

Re: Validation Problems

vinodkashyap
Administrator
Hi

First of all include validate="true" in form
Secondly remove this code

<interceptors> 
            <interceptor-stack name="defaultStack"> 
    <interceptor-ref name="validation"> 
           </interceptor-ref> 
            </interceptor-stack> 
        </interceptors> 

as defaultStack always called and it already contains validation interceptor

Try this It will solve your problem
If it doesn't work then you can try renaming your variable name from field to any other name
But hopefully above methods will resolve it
Reply | Threaded
Open this post in threaded view
|

Re: Validation Problems

DavidCAIT
Thanks for the suggestion.

Unfortunately, it didn't fix the problem. I added validate="true" and removed the <interceptor> tag, but the problem persisted. Then, I renamed my variable from "field" to "email" in both the jsp page, the validator, and the java class. The exception is the same as before.

Any other ideas?
Reply | Threaded
Open this post in threaded view
|

Re: Validation Problems



       <s:textfield name="username" label="Username"/>
<s:textfield name="email" label="email" />
</s:form>

Hope this helps ..
dynamicd
In reply to this post by DavidCAIT
In your struts.xml when u have for actions that need validation like add , edit
add the interceptor seperately.


 <action name="addUser" method="add"
                 class="controller.UserAction" >
                  <interceptor-ref name="MyDefaultStack" />
                                        <interceptor-ref name="validation" />
             <result name="addUser">users/showaddUser.jsp</result>
                          <result name="viewAllUsers" type="chain" >ViewAllUsers</result>
          </action>

my Default Stack looks like this .

<interceptor-stack name="MyDefaultStack">
                <interceptor-ref name="exception" />
                <interceptor-ref name="timer" />
           
                <interceptor-ref name="prepare" />
                <interceptor-ref name="servlet-config" />
                <interceptor-ref name="chain" />
            <interceptor-ref name="model-driven" />
                <interceptor-ref name="static-params" />
                <interceptor-ref name="params" />
                <interceptor-ref name="conversionError" />
        </interceptor-stack>


My Validation xml UserAction-validation.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
<validators>
    <field name="username">
        <field-validator type="requiredstring">
            <message>Name is a required field</message>
        </field-validator>
                <field-validator type="stringlength">
                        true
                        4
            <message>UserName should be atleast 4 characters</message>
        </field-validator>
    </field>
...
</validators>


My jsp looks like this

<%@ taglib prefix="s" uri="/struts-tags" %>  
  <s:head theme="ajax" debug="true"/>
<s:div id="Useradddiv">
<div class="table">

<s:form id="adduserform" method="POST" >
<p>Add User<p>
          <s:actionerror />