Struts: Creating the Model : Struts « J2EE « Java






Struts: Creating the Model

Struts: Creating the Model
/*
Title:       Struts : Essential Skills (Essential Skills)
Authors:     Steven Holzner
Publisher:   McGraw-Hill Osborne Media
ISBN:       0072256591
*/

//ch05_03.jsp

<%@ taglib uri="/tags/struts-bean" prefix="bean" %>
<%@ taglib uri="/tags/struts-html" prefix="html" %>

<HTML>
    <HEAD>
        <TITLE>Using Include Actions</TITLE>
    </HEAD>
    
    <BODY>
        <H1>Using Include Actions</H1>
        <html:form action="ch05_04.do">
            Enter some text
            <html:text property="text"/>
            <html:submit value="Submit"/>
        </html:form>
    </BODY>
</HTML>



//ch05_07.jsp
<%@ taglib uri="/tags/struts-bean" prefix="bean" %>
<%@ taglib uri="/tags/struts-html" prefix="html" %>

<HTML>
    <HEAD>
        <TITLE>Select an action to dispatch</TITLE>
    </HEAD>
    
    <BODY>
        <H1>Select an action to dispatch</H1>
        <html:form action="ch05_05.do">
            <html:select property="method">
                <html:option value="red">Red</html:option>
                <html:option value="yellow">Yellow</html:option>
                <html:option value="green">Green</html:option>
            </html:select>
            <BR>
            <BR>
            <BR>
            <html:submit />
        </html:form>
    </BODY>
</html>


//ch05_11.jsp
<%@ taglib uri="/tags/struts-bean" prefix="bean" %>
<%@ taglib uri="/tags/struts-html" prefix="html" %>

<HTML>
    <HEAD>
        <TITLE>Using LookupDispatchAction</TITLE>
    </HEAD>
    
    <BODY>
        <H1>Using LookupDispatchAction</H1>
        <html:form action="ch05_09.do">
            Select an action to dispatch:
            <html:select property="method">
                <html:option value="red">Red</html:option>
                <html:option value="yellow">Yellow</html:option>
                <html:option value="green">Green</html:option>
            </html:select>
            <BR>
            <BR>
            <BR>
            <html:submit />
        </html:form>
    </BODY>
</html>


//ch05_08.jsp
<HTML>
    <HEAD>
        <TITLE>Success</TITLE>
    </HEAD>
    
    <BODY>
        <H1>Success</H1>
        The action has been dispatched.
    </BODY>
</html>


//ch05_12.jsp
<HTML>
    <HEAD>
        <TITLE>Success</TITLE>
    </HEAD>
    
    <BODY>
        <H1>Success</H1>
        The action has been dispatched.
    </BODY>
</HTML>

package ch05;

import java.io.*;
import java.util.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import org.apache.struts.action.*;
import org.apache.struts.actions.DispatchAction;

public class ch05_05 extends DispatchAction 
{
  public ActionForward red(ActionMapping mapping,
    ActionForm form,
    HttpServletRequest request,
    HttpServletResponse response)
    throws IOException, ServletException {

        System.out.println("Stop.");

        return mapping.findForward("success");
    }

  public ActionForward yellow(ActionMapping mapping,
    ActionForm form,
    HttpServletRequest request,
    HttpServletResponse response)
    throws IOException, ServletException {

        System.out.println("Caution.");

        return mapping.findForward("success");
    }

  public ActionForward green(ActionMapping mapping,
    ActionForm form,
    HttpServletRequest request,
    HttpServletResponse response)
    throws IOException, ServletException {

        System.out.println("Go.");

        return mapping.findForward("success");
    }

}

package ch05;
import org.apache.struts.action.ActionForm;

public class ch05_02 extends ActionForm 
{
    
    private String text = "Hello";
    
    public String getText() 
    {
        return text;
    }
    
    public void setText(String text) 
    {
        this.text = text;
    }
}


package ch05;
import org.apache.struts.action.ActionForm;

public class ch05_10 extends ActionForm 
{
    
    private String method = "";
    
    public String getmethod() 
    {
        return method;
    }
    
    public void setMethod(String method) 
    {
        this.method = method;
    }
}


package ch05;

import java.io.*;
import java.util.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import org.apache.struts.action.*;
import org.apache.struts.actions.LookupDispatchAction;

public class ch05_09 extends LookupDispatchAction
{
  public ActionForward red(ActionMapping mapping,
    ActionForm form,
    HttpServletRequest request,
    HttpServletResponse response)
    throws IOException, ServletException {

        System.out.println("Stop.");

        return mapping.findForward("success");
    }

  public ActionForward yellow(ActionMapping mapping,
    ActionForm form,
    HttpServletRequest request,
    HttpServletResponse response)
    throws IOException, ServletException {

        System.out.println("Caution.");

        return mapping.findForward("success");
    }

  public ActionForward green(ActionMapping mapping,
    ActionForm form,
    HttpServletRequest request,
    HttpServletResponse response)
    throws IOException, ServletException {

        System.out.println("Go.");

        return mapping.findForward("success");
    }

   protected Map getKeyMethodMap()
   {
       Map map = new HashMap();
       map.put("ch05.red", "red");
       map.put("ch05.yellow", "yellow");
       map.put("ch05.green", "green");
       return map;
   }
}

package ch05;
import org.apache.struts.action.ActionForm;

public class ch05_06 extends ActionForm 
{
    
    private String method = "";
    
    public String getmethod() 
    {
        return method;
    }
    
    public void setMethod(String method) 
    {
        this.method = method;
    }
}

           
       








Struts-Essential-Skills-ch05.zip( 1,444 k)

Related examples in the same category

1.Exercise 1: Building your first Struts ApplicationExercise 1: Building your first Struts Application
2.Exercise 2: Improving your first Struts Application Exercise 2: Improving your first Struts Application
3.Exercise 3: Using JSTL, Struts-EL etcExercise 3: Using JSTL, Struts-EL etc
4.Struts Recipes: Build Struts with Ant
5.Using bean:resource to expose the struts.config.xml to your view
6.Create a pluggable validator for cross-form validation 2Create a pluggable validator for cross-form validation 2
7.Struts: Generate a response with XSL
8. Hibernate and Struts  Hibernate and Struts
9. In-container testing with StrutsTestCase and Cactus
10.Exercise 4: Applying Gof and J2EE Patterns:Deploy to WebLogic and Test
11.Exercise 5: Search, List, Action Chaining, Editable List Form
12.Exercise 6: Paging
13.Exercise 7: Better Form and Action Handling
14.Exercise 8: Creating Struts Modules
15.Exercise 9: Using Commons Validator with Struts
16.Exercise 10: Using Struts and Tiles
17.Essential Struts ActionEssential Struts Action
18.A Full Struts ApplicationA Full Struts Application
19.Struts Creating the ViewStruts Creating the View
20.Struts: Creating the ControllerStruts: Creating the Controller
21.Creating Custom TagsCreating Custom Tags
22.The Struts TagsThe Struts Tags
23.The Struts and TagsThe Struts and Tags
24.Web Services and the Validator and Tile PackagesWeb Services and the Validator and Tile Packages
25.Struts Framework: A Sample Struts ApplicationStruts Framework: A Sample Struts Application
26.Struts Framework Validator
27.Struts Framework: TilesStruts Framework: Tiles
28.Struts Framework: Declarative Exception Handling
29.Struts: Internationalizing Struts Applications
30.Securing Struts Applications
31.Testing Struts Applications
32.Struts exampleStruts example
33.Blank Struts templateBlank Struts template
34.Struts Framework
35.Struts: bank application
36.Struts applicationStruts application
37.Struts application 2Struts application 2