Struts: Creating the Controller : Struts « J2EE « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » J2EE » StrutsScreenshots 
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, 04096)) != -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
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.