ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

Learning Jakarta Struts, Part 2
Pages: 1, 2, 3

Determine the access path for each screen.

  • The login screen will be the default application page.
  • The welcome page will be accessed only upon a successful login by the user.
  • The error page will be accessed from any page that can generate an error.
  • Define the ActionMappings that correlate to the application business logic

    Related Reading

    Java Servlet Programming, 2nd EditionJava Servlet Programming, 2nd Edition
    By Jason Hunter with William Crawford
    Table of Contents
    Index
    Sample Chapter
    Full Description

    ActionMappings are basically the roadmap for the application. The ActionMappings define what happens at any given point in an application. ActionMappings are defined in the struts-config.xml file. ActionsMappings work with forwards to provide the flow of the application.



    Usually you build up ActionMapping information, such as the form name or input, as you continue your development. Primarily, this development step is meant to start forming the strawman of the struts-config.xml file. The forwards for an action indicate what will happen when the action class returns one of the defined MappingForwards. It is entirely possible to have many forwards available to an action. Each action class can define specific forwards that are relevant to that class. Also, keep in mind that it is possible to have global forwards. Think of it as an include file for each action. If a returned forward is not specific to the ActionMapping but is defined as a global forward, then the global definition will be used. Local forwards can always be used to override global forwards. In our sample below, if an "error" mapping is returned from an action, then Errorpage.jsp will be displayed, even though that forward is not specific to any action.

    You will see as you continue your development that this config file can get rather detailed in larger projects. In the sample below, taken from the StrutsSample struts-config.xml file, we can see global forward definitions and a sample action mapping. This mapping defines an action called "login" that is an instance of the class com.oreilly.actions.LoginAction. On a successful login the "success" forward will display Welcome.jsp. If a "failure" forward occurs, Login.jsp will be redisplayed. If an "error" forward is returned, then the global forward will be used and display Errorpage.jsp

    
    <!-- ========== Global Forward Definitions -->
    <global-forwards>
    <forward name="login" path="/Login.jsp"/>
    <forward name="error" path="/Errorpage.jsp"/>
    </global-forwards>
    
    <!-- ========== Action Mapping Definitions -->
    <action-mappings>
    <!-- Attributes of the <action> element -->
    <!--
    For other available attributes see the JavaDocs for 
    org.apache.struts.action.ActionMapping 
    
    path - The request URI path that is matched to select 
    this mapping. 
    
    type - Fully qualified Java class name of the 
    Action implementation class used by this mapping. 
    
    name - The name of the form bean defined in the 
    config file that this action will use. 
    
    unknown - Set to true if this action should be 
    configured as the default for this application to handle 
    all requests not handled by another action. Only one 
    action can be defined as a default within a single 
    application. 
    
    scope - Identifier of the scope ("request" or "session" 
    within which the form bean, if any, associated with this 
    action will be created 
    
    input - Context-relative path of the input form to which 
    control should be returned if a validation error is 
    encountered.
    
    validate - Set to true if the validate() method of the 
    action associated with this mapping should be called.
    
    forward elements - The set of ActionForwards locally 
    associated with this mapping.
    -->
    
    <!-- =================== -->
    <!-- O'Reilly Struts Sample Main Actions -->
    <!-- =================== -->
    <action path="/login"
    type="com.oreilly.actions.LoginAction"
    name="loginForm"
    scope="request"
    input="/Login.jsp">
    <forward name="success" path="/Welcome.jsp"/>
    <forward name="failure" path="/Login.jsp"/>
    </action>
    </action-mappings>

    As we have already talked about in the first article, struts-config.xml is the controller in the MVC model. It is well worth spending development time on this step to make sure your actions match your application requirements. Once all the pieces come into place, you will want to avoid a situation where you have to completely reorganize your actions due to poor definition at the beginning of the project.

    Our StrutsSample application requires only one action -- the login. Depending on the size of your application, there can be many actions required and defined. Once the ActionMappings are defined, you can go through each one and start putting together the ActionMapping classes and form classes.


    Pages: 1, 2, 3

    Next Pagearrow