Struts Tag Library

  • July 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Struts Tag Library as PDF for free.

More details

  • Words: 2,148
  • Pages: 56
Struts Tag Library

1

Disclaimer & Acknowledgments ●

● ●

Even though Sang Shin is a full-time employees of Sun Microsystems, the contents here are created as their own personal endeavor and thus does not reflect any official stance of Sun Microsystems. Sun Microsystems is not responsible for any inaccuracies in the contents. Acknowledgments: – Struts' user's guide is also used in creating slides and speaker notes –

“Using the Struts framework” presentation material from Sue Spielman of Switchback Software ([email protected])

2

Revision History ● ●

11/10/2003: version 1: created by Sang Shin Things to do – Speaker notes need to be added to some slides

3

Agenda ● ● ●

Struts tag libraries Struts and JSTL Struts-EL

4

Struts Tag Libraries

5

Tag Libraries Overview ●

Number of taglibs included as part of Struts –



Bean tags –



Provides presentation logic tags that eliminate need for scriptlets

Template tags (Tiles in v1.1) –



Form bridge between JSP view and other components

Logic tags –



Tags for accessing Beans and their properties

Html tags –



Usage is not required, but helpful

Tags to form JSP templates that include parameterized content

Nested Tags (v1.1) – –

Allows for object hierarchy Helpful for rendering lists of lists 6

Access to Tag Libraries ●

All tag libraries are defined in web.xml using element /WEB-INF/struts-bean.tld /WEB-INF/struts-bean.tld /WEB-INF/struts-html.tld /WEB-INF/struts-html.tld

7

Bean Tags 8

Bean Tags ●



Tags for accessing beans and their properties (not altering, however) Enhancements to <jsp:useBean> –



Some of the attributes, for example id, name, property, and scope, share same meanings

Convenient mechanisms to create new beans based on the value of: – – –

User entered parameters Request headers Cookies 9

Attributes of Bean Tags ● ●

● ●

id - define a bean name - refer to an existing bean (the value is either the value of an id attribute in a previous tag, or is found in application, session, request, or page scope) property - a property from a bean scope - scope to search for the bean. If scope is not specified then the bean is searched for in page, request, session and application order 10

Bean Tags ● ● ● ● ● ● ● ● ●



11



For creating variables from beans and properties –





Without it, you would have to create Java codebased scripting variables in your JSP pages

The variables are used later in the JSP page For exposing Java objects (i.e. Collections) that are created in a Action class to a JSP

12

Examples:









Get a bean with a String constant Get an existing bean



Get a single property from a bean

13

Example1: favorites.jsp (ch 03) 2 3 4 5
6
1

14

Example2: view_favorites.jsp (ch 03) 1

2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

<script language="JavaScript"> function showMessage() { alert( "Hello, !" ); }

Thanks for responding, !
Click Me

You have indicated that your favorite colors are:

    15


  • Example2: FovoritesForm Class (ch 03) 1

    public final class FavoritesForm extends ActionForm {

    2 3 private static String[] javaIdes = new String[] {"Eclipse", "IDEA", "JBuilder", "JDeveloper", "NetBeans"}; 4 private static String[] csharpIdes = new String[] {"SharpDevelop", "Visual Studio"}; 5 6 public FavoritesForm() { 7 webLinks = new ArrayList(); 8 for (int i=0; i<5; i++) webLinks.add(new WebLink()); 9 colors = new String[3]; 10 colors[0]="Black"; 11 colors[1]="Blue"; 12 colors[2]="Red"; 13 } 14 ... 15 public String[] getColors() { 16 return colors; 17 }

    16







    Use it to output the contents of a bean's property The information returned to the page is rendered as a String Use it to encode and unencode information

    17

    Example1: <jsp:useBean id="dvd" class="hansen.playground.DVD" scope="request"/> ... <jsp:getProperty name="dvd" property="title"/> Using Struts you simply use the write tag:

    18

    Example2: submitAction.java 1 public final class SubmitAction extends Action { 2 3 // The execute() method is where you provide your business logic 4 public ActionForward execute(ActionMapping mapping, 5 ActionForm form, 6 HttpServletRequest request, 7 HttpServletResponse response) { 8 9 // Cast ActionForm object to SubmitForm type 10 SubmitForm f = (SubmitForm) form; 11 12 // Retrieve the value of lastname field 13 String lastName = f.getLastName(); 14 15 // Translate the lastname to upper case and save it Request scope 16 request.setAttribute("lastName", lastName.toUpperCase()); 17 18 // Create and return ActionForward object with "success" outcome 19 return (mapping.findForward("success")); 20 } 21 }

    19

    Example2: submit.jsp 1 2 3 4 5 6 7 8 9 10 11 12 13

    Hello young old

    20

    Example3: view_favorites.jsp (ch 03) 1

    2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

    <script language="JavaScript"> function showMessage() { alert( "Hello, !" ); }

    Thanks for responding, !
    Click Me

    You have indicated that your favorite colors are:

      21




    • Looks up a key in the resource file

      22

      Example: index.jsp (ch 03) 1

      <bean:message key="index.title"/>

      Struts Chapter 3 Examples



      2 3 4 5 6 7 8 9

      10 11



      23

      ● ●

      Get a request parameter Example –



      24

      HTML Tags 25

      HTML Tags ●







      Form bridge between JSP view and other components Input forms are important for gathering userentered data Most of the actions of the HTML taglib involve HTML forms Error messages, hyperlinking, internationalization

      26

      HTML Tags ● ● ● ● ● ● ● ● ● ● ●

      checkboxes hidden fields password input fields radio buttons reset buttons select lists with embedded option or options items option options submit buttons text input fields textareas 27

      HTML Tags ● ● ● ● ● ●



      28



      Simplest way to display error messages –





      It is expected that ActionErrors is created (either in the validate() method of an ActionForm class or in execute() method of an Action class)

      Place the tag anywhere on the page you want the list of errors to be displayed Iterates over the errors writing unescaped contents to the page –

      Messages need to have HTML tags, which are not desirable 29

      Example: submit.jsp 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

      <%@ page language="java" %> <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %> <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %> <%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %> Submit example

      Example Submit Page

      Last Name:
      Address:
      Sex: Male Female
      Married:
      Age: 0-19 20-49 50-

      30

      Example: ApplicationResources.properties 1 errors.header=

      Validation Error(s)

        2 3 error.lastName=
      • Enter your last name 4 error.address=
      • Enter your address 5 error.sex=
      • Enter your sex 6 error.age=
      • Enter your age 7 error.birthYear=
      • Enter the year you were born between 1900 and 2004 inclusive 8 9 errors.footer=



      31



      Corrects the problem of





      Allows you to keep HTML tags in JSP pages not in the resource file

      By default, it looks for error messages stored in the request scope The id attribute defines the name of the scripting variable used to expose the error message text

      32

      Logic Tags 33

      Logic Tags ●



      Provides presentation logic tags that eliminate need for scriptlets Value comparisons Include: = != <= >= < >



      Substring matching –



      Presentation logic –



      match, notmatch forward, redirect

      Collections –

      iterate

      34

      Logic Tags ● ●



      35

      & tags ●



      The body of the tag is evaluated whenever the JavaBean, or its property, is present within the JSP page Attributes for evaluation – – – – –

      name parameter cookie header property 36

      Example: submit.jsp 1 2 Hello 3 4 young 5 6 7 old 8 9 10 11 12 13

      37

      & tags ● ●

      Checks against a specific value in a bean Assumes the bean exists –





      Exception occurs if not

      tag compares the bean's toString() value aganst the value property If property attribute is specified, then the value attribute is compared against the bean's property 38

      Example: submit.jsp 1 2 Hello 3 4 young 5 6 7 old 8 9 10 11 12 13

      39

      Usage Example: & tags ●



      You want to present different messages or buttons on a page depending upon the type of action you migt perform Example: –

      – – –

      Depending what a user wants to do (mode) – view, edit, or delete, you want to present different set of buttons View mode: Show only view button Edit mode: Show view and edit buttons Delete mode: Show only delete button 40

      Example: subscription.jsp (ch 13) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

      <bean:message key="subscription.title.create"/> <bean:message key="subscription.title.delete"/> <bean:message key="subscription.title.edit"/> 41

      Template Tags 42

      Template Tags ●





      Templates are JSP pages that include parameterized content Useful for creating dynamic JSP templates for pages that share a common format Functionality provided is similar to what can be achieved using the standard JSP include directive, but these tags allow for dynamic rather than static content

      43

      Template Tags ●

      Three template tags work in an interrelated function: –

      Get - Gets the content from request scope that was put there by a put tag.



      Insert - Includes a template



      Put - Puts content into request scope

      44

      Template sample (insert/put)



      45

      layout.jsp <template:get name='title'/>


      46

      Struts and JSTL 47

      When to JSTL in your Struts application? ●







      Developers should evaluate when to use the JSTL Many of the Struts taglib features are now available in the JSTL It’s simple: If the tag exists in the JSTL – use it Continue using the Struts tags where appropriate, they will continue to be supported 48

      Interaction with JSTL ●

      Struts-el taglibs allow for using expression values instead of just rtexprvalue Runtime: Expression:









      Set of optional taglibs that can be used with the JSTL expression language (EL) Implements many (but not all) of the Struts tags. Located in the contrib folder of the Struts release Container with servlet 2.3 support required

      49

      Struts EL Tag library 50

      Struts EL Extension ● ●



      Extension of the Struts tag library Uses the expression evaluation engine in the Jakarta Taglibs implementation of the JSP Standard Tag Library (version 1.0) to evaluate attribute values Some of the Struts tags were not ported to this library –



      their functionality was entirely supplied by the JSTL

      Requires the use of the Struts tag library, and the Java Server Pages Standard Tag Library 51

      Tag Mapping ●



      Every Struts tag that provides a feature that is not covered by the JSTL (1.0) library is mapped into the Struts-EL library Bean Tag Library Tags NOT Implemented in Struts-EL – –

      cookie (in Struts): c:set, EL (in JSTL) define (in Struts), c:set, EL (In JSTL)

      52

      How to use Struts EL ●

      Struts –





      Struts EL –



      53

      Best Practice Guidelines 54

      Follow Good MVC Practice ●



      JSP pages must “know” as little as possible about the back-end architecture JSP page should only concern itself with rendering the view and not manipulating any data logic

      55

      Passion!

      56

Related Documents

Struts Tag Library
July 2020 3
Struts
June 2020 26
Struts
November 2019 49
Struts
May 2020 36