Cover | Table of Contents | Colophon
http://jakarta.apache.org/site/mail.html,
before joining. You can then join one or more of the
Jakarta Project's
mailing lists, including those for Struts, from the URL http://jakarta.apache.org/site/mail2.html.
http://jakarta.apache.org/struts/. For more
information on downloading and installing Struts, see Appendix B.
javax.servlet.http.HttpServletRequest
interface. Among other things, this
object contains a collection of key/value attribute pairs that can be
used to store objects for the lifetime of the request. The key of
each pair is a String, and the value can be any
type of Object. The methods to store objects in
and retrieve them from the request scope are:
public void setAttribute( String name, Object obj ); public Object getAttribute( String name );
removeAttribute( ) method; however,
because the scope of the attribute is only for the lifetime of the
request, it is not as important to remove them as it is for other
scoped attributes. Once the server fulfills a request and a response
is returned to the client, the request and its attributes are no
longer available to the client and may be garbage-collected by the
JVM.
HttpServletRequest
object from the URI query string and
data that is sent in a POST method. The parameters are formatted as
key/value pairs and are accessed by applications as request
parameters. URL parameters play an important role in all web
applications, and the Struts framework is no exception.
sendRedirect(
)
method is invoked, it causes the web
container to return to the browser a response indicating that a new
URL should be requested. Because the browser issues a completely new
request, any objects that are stored as request attributes before the
redirect occurs will be lost. This is one of the biggest differences
between a forward and redirect. Figure 2-5
illustrates why this occurs.
package com.oreilly.struts.chapter2examples;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.RequestDispatcher;
/**
* Perform a redirect for the page "redirect.jsp"
*/
public class RedirectServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
redirect( request, response );
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
redirect( request, response );
}
/**
* Set a few URL parameters and objects for the request to see what happens
* to them during a redirect.
*/
protected void redirect(HttpServletRequest req,HttpServletResponse resp)
throws ServletException, IOException{
log( "A request arrived for " + req.getServletPath( ) );
// Put some objects into request scope
req.setAttribute( "firstName", "John" );
req.setAttribute( "lastName", "Doe" );
String contextPath = req.getContextPath( );
String redirectStr = contextPath + "/result.jsp?username=foo&password=bar";
log( "redirecting to " + redirectStr );
// Always call the encodeRedirectURL( ) method when perfoming a redirect
resp.sendRedirect( resp.encodeRedirectURL(redirectStr) );
}
}
validator
package shown in Figure 3-4 does not represent the entire set of classes
and interfaces necessary for the Validator framework. These are only
the Struts-specific extensions necessary to use the Validator
framework with Struts.
action
package contains classes for the controller, some that are used by
the view domain, and even a few that probably would have been better
off in the util package. While it may be confusing
at first, it's not that hard to get accustomed to
where everything is. Table 3-1 enumerates the
top-level packages and provides brief descriptions of their purposes.
A few of these top-level packages also contain subpackages, which
will be covered in later chapters.
org.apache.struts.action.ActionServlet
class.
ActionServlet extends the
javax.servlet.http.HttpServlet
class and is responsible for packaging and routing HTTP traffic to
the appropriate handler in the framework. The
ActionServlet
class is not abstract
and therefore can be used as a concrete controller by your
applications. Prior to Version 1.1 of the Struts framework, the
ActionServlet was solely responsible for receiving
the request and processing it by calling the appropriate handler. In
Version 1.1, a new class, called
org.apache.struts.action.RequestProcessor
, was introduced to
process the request for the controller. The main reason for
decoupling the request processing from the
ActionServlet is to provide you with the
flexibility to subclass the ActionFormsActionForm objects are used in the
framework to pass client input data back and forth between the user
and the business layer. The framework automatically collects the
input from the request and passes this data to an
Action using a form bean, which then can be passed
along to the business layer. To keep the presentation layer decoupled
from the business layer, you should not pass the
ActionForm itself to the business layer; rather,
create the appropriate DTO using the data from the
ActionForm. The following steps illustrate how the
framework
processes an ActionForm for every request:
ActionForm has been configured.
ActionForm is configured for the action, use
the name attribute from the
action element to look up the form bean
configuration information.
ActionForm
already has been created.
ActionForm instance is present in the
appropriate scope and it's the same type as needed
for the new request, reuse it.
ActionForm and store it in the appropriate scope
(set by the scope attribute for the
actionActionServlet,
RequestProcessor, and Action
classes provide the controller components; the
Action communicates with your
application's model components; and the combination
of the ActionForm, DTOs, JSP pages, and tag
libraries make up the view.
http://www.somehost.com/storefront/index.jsp
into his browser's location bar, the JSP page will
be served from the root of the Storefront web application.
com.oreilly.struts.framework.StorefrontController