File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Post-Request-Get technique

 
Vadim Vararu
Ranch Hand
Posts: 147
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the basic architecture of my web-applications. I have a dispatcher that receives requests. Each requested URL has an "action" parameter that tell the dispatcher which Action object to use for execution.
I like it very much and i think it's the "standard" way. But, i don't like that when i do some tasks like "deleteSomething", or "addSomething".... it gets executet as many time, as i hit Refresh button from the browser.
I've read Bear's article about PRG technique, but i face other problems now, like errorReporting, that i used to put into the request object. Another problem is that Action.process() method always return a Page (implemented by myself) that contains the path to o JSP that gets forwarded. Using PRG technique i have to return from process method two kind of objects: either a path to a jsp in application, or a relative(or absolute) URL for redirecting. So, i have to check and decide: to redirect, or to forward?

Advice me please, how to implement this technique in a better way.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 33713
316
Eclipse IDE Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vadim,
That is correct. Your action decides whether it is repainting the form with an error message or the "result" page (which is the redirect.) Typically these actions are called "failure" or "success."
 
Vadim Vararu
Ranch Hand
Posts: 147
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeap, but could you please give me an idea?
Till now, my action classes returned just a Page(that incapsulates a String) - a relative file path of the jsp on the server and i just forwarded to this jsp. Now, situation changes and i can return either a Page, either a piece of URL (for redirect). So, i have to types of actions: one that returns a Page(a string containing jsp path to forward to), another one that returns a piece of URL(to redirect to). I can't find the best architecture solution... take a look, please, at my controller and advice me how to refactor in order to manage redirects, as well.



Here is an example of an action:
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 33713
316
Eclipse IDE Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a Struts question. Moving to our Struts forum.
 
Vadim Vararu
Ranch Hand
Posts: 147
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe this architecture is similar to struts', but i don't use struts.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 33713
316
Eclipse IDE Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See this article which contains an example using forwards in Struts 2.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This has nothing to do with Struts; he's right.

It seems like the redirect/forward decision needs to be encapsulated in the Page somehow. For example, in S1 the ActionForward had a "redirect" boolean configured in XML, Struts 2 has different result types, including a dispatcher (forward) or redirect. So when you determine the Page object being returned you'd also have to decide which it is, a forward or a redirect, and encode that in the Page.

The Page processor does the right thing based on whether it's a redirect or forward.

There aren't too many options for maintaining data across a redirect: you either keep it in session (even if only for a single redirect, like Rails' "flash" concept), keep it in a thread local (for singleton controllers, but prone to bugs, IMO), or... not sure what other easy solutions might be.
 
Vadim Vararu
Ranch Hand
Posts: 147
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes...session is a good solution. The only think i don't like is that i have to remeber to remove attributes from session at the bottom of each .jsp.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't, although you can. I don't think a JSP page should be used to control the lifespan of server-side objects, but that's just me. If they're short-lived objects, again like Rails' "flash" scope (they live for a single redirect) then they could be cleared out in a filter or similar framework mechanism.
 
Vadim Vararu
Ranch Hand
Posts: 147
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Usually, JSP is the last step in a request lifecycle..... am i right? Is it possible to set a filter somehow after the final jsp to clean the session? It's not a problem, actually, to do it at the end of each view, but on the other hand, it's not the business of view.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Filters can wrap requests (that's how GZip filters work, for example--after the response data is available they gzip it up).

I've not actually implemented this functionality with a filter, I've always done it within whatever framework I'm using, but since you can run code and modify things after the filter chain executes I don't see why it wouldn't work.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic