• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Struts ActionForm: ClassCastException caused by FormTag.initFormBean

 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey guys

I'm trying to created a login page using struts and I'm getting

errors. The page was working when I used ActionForm. But I started

getting errors when I changed to DynaValidatorForm.

I'm learning Struts, so I can't seem to find out where the source of

the problem is.

=====================================

[7/18/06 18:16:37:000 PDT] 00000044 ServletWrappe E SRVE0068E: Could

not invoke the service() method on servlet /web/pages/login/login.jsp.

Exception thrown : javax.servlet.ServletException: Exception creating

bean of class com.poshWebApp.actions.LoginAction:

java.lang.ClassCastException: com.poshWebApp.actions.LoginAction
at

org.apache.jasper.runtime.PageContextImpl.handlePageException(PageCont

extImpl.java:639)
at com.ibm._jsp._login._jspService(_login.java:89)
at

com.ibm.ws.jsp.runtime.HttpJspBase.service(HttpJspBase.java:88)
at

javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
at

com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.

java:1212)
at

com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWr

apper.java:629)
at

com.ibm.wsspi.webcontainer.servlet.GenericServletWrapper.handleRequest

(GenericServletWrapper.java:117)
at

com.ibm.ws.jsp.webcontainerext.JSPExtensionServletWrapper.handleReques

t(JSPExtensionServletWrapper.java:171)
at

com.ibm.ws.jsp.webcontainerext.JSPExtensionProcessor.handleRequest(JSP

ExtensionProcessor.java:230)
at

com.ibm.ws.webcontainer.webapp.WebApp.handleRequest(WebApp.java:2837)
at

com.ibm.ws.webcontainer.webapp.WebGroup.handleRequest(WebGroup.java:22

0)
at

com.ibm.ws.webcontainer.VirtualHost.handleRequest(VirtualHost.java:204

)
at

com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:1

681)
at

com.ibm.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java

:77)
at

com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscriminat

ion(HttpInboundLink.java:421)
at

com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewInformat

ion(HttpInboundLink.java:367)
at

com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.ready(HttpInbound

Link.java:276)
at

com.ibm.ws.tcp.channel.impl.NewConnectionInitialReadCallback.sendToDis

criminaters(NewConnectionInitialReadCallback.java:201)
at

com.ibm.ws.tcp.channel.impl.NewConnectionInitialReadCallback.complete(

NewConnectionInitialReadCallback.java:103)
at

com.ibm.ws.tcp.channel.impl.WorkQueueManager.requestComplete(WorkQueue

Manager.java:548)
at

com.ibm.ws.tcp.channel.impl.WorkQueueManager.attemptIO(WorkQueueManage

r.java:601)
at

com.ibm.ws.tcp.channel.impl.WorkQueueManager.workerRun(WorkQueueManage

r.java:934)
at

com.ibm.ws.tcp.channel.impl.WorkQueueManager$Worker.run(WorkQueueManag

er.java:1021)
at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1332)
---- Begin backtrace for Nested Throwables
javax.servlet.jsp.JspException: Exception creating bean of class

com.poshWebApp.actions.LoginAction: java.lang.ClassCastException:

com.poshWebApp.actions.LoginAction
at

org.apache.struts.taglib.html.FormTag.initFormBean(FormTag.java:547)
at

org.apache.struts.taglib.html.FormTag.doStartTag(FormTag.java:520)
at com.ibm._jsp._login._jspx_meth_html_form_0(_login.java:158)
at com.ibm._jsp._login._jspx_meth_html_html_0(_login.java:196)
at com.ibm._jsp._login._jspService(_login.java:81)
at

com.ibm.ws.jsp.runtime.HttpJspBase.service(HttpJspBase.java:88)
at

javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
at

com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.

java:1212)
at

com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWr

apper.java:629)
at

com.ibm.wsspi.webcontainer.servlet.GenericServletWrapper.handleRequest

(GenericServletWrapper.java:117)
at

com.ibm.ws.jsp.webcontainerext.JSPExtensionServletWrapper.handleReques

t(JSPExtensionServletWrapper.java:171)
at

com.ibm.ws.jsp.webcontainerext.JSPExtensionProcessor.handleRequest(JSP

ExtensionProcessor.java:230)
at

com.ibm.ws.webcontainer.webapp.WebApp.handleRequest(WebApp.java:2837)
at

com.ibm.ws.webcontainer.webapp.WebGroup.handleRequest(WebGroup.java:22

0)
at

com.ibm.ws.webcontainer.VirtualHost.handleRequest(VirtualHost.java:204

)
at

com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:1

681)
at

com.ibm.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java

:77)
at

com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscriminat

ion(HttpInboundLink.java:421)
at

com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewInformat

ion(HttpInboundLink.java:367)
at

com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.ready(HttpInbound

Link.java:276)
at

com.ibm.ws.tcp.channel.impl.NewConnectionInitialReadCallback.sendToDis

criminaters(NewConnectionInitialReadCallback.java:201)
at

com.ibm.ws.tcp.channel.impl.NewConnectionInitialReadCallback.complete(

NewConnectionInitialReadCallback.java:103)
at

com.ibm.ws.tcp.channel.impl.WorkQueueManager.requestComplete(WorkQueue

Manager.java:548)
at

com.ibm.ws.tcp.channel.impl.WorkQueueManager.attemptIO(WorkQueueManage

r.java:601)
at

com.ibm.ws.tcp.channel.impl.WorkQueueManager.workerRun(WorkQueueManage

r.java:934)
at

com.ibm.ws.tcp.channel.impl.WorkQueueManager$Worker.run(WorkQueueManag

er.java:1021)
at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1332)


==================================================================

LOGIN.JSP


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<html:html>
<head>

<%@ page
language="java"
contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"
%>
<META name="GENERATOR" content="IBM Software Development Platform">
<meta http-equiv="Content-Type" content="text/html;">
<title>Untitled</title>


<style>
TD{
font-family:verdana,arial;
color:black;
font-size:8pt;

}
.news{
font-family:arial;
}
.bot{
color:black;
text-decoration:none;
font-size:7pt;
font-family:Verdana;
font-weight:bold;

}
.copy{
font-size:7pt;
color:black;
}
</style>
</head>

<body>

<html:errors/>

<html:form action="/submitLogin"

type="com.poshWebApp.actions.LoginAction" name="LoginForm"

method="POST">

<table border="0" cellpadding="0" cellspacing="0" width="159">


<tr>
<td valign="top" class="news"><b>UserName: </b></td>
<td>   <html:text property="userName" size="11" />


</td>
</tr>

<tr>


<td valign="top" class="news"><b>PassWord: </b></td>
<td>   <html:text property="passWord" size="11" />
</td>

</tr>

<tr>

<td valign="top"class="news"><INPUT type="submit" value="Logon"></td>

</tr>

</table>
</html:form>

</html:form>
</BODY>
</html:html>

====================================

STUTS_CONFIG.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD

Struts Configuration 1.1//EN"

"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>

<!-- Data Sources -->
<data-sources>
</data-sources>

<!-- Form Beans -->
<form-beans>
<form-bean name="LoginForm"

type="org.apache.struts.validator.DynaValidatorForm">

<!-- Dynamic properties of the Login Form -->
<form-property name="userName"

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

type="java.lang.String" />

</form-bean>
</form-beans>

<!-- Global Exceptions -->
<global-exceptions>
</global-exceptions>

<!-- Global Forwards -->
<global-forwards>
</global-forwards>

<!-- Action Mappings -->
<action-mappings>
<action path="/myPath" forward="/main.jsp" />
<action path="/submitLogin" scope="request"

type="com.poshWebApp.actions.LoginAction" name="LoginForm"

validate="false" input="/web/pages/login/login.jsp">
<forward name="success" path="/main.jsp" />
</action>
</action-mappings>

<!-- Message Resources -->
<message-resources

parameter="pbwebapp.resources.ApplicationResources"/>

<!-- Validator Plugin -->
<plug-in

className="org.apache.struts.validator.ValidatorPlugIn">
<set-property property="pathnames"

value="/WEB-INF/validator-rules.xml, /WEB-INF/validation.xml" />
</plug-in>

</struts-config>

===============================================================

LOGINACTION.java

package com.poshWebApp.actions;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.validator.DynaValidatorForm;

/**
* Form bean for a Struts application.
* Users may access 2 fields on this form:
* <ul>
* <li>passWord - [your comment here]
* <li>userName - [your comment here]
* </ul>
* @version 1.0
* @author
*/
public class LoginAction extends Action {

public ActionForward execute(ActionMapping mapping,
ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception {

DynaValidatorForm dynaform = (DynaValidatorForm) form;
dynaform.set("userName", "user name");
dynaform.set("passWord", "password");


request.setAttribute(mapping.getAttribute(), dynaform);

System.out.println("Forwarding - SUCCESS page");

return mapping.findForward("success");
}


}


Please help...Thanks!!!
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It looks like your JSP is still trying to create an instance of your old ActionForm bean. Try changing something insignificant in the JSP, such as adding or removing a space and saving it. This will force the JSP to recompile. This may cause it to recognize the form bean type as DynaValidagorForm.
 
Nina Anderson
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your response. I tried modifying the jsp file and re-compiling the project; but I'm still getting the same error.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Struts has a nasty habit of quietly refusing to create a form if you've got something wrong, so the ActionForm that the Action gets is null, so you get ClassCastException. Try checking if your form is null; if so, your standard output may have something (slightly) helpful to say about why the form wasn't created.
I'm wondering if this might be the reason (from your jsp):


</html:form>

</html:form>



HTH!
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also try removing all the attributes from your <html:form> tag except action="/submitLogin".

Another thing I noticed is that the code in your Action class doesn't make sense. The execute() method of LoginAction is called after the form is submitted. So why are you setting the values of userName and Password? You should be reading these vaues from the form, not setting them:

String userName= (String) dynaform.get("userName");
String passWord= (String) dynaform.get("passWord")/
// do something with the values such as verify against a database.
 
Nina Anderson
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the tip.

I remove all the attributes in the <html:form ...> except for "action=...". The exception error disappeared and the login page now displays successful.

However, the page does not seem to be going through the form validation. When I enter an invalid login (eg. leave password blank) the page no longer displays error messages.

Previously, when I was using the ActionForm class, I explicitly added the error keys to the ActionError() object to be displayed in the jsp <html:errors>. But, now that I using DynaValidatorForm, the form is not being validated.

I'll appreciate your insight.

Here's my validation files:

=======================================================

Validation.xml

<formset>
<form name="LoginForm">
<field property="userName" depends="required">
<arg0 key="prompt.username" />
</field>
<field property="passWord" depends="required, minlength, maxlength">
<arg0 key="prompt.password" />
<arg1 key="${var:minlength}" name="minlength" resource="false" />
<arg2 key="${var:maxlength}" name="maxlength" resource="false" />
<var>
<var-name>minlength</var-name>
<var-value>6</var-value>
</var>
<var>
<var-name>maxlength</var-name>
<var-value>12</var-value>
</var>
</field>
</form>
</formset>

==================================================================

ApplicationResource.properties

# Optional header and footer for <errors/> tag.
errors.header=<ul><hr><h3>Errors</h3><ul>
errors.footer=</ul><hr>
errors.username.required=<li>valid username required
errors.password.required=<li>valid password required

# Struts Validator Error Messages
errors.required={0} is required.
errors.minlength={0} can not be less than {1} characters.
errors.maxlength={0} can not be greater than {1} characters.
errors.invalid={0} is invalid.

======================================================

Validation-rules.xml

<validator name="required"
classname="org.apache.struts.validator.FieldChecks"
method="validateRequired"
methodParams="java.lang.Object,
org.apache.commons.validator.ValidatorAction,
org.apache.commons.validator.Field,
org.apache.struts.action.ActionErrors,
javax.servlet.http.HttpServletRequest"
msg="errors.required">
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

<action path="/submitLogin" scope="request"

type="com.poshWebApp.actions.LoginAction" name="LoginForm"

validate="false" input="/web/pages/login/login.jsp">



According to this portion of the struts-config.xml file that you included in an earlier post, you have validation turned off for this action. Turn it on by specifying validate="true"
 
Nina Anderson
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah...I already noticed that and set validation=true, but that still did not help.
 
Nina Anderson
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I stopped the server, then re-started it again. The validation works fine now.

Thanks for all your help!!!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic