Dear Wiki user, You have subscribed to a wiki page or wiki category on "Struts Wiki" for change notification.
The following page has been changed by MichaelJouravlev: http://wiki.apache.org/struts/StrutsNavigationBasics ------------------------------------------------------------------------------ - == Web Resources And Navigation == + == Web Resources And Their Views == - A typical web application is a collection of web resources linked with each other. When using Struts, every web resource is represented with one or more action class/form bean pairs. When web resource is asked to render itself, a corresponding action class selects a view that reflects current state of the web resource, and sends it to the browser. + A typical web application is a collection of ''resources'' linked with each other. In Struts a web resource is represented with one or more pairs of an action class and a form bean. When a user requests a web resource to render itself, a corresponding action class selects an appropriate view and sends it to the browser. - Struts approach is different from page-oriented framewors like ASP.NET, where a user requests a page and web server displays it. In ASP.NET web resource is equal to web page, while in Struts one web resource can have several corresponding web pages. + Struts approach differs from page-oriented framewors like ASP.NET. In ASP.NET a web resource equals to web page, while in Struts one web resource can have several corresponding web pages. inline:basic_action_asp.gif - Struts allows to separate logical state of a web resource from its visual representation. Struts is agnostic to presentation technology, so a page can be generated using JSP file, Tile definition, Velocity template, XSLT stylesheet or other rendering techniques. + Struts provides a clean separation between logical state of a web resource from its visual representation. Struts is agnostic to presentation technology, so a page can be displayed using JSP file, Tile definition, Velocity template, XSLT stylesheet or other rendering techniques. ==== Example ==== @@ -18, +18 @@ * Exactly one result was found => outcome "single". * More than one result was found => outcome "multiple". + It is up to the application architect to decide to render the same resulting page for all outcomes or to render different page for every outcome. The code of the search action is not affected by this decision. - The search Action should return these three results as three separate logical outcomes. It is up to the application architect to decide to send all three outcomes to the "here's the list of responses" table page. It's also up to the application architect (perhaps later, in response to user feedback) to say "let's do this instead": - - * If there's no results, go to a page that says "sorry, no results were found, please try your search again." - * If there's exactly one response, go to the details page to display the corresponding data. - * If there's more than one response, go to the list page (as per the previous behavior). - - Note that the code of the search action is not affected by this decision. == Round Trip And Postback == - When a user interacts with a web resource, the browser may respond to some of the user actions by executing client-side scripts while some other actions that require server resources must be sent for processing to the web server. When server-side processing is involved, a typical interactive user session consists of the following steps: + When a user interacts with a web resource and server-side processing is involved, a typical interaction consists of the following steps: 1. The user requests a web resource from the web server. 2. Struts selects an action class that handles requests to that web resource. @@ -36, +30 @@ 4. The user enters the data into HTML form and submits it to the web server. 5. The action class processes the request, and sends the result back to the user. - The above interaction can be processed either with two different action classes or with one action class. + The above interaction can be handled either with two different action classes or with one action class. If two different action classes are involved, then the action class that renders a page is called ''render action'', while the class that processes submitted data is called ''submit action''. - If only one action class and one action form are used for both render and submit phases, then step 4 can also be referred to as a ''postback'', because input data is posted back to the same action that displayed the page. + If only one action class handles both render and submit phases, then step 4 can also be referred to as a ''postback'', because input data is posted back to the same action that displayed the page. Steps 4 and 5 are collectively referred to as a ''round trip''. - This model of execution allows a Web server to support a large number of clients because each client request occupies the server resources only for a short duration. + == Navigation Between Web Resources == + In a Struts application an action class selects an appropriate view, so a user cannot navigate to a specific view. What a user can navigate to is a specific action. Control can be transferred from action to action programmatically as well. + + === Navigation Via Link === + + An action may render a page that contains a link pointing to other actions. This way a user can manually navigate to another actions. + + === Navigation Via Postback === + + An action may render a page that contains an HTML form. When the form is submitted, the action can analyzed input data and either forward or redirect to another action. This way a user can be transferred to another web resource programmatically. + + Struts allows submitting an HTML form to an arbitrary action, but to keep things under control it is recommended to process all submits in original web resource's action and then transfer control to destination action instead of directly submitting HTML form to destination action. + + === In-Server Forwarding === + + Forwarding transfers the execution from the current action to the specified action on the same server. If original action has created a response stream, it is reused by the new action. + + After forwarding occurs, the browser shows the address of original action because the forwarding occurs on the server side and the browser remains unaware of it. + + === Redirecting Via Browser === + + With redirection, server creates a response whose header contains a 302 (Object Moved) status code and the target URL. When the browser receives this response from the server, it uses the header information to generate another request to the new URL. When using redirection, the transfer of control happens at the client side and involves two round trips to the server. + + You should use redirection in the following cases: + * You want to connect to a resource on some other web server. + * You want to connect to a non-Struts resource (such as an HTML file). + * You want to pass the query string as part of the URL. + * You want to have URL clean of parameters, passing them through session object. +