You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Seth Milder <mr...@physics.gmu.edu> on 2002/10/23 20:07:11 UTC
Inner classes
I am designing a webapp using Struts and I would like to include with
every ActionForm class an inner servlet class that queries a DB to get
config info for the outer class. I would then use this information in
the validate() routine to determine which fields should be validated and
which should not be (it will basically be a list of required fields for
a given webapp). The trouble is that when I try to call the servlet I
get a stack trace (see below). Is there a better way of doing this with
Struts? Is a servlet in an inner class impossible?
Thanks for any insights or help on this.
Seth Milder
org.apache.catalina.INVOKER.edu.gmu.opentac2.webapp.RegistrationForm$Config
as unavailable
2002-10-23 13:08:43 invoker: Cannot allocate servlet instance for path
/usabo/servlet/edu.gmu.opentac2.webapp.RegistrationForm$Config
javax.servlet.ServletException: Error instantiating servlet class
edu.gmu.opentac2.webapp.RegistrationForm$Config
at
org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:901)
at
org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:658)
at
org.apache.catalina.servlets.InvokerServlet.serveRequest(InvokerServlet.java:401)
at
org.apache.catalina.servlets.InvokerServlet.doGet(InvokerServlet.java:180)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:740)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)
at
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)
at
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:260)
at
org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at
org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at
org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at
org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at
org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at
org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at
org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2397)
at
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)
at
org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at
org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:170)
at
org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
at
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:171)
at
org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
at
org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at
org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:174)
at
org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at
org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at
org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at
org.apache.catalina.connector.warp.WarpRequestHandler.handle(Unknown Source)
at
org.apache.catalina.connector.warp.WarpConnection.run(Unknown Source)
at java.lang.Thread.run(Thread.java:536)
----- Root Cause -----
java.lang.InstantiationException:
edu.gmu.opentac2.webapp.RegistrationForm$Config
at java.lang.Class.newInstance0(Class.java:281)
at java.lang.Class.newInstance(Class.java:249)
at
org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:892)
at
org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:658)
at
org.apache.catalina.servlets.InvokerServlet.serveRequest(InvokerServlet.java:401)
at
org.apache.catalina.servlets.InvokerServlet.doGet(InvokerServlet.java:180)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:740)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)
at
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)
at
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:260)
at
org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at
org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at
org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at
org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at
org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at
org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at
org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2397)
at
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)
at
org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at
org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:170)
at
org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
at
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:171)
at
org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
at
org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at
org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:174)
at
org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at
org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at
org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at
org.apache.catalina.connector.warp.WarpRequestHandler.handle(Unknown Source)
at
org.apache.catalina.connector.warp.WarpConnection.run(Unknown Source)
at java.lang.Thread.run(Thread.java:536)
2002-10-23 13:08:43 StandardWrapperValve[invoker]: Servlet.service() for
servlet invoker threw exception
javax.servlet.ServletException: Cannot allocate servlet instance for
path /usabo/servlet/edu.gmu.opentac2.webapp.RegistrationForm$Config
at
org.apache.catalina.servlets.InvokerServlet.serveRequest(InvokerServlet.java:420)
at
org.apache.catalina.servlets.InvokerServlet.doGet(InvokerServlet.java:180)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:740)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)
at
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)
at
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:260)
at
org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at
org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at
org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at
org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at
org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at
org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at
org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2397)
at
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)
at
org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at
org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:170)
at
org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
at
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:171)
at
org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
at
org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at
org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:174)
at
org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at
org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at
org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at
org.apache.catalina.connector.warp.WarpRequestHandler.handle(Unknown Source)
at
org.apache.catalina.connector.warp.WarpConnection.run(Unknown Source)
at java.lang.Thread.run(Thread.java:536)
----- Root Cause -----
java.lang.InstantiationException:
edu.gmu.opentac2.webapp.RegistrationForm$Config
at java.lang.Class.newInstance0(Class.java:281)
at java.lang.Class.newInstance(Class.java:249)
at
org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:892)
at
org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:658)
at
org.apache.catalina.servlets.InvokerServlet.serveRequest(InvokerServlet.java:401)
at
org.apache.catalina.servlets.InvokerServlet.doGet(InvokerServlet.java:180)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:740)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)
at
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)
at
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:260)
at
org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at
org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at
org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at
org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at
org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at
org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at
org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2397)
at
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)
at
org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at
org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:170)
at
org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
at
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:171)
at
org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
at
org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at
org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:174)
at
org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at
org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at
org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at
org.apache.catalina.connector.warp.WarpRequestHandler.handle(Unknown Source)
at
org.apache.catalina.connector.warp.WarpConnection.run(Unknown Source)
at java.lang.Thread.run(Thread.java:536)
--
Seth Milder
Department of Physics and Astronomy
MS 3f3
George Mason University
Fairfax, VA
http://www.mrseth.org
--
"The question is complex and life is short." --Protagoras on the
existence of the gods
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>
Re: Inner classes
Posted by Seth Milder <mr...@physics.gmu.edu>.
Joe Germuska wrote:
> <snip>
Thanks a lot! These are exactly the sorts of things I was looking for.
You are a great asset to this list.
--
Seth Milder
Department of Physics and Astronomy
MS 3f3
George Mason University
Fairfax, VA
http://www.mrseth.org
--
The big thieves hang the little ones. CZECH PROVERB
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>
Re: Inner classes
Posted by Joe Germuska <Jo...@Germuska.com>.
>>If you want your actions to be able to deal with multiple forms for some
>>reason, you could write a Struts PlugIn that puts itself into the
>>ServletContext at initialization time, and then is available to all of
>>your actions to validate the ActionForms.
>
>Intersting. Can you give me a link? Or an example? Sorry if I seem obtuse.
Check out the Struts source code
<http://jakarta.apache.org/site/cvsindex.html> and look at the
ValidatorPlugIn or the TilesPlugIn, or just check out the Java Doc
for the class (note this is Struts 1.1):
<http://jakarta.apache.org/struts/api/org/apache/struts/action/PlugIn.html>
Basically, PlugIn is a simple interface (two methods, init and
destroy); you tell Struts about your PlugIn class in the
struts-config.xml file, and Struts creates an instance of that class
at initialization time and calls init(ActionServlet servlet,
ApplicationConfig config). When I've used it, I have my PlugIn
implementation call
servlet.getServletContext().setAttribute("some.key",this); Then all
my actions have access to the servlet context and can request the
same class.
Your plugin could have a method like "validate(ActionForm)", and then
your actions could just find the plugin and pass the form to it, and
you could hide all the complexity of how the forms get validated in
the plugin.
>>Is there a strong reason to have the validation rules in a database
>>instead of in an XML file?
>
>No. But I want them to be configurable at runtime. I suppose I could
>do that in an XML file, but databases are more natural for me
>personally.
Understandable. Check out Commons Digester if you want to see just
how easy it can be to pull XML into your java environment without
screwing around with DOM or SAX...
>> Have you checked out the Validator system?
>
>No. What is that?
commons-validator is a system for validating beans based on an XML
configuration file. It was originally written for Struts, and then
was broken down into a non-struts validating core plus some pieces to
allow you to use it more easily in Struts.
I haven't used it myself yet, because I usually don't have supremely
complicated form validation and I find it easier to do it in my
actions.
The JavaDoc is here
<http://jakarta.apache.org/struts/api/org/apache/struts/validator/package-summary.html#package_description>
It may be that some more avid users could point you to more
documentation...
Joe
PS Here's an example of how I initialize my latest plugin. This
method is called from the main "init()" method. MailController is
the class which my plugin manages and puts into context. Its
"parse()" method essentially passes the XML file identified by
"config" over to commons Digester. This basic mechanism would work
for just about any kind of class that you want to have initialized at
application startup and available to all of your Actions.
private void initializeController(ActionServlet servlet) throws
ServletException {
URL configURL = null;
try {
configURL = servlet.getServletContext().getResource(this.config);
}
catch (MalformedURLException ex) { }
if (configURL == null)
{
configURL =
Thread.currentThread().getContextClassLoader().getResource(this.config);
}
if (configURL == null) throw new ServletException("No resource
found for " + this.config);
try {
this.controller = MailController.parse(configURL.openStream());
}
catch (IOException ex) {
throw new ServletException("error reading " + configURL,ex);
}catch (EmailException ex) {
throw new ServletException("error parsing " + configURL,ex);
}
servlet.getServletContext().setAttribute(this.key, this.controller);
}
--
--
* Joe Germuska { joe@germuska.com }
"It's pitiful, sometimes, if they've got it bad. Their eyes get
glazed, they go white, their hands tremble.... As I watch them I
often feel that a dope peddler is a gentleman compared with the man
who sells records."
--Sam Goody, 1956
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>
Re: Inner classes
Posted by Seth Milder <mr...@physics.gmu.edu>.
Joe Germuska wrote:
> At 2:07 PM -0400 2002/10/23, Seth Milder wrote:
>
> > I am designing a webapp using Struts and I would like to include with
> > every ActionForm class an inner servlet class that queries a DB to get
> > config info for the outer class. I would then use this information in
> > the validate() routine to determine which fields should be validated
> > and which should not be (it will basically be a list of required
> > fields for a given webapp). The trouble is that when I try to call the
> > servlet I get a stack trace (see below). Is there a better way of
> > doing this with Struts? Is a servlet in an inner class impossible?
>
>
> whoa -- that's twisted!
I've been called that before :)
> I don't know if the Servlet spec supports inner
> classes as servlets, but the design intentions of Struts are for the
> ActionForm classes to be relatively stupid objects used to push data
> around.
>
> Why not just have your Action class do the database stuff? There's no
> strict reason why you need to validate action forms with the validate()
> method; it's just a convenience.
Maybe you are right. I am new to all of this, so my ideas may not be
very good.
>
> If you want your actions to be able to deal with multiple forms for some
> reason, you could write a Struts PlugIn that puts itself into the
> ServletContext at initialization time, and then is available to all of
> your actions to validate the ActionForms.
Intersting. Can you give me a link? Or an example? Sorry if I seem obtuse.
>
> Is there a strong reason to have the validation rules in a database
> instead of in an XML file?
No. But I want them to be configurable at runtime. I suppose I could do
that in an XML file, but databases are more natural for me personally.
> Have you checked out the Validator system?
No. What is that?
>
> There's got to be a less tangled way to achieve your ends...
>
> Joe
I hope so. Thanks for the speedy reply. I knew this list would set me
straight :)
--
Seth Milder
Department of Physics and Astronomy
MS 3f3
George Mason University
Fairfax, VA
http://www.mrseth.org
--
Most people prefer certainty to truth.
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>
Re: Inner classes
Posted by "V. Cekvenich" <vi...@users.sourceforge.net>.
+1
.V
Joe Germuska wrote:
> At 2:07 PM -0400 2002/10/23, Seth Milder wrote:
>
>> I am designing a webapp using Struts and I would like to include with
>> every ActionForm class an inner servlet class that queries a DB to get
>> config info for the outer class. I would then use this information in
>> the validate() routine to determine which fields should be validated
>> and which should not be (it will basically be a list of required
>> fields for a given webapp). The trouble is that when I try to call the
>> servlet I get a stack trace (see below). Is there a better way of
>> doing this with Struts? Is a servlet in an inner class impossible?
>
>
> whoa -- that's twisted! I don't know if the Servlet spec supports inner
> classes as servlets, but the design intentions of Struts are for the
> ActionForm classes to be relatively stupid objects used to push data
> around.
>
> Why not just have your Action class do the database stuff? There's no
> strict reason why you need to validate action forms with the validate()
> method; it's just a convenience.
>
> If you want your actions to be able to deal with multiple forms for some
> reason, you could write a Struts PlugIn that puts itself into the
> ServletContext at initialization time, and then is available to all of
> your actions to validate the ActionForms.
>
> Is there a strong reason to have the validation rules in a database
> instead of in an XML file? Have you checked out the Validator system?
>
> There's got to be a less tangled way to achieve your ends...
>
> Joe
>
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>
Re: Inner classes
Posted by Joe Germuska <Jo...@Germuska.com>.
At 2:07 PM -0400 2002/10/23, Seth Milder wrote:
>I am designing a webapp using Struts and I would like to include
>with every ActionForm class an inner servlet class that queries a DB
>to get config info for the outer class. I would then use this
>information in the validate() routine to determine which fields
>should be validated and which should not be (it will basically be a
>list of required fields for a given webapp). The trouble is that
>when I try to call the servlet I get a stack trace (see below). Is
>there a better way of doing this with Struts? Is a servlet in an
>inner class impossible?
whoa -- that's twisted! I don't know if the Servlet spec supports
inner classes as servlets, but the design intentions of Struts are
for the ActionForm classes to be relatively stupid objects used to
push data around.
Why not just have your Action class do the database stuff? There's
no strict reason why you need to validate action forms with the
validate() method; it's just a convenience.
If you want your actions to be able to deal with multiple forms for
some reason, you could write a Struts PlugIn that puts itself into
the ServletContext at initialization time, and then is available to
all of your actions to validate the ActionForms.
Is there a strong reason to have the validation rules in a database
instead of in an XML file? Have you checked out the Validator system?
There's got to be a less tangled way to achieve your ends...
Joe
--
--
* Joe Germuska { joe@germuska.com }
"It's pitiful, sometimes, if they've got it bad. Their eyes get
glazed, they go white, their hands tremble.... As I watch them I
often feel that a dope peddler is a gentleman compared with the man
who sells records."
--Sam Goody, 1956
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>