The Struts Tags : Struts « J2EE « Java






The Struts Tags

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


//ch08_01.jsp
<%@ taglib prefix="ch08" uri="WEB-INF/ch08_02.tld" %>
<HTML>
    <HEAD>
        <TITLE>Inserting Text With Custom Tags</TITLE>
    </HEAD>
    <BODY>
        <H1>Inserting Text With Custom Tags</H1>
        <ch08:message />
    </BODY>
</HTML>

//ch08_04.jsp

<%@ taglib prefix="ch08" uri="WEB-INF/ch08_05.tld" %>
<HTML>
  <HEAD>
        <TITLE>Handling Custom Tag Attributes</TITLE>
    </HEAD>
    <BODY>
        <H1>Handling Custom Tag Attributes</H1>
        <ch08:message text="This text was set using an attribute."/>
    </BODY>
</HTML>

//ch08_07.jsp
<%@ taglib prefix="ch08" uri="WEB-INF/ch08_08.tld" %>
<HTML>
    <HEAD>
        <TITLE>Creating Iterating Tags</TITLE>
    </HEAD>

    <BODY>
        <H1>Creating Iterating Tags</H1>
        <%
            String[] toppings = new String[]{ "Pepperoni", "Sausage", "Ham", "Olives" };
            pageContext.setAttribute("toppings", toppings);
        %>

        <ch08:iterator>
            Topping: 
        </ch08:iterator>
    </BODY>
</HTML>

//ch08_10.jsp
<%@ taglib prefix="ch08" uri="WEB-INF/ch08_11.tld" %>
<HTML>
    <HEAD>
        <TITLE>Creating Cooperating Tags</TITLE>
    </HEAD>

    <BODY>
        <H1>Creating Cooperating Tags</H1>
        <ch08:createToppings/>
        <ch08:iterator>
            Topping: 
        </ch08:iterator>
    </BODY>
</HTML>

//ch08_13.jsp
<%@ taglib prefix="ch08" uri="WEB-INF/ch08_15.tld" %>
<HTML>
    <HEAD>
        <TITLE>Custom Tags and Variables</TITLE>
    </HEAD>

    <BODY>
        <H1>Custom Tags and Variables</H1>
        <ch08:createToppings/>
        <%
            for(int loopIndex = 0; loopIndex < toppings.length; loopIndex++) {
                out.println("Topping: " + toppings[loopIndex] + "<BR>");
            }
        %>
    </BODY>
</HTML>

package ch08;

import javax.servlet.jsp.tagext.*;
import javax.servlet.jsp.*;

public class ch08_03 extends TagSupport 
{

  public int doEndTag() throws JspException 
{
    try {
      pageContext.getOut().print("This text is from the custom tag.");
    } catch (Exception e) {
      throw new JspException(e.toString());
    } 
    return EVAL_PAGE;
  } 
}



package ch08;

import javax.servlet.jsp.tagext.*;
import javax.servlet.jsp.*;

public class ch08_06 extends TagSupport 
{
  String text;

  public void setText(String s) {
    text = s;
  } 

  public int doEndTag() throws JspException 
{
    try {
      pageContext.getOut().print(text);
    } catch (Exception e) {
      throw new JspException(e.toString());
    } 
    return EVAL_PAGE;
  } 
}

package ch08;

import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;

public class ch08_09 extends TagSupport{
  private int iterationCounter = 0;
  private String[] toppings = null;

  public int doStartTag()
  {
    toppings = (String[]) pageContext.getAttribute("toppings");
    return EVAL_BODY_INCLUDE;
  }

  public int doAfterBody() throws JspException
  {
    try{
      pageContext.getOut().print(" " + toppings[iterationCounter] + "<BR>");
    } catch(Exception e){
      throw new JspException(e.toString());
    }
    iterationCounter++;
    if(iterationCounter >= toppings.length) {
      return SKIP_BODY;
    }
    return EVAL_BODY_AGAIN;
  }
}


package ch08;

import javax.servlet.jsp.tagext.*;

public class ch08_12 extends TagSupport 
{

  public int doStartTag() {
    String[] toppings = new String[] {"Pepperoni", "Sausage", "Ham", "Olives"};
    pageContext.setAttribute("toppings", toppings);

    return SKIP_BODY;
  } 
}

           
       








Struts-Essential-Skills-ch08.zip( 1,443 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.Struts: Creating the ControllerStruts: Creating the Controller
22.Creating Custom TagsCreating Custom 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