PATH Documentation > WebObjects

 

Using Direct Actions in JSP Pages

In this section you'll create a WebObjects component called FoodInquiry that contains a WOForm element with two WOTextFields and a WOSubmitButton. The FoodInquiry page is displayed by a direct action, which itself is invoked by a JSP page that provides the FoodInquiry component with initial values for its form elements using <wo:formValue> tags.

  1. Using a text editor, create a file with the following contents:
    <%-- LogIn.jsp --%>
    
    <%@ taglib uri="/WOtaglib" prefix="wo" %>
    
    <wo:directAction actionName="login" className="LoginAction"  bodyContentOnly="false">
        <wo:formValue key="VisitorName" value='<%= "enter name" %>' />
        <wo:formValue key="FavoriteFood" value='<%= "enter food" %>' />
    </wo:directAction>
  2. Save the file as LogIn.jsp in the JSP_Example/Servlet Resources/jsp directory.
  3. In Project Builder, create a component called FoodInquiry (make sure you assign it to the Application Server target).
  4. Add the visitorName and favoriteFood String keys to the component (create accessor methods). Also add the showFavoriteFood action returning the FavoriteFood component.

    When you're done, FoodInquiry.java should look like Listing 3-3. (Note that if you use WebObjects Builder to add the keys and the action, you need to add a couple of lines of code to the showFavoriteFood method.

    Listing 3-3 FoodInquiry.java

    import com.webobjects.foundation.*;
    import com.webobjects.appserver.*;
    import com.webobjects.eocontrol.*;
    import com.webobjects.eoaccess.*;
    
    public class FoodInquiry extends WOComponent {
        protected String visitorName;
        protected String favoriteFood;
    
        public FoodInquiry(WOContext context) {
            super(context);
        }
    
        public FavoriteFood showFavoriteFood() {
            FavoriteFood nextPage = (FavoriteFood)pageWithName("FavoriteFood");
        
            // set the properties of the FavoriteFood component
            nextPage.setVisitorName(visitorName);
            nextPage.setFavoriteFood(favoriteFood);
    	
            return nextPage;
        }
        
        public String visitorName() {
            return visitorName;
        }
        public void setVisitorName(String newVisitorName) {
            visitorName = newVisitorName;
        }
        
        public String favoriteFood() {
            return favoriteFood;
        }
        public void setFavoriteFood(String newFavoriteFood) {
            favoriteFood = newFavoriteFood;
        }
    }
  5. Edit the component using WebObjects Builder so that it looks like Figure 3-9.

    Figure 3-9 The FoodInquiry component in WebObjects Builder

    [image: ../art/food_inquiry_wo.gif]

    1. Bind the Submit button to the showFavoriteFood action.
    2. Enter Food Inquiry as the component's title.
    3. Enter "VisitorName" as the value for the name attribute of the WOTextField that corresponds to the Visitor Name label.

      [image: ../art/favorite_food_wo_name.gif]

    4. Enter "FavoriteFood" as the value for the name attribute of the WOTextField that corresponds to the Favorite Food label.

    5. Build the project and restart the servlet container.
  6. Add the loginAction method (listed below) to the DirectAction class.
    public WOActionResults loginAction() {
        FoodInquiry result = (FoodInquiry)pageWithName("FoodInquiry");
    
        // get form values
        String visitorName = request().stringFormValueForKey("VisitorName");
        String favoriteFood= request().stringFormValueForKey("FavoriteFood");
    
        // set the component's instance variables
        result.setVisitorName(visitorName);
        result.setFavoriteFood(favoriteFood);
    
        return result;
    }
  7. Finally, you need to add the JSP page that invokes the direct action. Using a text editor, create a file the following contents and save it in the jsp directory of the project as LogIn.jsp.
    <%-- LogIn.jsp --%>
    
    <%@ taglib uri="/WOtaglib" prefix="wo" %>
    
    <wo:directAction actionName="login" className="LoginAction"  bodyContentOnly="false">
        <wo:formValue key="FavoriteFood" value='<%= "enter food" %>' />
        <wo:formValue key="VisitorName"  value='<%= "enter name" %>' />
    </wo:directAction>

To view the output of the LogIn JSP page in Tomcat use the following URL:

http://localhost:8080/JSP_Example/jsp/LogIn.jsp

You should see a page like the one in Figure 3-10.


Figure 3-10 The output of LogIn.jsp

[image: ../art/log_in_jsp.gif]

 


© 2002 Apple Computer, Inc. (Last Updated January 3, 2002)