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

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

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


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

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

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

            <html:checkbox property="checkbox"/>Check Me

            <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

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

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

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

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

            <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>

            <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

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


            <html:submit value="Submit"/>

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;

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) 
    { = 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 =, 0, 4096)) != -1) {
                byteStream.write(dataBuffer, 0, numberBytes);
            fileText = new String(byteStream.toByteArray());
        catch (IOException e) {
            return null;
        return fileText;

    public void reset(ActionMapping mapping, HttpServletRequest request) 

package ch06;

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");
       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