This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Struts and the fly likes how to formard a JSP to other JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "how to formard a JSP to other JSP " Watch "how to formard a JSP to other JSP " New topic
Author

how to formard a JSP to other JSP

yulina shu
Greenhorn

Joined: Mar 09, 2005
Posts: 11
Hi all,

I have problem to forward a jsp page to other jsp which is with different Action and ActionForm.

e.g.
in search.jsp, when click "Search" button, it goes to display SearchResult page, which combines with SearchResultForm and SearchResultAction.


Thanks and regards,
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
I'm a little unclear about what you are attemting to do, and what is going wrong, but one thing you should understand is that when you submit a form from a JSP, the action attribute of the <form> tag should be a struts action, not a JSP. Once the action has performed its functions, it should then forward to a JSP.


Merrill
Consultant, Sima Solutions
yulina shu
Greenhorn

Joined: Mar 09, 2005
Posts: 11
Hi Merrill,

Thanks so much for your reply.

I tried to forward to searchresult.jsp in searchAction, but it did not go to searchresult page instead of back to the same page. I used a button with onClick event, is this a problem?

I am sorry I can not remember my code, it is not in my hand now.

Thanks again.
Tim Manchester
Ranch Hand

Joined: Nov 03, 2004
Posts: 62
What you need are 10 things:
1. Your originating jsp (search.jsp)
2. Your jsp needs to know which action you're going to use (the following is an example of what the form tag in your jsp might look like):
<html:form action="/search">
3. The form for that jsp (searchForm.java)
4. The action for that jsp (SearchAction.java)
5. The jsp to forward to (searchResults.jsp)
6. Your jsp needs to know which action you're going to use (the following is an example of what the form tag in your jsp might look like):
<html:form action="/searchResults">
7. The form for that jsp (searchResultsForm.java)
8. The action for that jsp (searchResultsAction.java)
9. Both jsps need to be listed in your web.xml file
10. Your forms need to be listed in your struts-config.xml file (note, the following is an example, it is probably not exactly right for your use):
<form-beans>
<form-bean name="searchForm" type="searchForm"></form-bean>
<form-bean name="searchResultsForm" type="searchResultsForm"></form-bean>
</form-beans>
9. Lastly your actions and forwards need to be listed in your struts-config.xml file (again, the following example is probably not exactly right for you, it's just an example):
<action-mappings>
<action name="searchForm" path="/search" type="searchAction">
<forward name="searchResults" path="/searchResults"></forward>
</action>
</action-mappings>

It's not enough to have all the jsps and java classes in place. You need to tell the web server about the pages in the web.xml file and you need to tell struts what all the jsp/form/action/forward relationships are in sturts-config.xml.

It can be a bit much to digest, but after you do it a couple of times you'll get it.

Regards,

- DM_Tim

[ March 21, 2005: Message edited by: Tim Manchester ]
[ March 21, 2005: Message edited by: Tim Manchester ]
yulina shu
Greenhorn

Joined: Mar 09, 2005
Posts: 11
Thanks, Tim. I did define those forward-mapping and form-beans, and tried to forward to searchResult form searchAction, but it goes to the same search page instead of going to searchResult page. I don't know what it is wrong. Maybe I did not define form-bean correctly.

I will try to do it again following your steps and see what I can get.

Thanks,
Tim Manchester
Ranch Hand

Joined: Nov 03, 2004
Posts: 62
No, that's not it. You are forwarded by struts based on the forward in your action mapping (or global forward). Double check to make sure that the forward in your struts-config.xml is correct AND make sure that the page you want to foward to is mapped to the name you think it is in your web.xml file AND make sure your searchAction is picking the right forward in the execute method.

If you have all that lined up. You'll get to the right place.

- DM_Tim
yulina shu
Greenhorn

Joined: Mar 09, 2005
Posts: 11
Thanks, Tim.
yulina shu
Greenhorn

Joined: Mar 09, 2005
Posts: 11
Hi all,

I can go to searchResult page from search page now, but it did not excute the searchResultAction class before loading the result page.

Please point out where I made a mistake, thanks!

(1) defined the form

<form-bean name="ParticipantSearchForm"
type="com.caspar.advanceSearch.form.ParticipantSearchForm" />

(2) defined the action-mapping
<action path="/searchResultSumit"
type="com.caspar.advanceSearch.action.ParticipantSearchAction"
name="ParticipantSearchForm"
scope="request"
input="/pages/searchResult.jsp" >
<forward
name="success"
path="/pages/searchResult.jsp"/>
</action>

(3)in searchResult page, I have
<html:form action="/searchResultSumit" >

Thanks for help!
Tim Manchester
Ranch Hand

Joined: Nov 03, 2004
Posts: 62
What makes you think you didn't go into your SearchResultsAction.execute method ? Something had to tell struts where to go from your SearchResults page (the forward in your action class). Put a System.out statement in the execute method (and a breakpoint if you can) just to be sure you really aren't getting there. I suspect you are.

Regards,

- DM_Tim
yulina shu
Greenhorn

Joined: Mar 09, 2005
Posts: 11
Thanks Tim, I used debug and set a breakpoint in ParticipantSearchAction class, it never goes there. But it did go to ParticipantSearchForm.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to formard a JSP to other JSP
 
Similar Threads
JSP Page Scope
problem in html:image
How to update the browser address with the displayed page
ragarding usage of innerhtml
How to call to another JSP from one JSP using spring??