Struts: Creating the Controller : Struts « J2EE « Java






Struts: Creating the Controller

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


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

<html:html>
    <head>
        <title>Using &lt;html&gt; Tags</title>
        <script language="JavaScript">
            function clicker()
            {
                confirm("You clicked the button.");
            }
        </script>
    </head>
    
    <body>
        <h1>Using &lt;html&gt; Tags</h1>

        <html:errors/>

        <html:form action="ch06_02.do" method="POST" enctype="multipart/form-data">

            <h2>Text Fields:</h2>
            <html:text property="text"/>
            <br>

            <h2>Text Areas:</h2>
            <html:textarea property="textarea" rows="10"/>
            <br>

            <h2>Checkboxes:</h2>
            <html:checkbox property="checkbox"/>Check Me
            <br>

            <h2>Radio Buttons:</h2>
            <html:radio property="radio" value="red"/>red
            <html:radio property="radio" value="green"/>green
            <html:radio property="radio" value="blue"/>blue
            <br>

            <h2>Buttons:</h2>
            <html:button onclick="clicker()" value="Click Me" property="text"/>
            <br>

            <h2>Links:</h2>
            <html:link action="ch06_02">Click Me</html:link>
            <br>

            <h2>Images:</h2>
            <html:img page="/image.jpg"/>
            <br>

            <h2>Image Controls:</h2>
            <html:image page="/imagecontrol.jpg" property=""/>
            <br>

            <h2>Select Controls:</h2>
            <html:select property="multipleSelect" size="9" multiple="true">
                <html:option value="Multi 0">Multi 0</html:option>
                <html:option value="Multi 1">Multi 1</html:option>
                <html:option value="Multi 2">Multi 2</html:option>
                <html:option value="Multi 3">Multi 3</html:option>
                <html:option value="Multi 4">Multi 4</html:option>
                <html:option value="Multi 5">Multi 5</html:option>
                <html:option value="Multi 6">Multi 6</html:option>
                <html:option value="Multi 7">Multi 7</html:option>
                <html:option value="Multi 8">Multi 8</html:option>
            </html:select>

            <h2>Multibox Controls:</h2>
                <html:multibox property="multiBox" value="a" />a
                <html:multibox property="multiBox" value="b" />b
                <html:multibox property="multiBox" value="c" />c
                <html:multibox property="multiBox" value="d" />d
                <html:multibox property="multiBox" value="e" />e
            <br>

            <h2>File Controls:</h2>
            <html:file property="file" />

            <br>
            <br>

            <html:submit value="Submit"/>
            <html:cancel/>
        </html:form>
    </body>
</html:html>

package ch06;
import org.apache.struts.action.*;
import org.apache.struts.upload.FormFile;
import org.apache.struts.upload.MultipartRequestHandler;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class ch06_03 extends ActionForm 
{
    
    private String text = "";
    private String X;
    private String Y;
    private String textarea = "";
    private String[] selectItems = new String[3];
    private String[] multiBox = new String[5];
    private boolean checkbox = false;
    private String radio = "";
    private FormFile file;
    private String fileText;
    
    public String getText() 
    {
        return text;
    }
    
    public void setText(String text) 
    {
        this.text = text;
    }
    
    public String getTextarea() 
    {
        return textarea;
    }
    
    public void setTextarea(String textarea) 
    {
        this.textarea = textarea;
    }

    public boolean getCheckbox() 
    {
        return checkbox;
    }
    
    public void setCheckbox(boolean checkbox) 
    {
        this.checkbox = checkbox;
    }
    
    public String getRadio() 
    {
        return radio;
    }
    
    public void setRadio(String radio) 
    {
        this.radio = radio;
    }
    
    public String getX() 
    {
        return X;
    }
    
    public void setX(String X) 
    {
        this.X = X;
    }
    
    public String getY() 
    {
        return Y;
    }
    
    public void setY(String Y) 
    {
        this.Y = Y;
    }

    public String[] getSelectItems() 
      {
        return selectItems;
    }
    
    public void setSelectItems(String[] selectItems) 
    {
        this.selectItems = selectItems;
    }
    
    public String[] getMultiBox() 
      {
        return multiBox;
    }
    
    public void setMultiBox(String[] multiBox) 
    {
        this.multiBox = multiBox;
    }
    
    private String[] multipleSelect = {"Multi 3", "Multi 5", "Multi 7"};

    public String[] getMultipleSelect() {
        return (this.multipleSelect);
    }

    public void setMultipleSelect(String multipleSelect[]) {
        this.multipleSelect = multipleSelect;
    }

    public FormFile getFile() {
        return file;
    }

    public void setFile(FormFile file) {
        this.file = file;
    }

    public String getFileText() {

        try {
            ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
            InputStream input = file.getInputStream();

            byte[] dataBuffer = new byte[4096];
            int numberBytes = 0;
            while ((numberBytes = input.read(dataBuffer, 0, 4096)) != -1) {
                byteStream.write(dataBuffer, 0, numberBytes);
            }
            fileText = new String(byteStream.toByteArray());
            input.close();
        }
        catch (IOException e) {
            return null;
        }
        return fileText;
    }

    public void reset(ActionMapping mapping, HttpServletRequest request) 
    {
    }
}

package ch06;

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

public class ch06_02 extends Action 
{
  public ActionForward execute(ActionMapping mapping,
    ActionForm form,
    HttpServletRequest request,
    HttpServletResponse response)
    throws IOException, ServletException {

        ActionErrors actionerrors = new ActionErrors();
                
        ch06_03 dataForm = (ch06_03)form;
        
        String text = dataForm.getText();        
        if(text.trim().equals("")) {
            actionerrors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.notext"));
        }
        
        if(actionerrors.size() != 0) {            
            saveErrors(request, actionerrors);
            return new ActionForward(mapping.getInput());            
        }
        return mapping.findForward("success");
    }
}
           
       








Struts-Essential-Skills-ch06.zip( 1,456 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 ModelStruts: Creating the Model
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