HELP. newbie <html:form action...> acting funny.

P

philoso

I dont know if I'm doing this right... but I've been stuck with this
for couple days. The struts controller is trying to make an extra
actionForm with <html:form action="/page2"> (i.e. I'm trying get to
page1 with "action=/page2" html tag. When struts gets there, it also
trys to make page2 actionForm.) hope what I've said makes sense.

Here is my files.
struts-config.xml
<struts-config>
<form-beans>
<form-bean name="untitled1ActionForm"
type="struts_test.Untitled1ActionForm" />
</form-beans>
<action-mappings>
<action name="untitled1ActionForm"
type="struts_test.Untitled1Action" validate="true" scope="request"
path="/page1">
<forward name="success" path="/page1.jsp" />
</action>
<action name="untitled2ActionForm"
type="struts_test.Untitled2Action" validate="true" scope="request"
path="/page2">
<forward name="success" path="/page2.jsp" />
</action>
</action-mappings>
</struts-config>


page1.jsp

<body>

<html:form action="/page2" >
<p><html:text property="T1" size="20"/></p>
<p><html:text property="T2" size="20"/></p>
<p><html:submit property="submit"/></p>
</html:form>
</body>



=== SO.. when I type in http://localhost:8080/struts/page1.do, the
struts controller makes untitled1ActionForm and untitled2ActionForm.
On page1.jsp, when I get rid of <html:form action="page2">, it does
not make untitle2ActionForm.
 
S

Sudsy

philoso said:
I dont know if I'm doing this right... but I've been stuck with this
for couple days. The struts controller is trying to make an extra
actionForm with <html:form action="/page2"> (i.e. I'm trying get to
page1 with "action=/page2" html tag. When struts gets there, it also
trys to make page2 actionForm.) hope what I've said makes sense.

Here is my files.
struts-config.xml
<struts-config>
<form-beans>
<form-bean name="untitled1ActionForm"
type="struts_test.Untitled1ActionForm" />
</form-beans>
<action-mappings>
<action name="untitled1ActionForm"
type="struts_test.Untitled1Action" validate="true" scope="request"
path="/page1">
<forward name="success" path="/page1.jsp" />
</action>
<action name="untitled2ActionForm"
type="struts_test.Untitled2Action" validate="true" scope="request"
path="/page2">
<forward name="success" path="/page2.jsp" />
</action>
</action-mappings>
</struts-config>

Your post was difficult to understand but the error here is clear:
the name attribute on an action is the name of the corresponding
form bean. In the action with path="/page2" you've specified that
you're going to be using a form bean named untitled2ActionForm but
that definition is missing.
The choice of attribute names in the design of Struts was unfortunate.
It's not immediately obvious that you're not defining a name for the
action; it's actually the key which associates a form bean with an
action.
It's as bad as the input attribute: only careful reading will uncover
that it's the location of the page to display if validation fails.
It shouldn't even be specified unless validate="true" is set.
In any case, define the form in your form-beans section and the error
should go away. Of course it doesn't mean that your fun is coming to
and end, especially if your form doesn't provide the mutators named
just so...
 
P

philoso

Hi Sudsy.
Thanks for your help.

It does not matter untitled2ActionForm is defined in <form-beans>. If
I have it defined, then it makes the untitled2actionForm, but its not
the form that I want. I only want to have untitled1ActionForm made.
(BTW, I'm trying to prepopulate the page1.jsp with instance of
untitled1ActionForm.)

If I have page1.jsp with "action=/food", the struts tries to make
actionForm for food(ofcourse if defined in <form-beans>)

I'm sorry if it was not clear enough. I really dont know how to
explain this problem clearly.
 
S

Sudsy

philoso said:
Hi Sudsy.
Thanks for your help.

It does not matter untitled2ActionForm is defined in <form-beans>. If
I have it defined, then it makes the untitled2actionForm, but its not
the form that I want. I only want to have untitled1ActionForm made.
(BTW, I'm trying to prepopulate the page1.jsp with instance of
untitled1ActionForm.)

If I have page1.jsp with "action=/food", the struts tries to make
actionForm for food(ofcourse if defined in <form-beans>)

I'm sorry if it was not clear enough. I really dont know how to
explain this problem clearly.

I answered a similar question within the last few days. There's
nothing you preventing you from using the same form in multiple
actions. If you want to propogate the field contents then you
just have to use the name attribute in your html:text and/or
html:hidden tags. You just can't use it in the first JSP in the
chain since it doesn't exist at that point. As you noted, the
form only gets instantiated(*) when the form is submitted and
before your AtionForm#validate or Action#execute is invoked.

(*) An existing instance can be reused if available.
 
P

philoso

Thanks for you info Sudsy.

This is how I'm getting to page1.
<action forward="main.jsp" path="main" />

And from main.jsp, I have
<a href="/untitled1Action.do">page1</a>

On course to /untitled1Action.do, struts creates untitled1ActionForm and
untitled1Action.
So far so good.

In untitled1Action.java, it does its business logic and returns
actionMapping.findForward("success");

In struts-config, it is made so that "success" forwards the mapping to
"page1.jsp"

And here is my problem.
When this gets forwarded, it "creates another actionForm" before it gets to
page1.jsp, namely untitle2ActionForm. (If <form-bean
name=untitled2ActionForm type="struts-test.Untitled2ActionForm" /> is
defined in struts-config. I dont know why this second actionForm is being
created. It always seems to follow the XXX in <html:form action=XXX> from
page1.jsp.( XXX=>page2 in this example. In which case, is defined in
struts-config <action name="untitled2ActionForm"
type="struts_test.Untitled2Action" validate="true" scope="request"
path="/page2"> ).
So in the end, it tries to use untitled2ActionForm instead of
untitled1ActionForm.

Is this how it suppose to be? or did I do something wrong?

Here is the snippet of debug info.

296078 [Thread-10] DEBUG org.apache.struts.util.RequestUtils - Get module
name for path /page1.do
296078 [Thread-10] DEBUG org.apache.struts.util.RequestUtils - Module name
found: default
296078 [Thread-10] DEBUG org.apache.struts.action.RequestProcessor -
Processing a 'GET' for path '/page1'
296125 [Thread-10] DEBUG org.apache.struts.action.RequestProcessor -
Setting user locale 'en_US'
296125 [Thread-10] DEBUG org.apache.struts.util.RequestUtils - Looking for
ActionForm bean instance in scope 'request' under attribute key
'untitled1ActionForm'
296187 [Thread-10] DEBUG org.apache.struts.util.RequestUtils - Creating
new ActionForm instance of type 'struts_test.Untitled1ActionForm'
<========== HERE IS CORRECT FORM ==========
296187 [Thread-10] DEBUG org.apache.struts.util.RequestUtils - -->
struts_test.Untitled1ActionForm@17599cc
296187 [Thread-10] DEBUG org.apache.struts.action.RequestProcessor -
Storing ActionForm bean instance in scope 'request' under attribute key
'untitled1ActionForm'
296187 [Thread-10] DEBUG org.apache.struts.action.RequestProcessor -
Populating bean properties from this request
296218 [Thread-10] DEBUG org.apache.commons.beanutils.BeanUtils -
BeanUtils.populate(struts_test.Untitled1ActionForm@17599cc, {})
296218 [Thread-10] DEBUG org.apache.struts.action.RequestProcessor -
Validating input form properties in actionFrom-validate
296218 [Thread-10] DEBUG org.apache.struts.action.RequestProcessor - No
errors detected, accepting input
296218 [Thread-10] DEBUG org.apache.struts.action.RequestProcessor -
Looking for Action instance for class struts_test.Untitled1Action
296218 [Thread-10] DEBUG org.apache.struts.action.RequestProcessor -
Returning existing Action instance in action-execute
296234 [Thread-10] DEBUG org.apache.struts.action.RequestProcessor -
processForwardConfig(ForwardConfig
[name=next,path=/page1.jsp,redirect=false,contextRelative=false])
296234 [Thread-10] DEBUG org.apache.struts.util.RequestUtils - Looking for
ActionForm bean instance in scope 'request' under attribute key
'untitled2ActionForm' <===== WHY??? =====
296234 [Thread-10] DEBUG org.apache.struts.util.RequestUtils - Creating
new ActionForm instance of type 'struts_test.Untitled2ActionForm'
<=======WHY??? ========
296234 [Thread-10] DEBUG org.apache.struts.util.RequestUtils - -->
struts_test.Untitled2ActionForm@1c45731
ApplicationDispatcher[/struts]: Servlet.service() for servlet debugjsp threw
exception
org.apache.jasper.JasperException: No getter method for property T1 of bean
org.apache.struts.taglib.html.BEAN <=== WELL, DUH~~ USING WRONG FORM. =====
at
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:2
54)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:295)
...
...
...
...
 
S

Sudsy

philoso said:
Thanks for you info Sudsy.

This is how I'm getting to page1.
<action forward="main.jsp" path="main" />

And from main.jsp, I have
<a href="/untitled1Action.do">page1</a>

On course to /untitled1Action.do, struts creates untitled1ActionForm and
untitled1Action.

Let's be perfectly clear: there is no mapping for a path of
"/untitled1Action". Did you mean "/page1.do"?
So far so good.

In untitled1Action.java, it does its business logic and returns
actionMapping.findForward("success");

In struts-config, it is made so that "success" forwards the mapping to
"page1.jsp"

The action mapping for path="/page1" contains an action-forward
with a name of "success" and a path of "/page1.jsp".
And here is my problem.
When this gets forwarded, it "creates another actionForm" before it gets to
page1.jsp, namely untitle2ActionForm. (If <form-bean
name=untitled2ActionForm type="struts-test.Untitled2ActionForm" /> is
defined in struts-config. I dont know why this second actionForm is being
created. It always seems to follow the XXX in <html:form action=XXX> from
page1.jsp.( XXX=>page2 in this example. In which case, is defined in
struts-config <action name="untitled2ActionForm"
type="struts_test.Untitled2Action" validate="true" scope="request"
path="/page2"> ).
So in the end, it tries to use untitled2ActionForm instead of
untitled1ActionForm.

Is this how it suppose to be? or did I do something wrong?

Struts attempts to create a new form based on the target action(s) in
form1.jsp. This is documented behaviour. There might be situations
where you want to pull something out of the ActionForm within the
html:form in the page.
There's a flowchart here:
<http://www.sudsy.net/technology/struts-arch.html>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top