Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to pass a dynamic parameter in struts-config.xml?

 
liang gu
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have 2 actions specified in struts-config.xml, MyAction1 and MyAction2. If MyAction1 is successful, it generate a dynamic id. I need to pass this dynamic id to MyAction2 and forward to the URL: MyAction2.do?id=dynamicId. But I don't how to pass this dynamic id in struts-config.xml.
<action path="/MyAction1"
type="MyAction1Class">
<forward name="succeeded" path="/MyAction2"/>
<forward name="failed" path="/error.jsp"/>
</action>
Any solutions or suggestions to my problem? Thanks a lot.
 
Matthew Phillips
Ranch Hand
Posts: 2676
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The config file is loaded when the app is first started, so you will not be able to do it that way. You could add the id to a session in MyAction1 and then have MyAction2 pull the value from the session.
[ November 20, 2002: Message edited by: Matthew Phillips ]
 
liang gu
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about doing a response.forward() in MyAction1 class?
response.forward("MyAction2.do?id="+dynamicId);
I know that struts-config.xml is supposed to control the application. By doing response.forward(), it is kind of breaking the MVC model a little bit.
Liang
 
Matthew Phillips
Ranch Hand
Posts: 2676
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That would probably work, but why reinvent the wheel. Since MyAction1 is already going to forward to MyAction2 by returning an ActionForward, all you need to do is add the id to something that MyAction2 can access.
 
Matthew Phillips
Ranch Hand
Posts: 2676
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I was reading Struts In Action I found another solution for you. It seems like it may be more appropriate to what you want to do.
 
liang gu
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Matthew:
The new solution you just gave is really and exactly what I needed. Thanks so much. And by the way, do you have a solution to the other post of mine?
------------------------------------------
I have two pages built by struts. The first page is the "add record" page. After click on "submit" button on this page, the request sent to the server, server then process "add record", then forward to a servlet to display the second page(if process is successful): the list of all records(including the one just added).
The problem I have now is that: if I refresh the second page(listing of all records), it will add another new record and then show the list of all records. Basically, when the page refreshed, the struts redo everything for the whole action(specifed in struts-config.xml), but all I want is only to refresh the list of all records. The following is part of my struts-config.xml I used:
<action path="/AddRecordCommit"
type="AddRecordCommitAction"
name="AddRecordForm"
scope="session"
input="/AllRecordList">
<forward name="succeeded" path="/servlet/showResultServlet"/>
<forward name="failed" path="/error.jsp"/>
</action>
 
Matthew Phillips
Ranch Hand
Posts: 2676
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by liang gu:
Hi, Matthew:
The new solution you just gave is really and exactly what I needed. Thanks so much. And by the way, do you have a solution to the other post of mine?
------------------------------------------
I have two pages built by struts. The first page is the "add record" page. After click on "submit" button on this page, the request sent to the server, server then process "add record", then forward to a servlet to display the second page(if process is successful): the list of all records(including the one just added).
The problem I have now is that: if I refresh the second page(listing of all records), it will add another new record and then show the list of all records. Basically, when the page refreshed, the struts redo everything for the whole action(specifed in struts-config.xml), but all I want is only to refresh the list of all records. The following is part of my struts-config.xml I used:
<action path="/AddRecordCommit"
type="AddRecordCommitAction"
name="AddRecordForm"
scope="session"
input="/AllRecordList">
<forward name="succeeded" path="/servlet/showResultServlet"/>
<forward name="failed" path="/error.jsp"/>
</action>

I might have one idea, but I will have to look it up tonight. I will reply to that post if I what I am looking for.
 
Adam Hardy
Ranch Hand
Posts: 567
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you guys want to use several lines of code for solving this when you can just put the information into the session or the request as an attribute, with only one line of code?
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic