Struts 2 > Home > Tutorials > Step by Step > TutorialLesson04-01 |

Struts 2
TutorialLesson04-01
Edit Page Browse Space Add Page Add News
Added by vitorsouza, last edited by Ted Husted on May 28, 2006 (view change) show comment hide comment
Comment:
Update parent page
View page history

# Lesson 4.1: Using JSP as the View

When using JSP to render the views, you can choose to access the action's data using scriptlets or tags. Tags are the recommended approach.

## Accessing Action Data through Scriplets:

Action data can be accessed through an object called Value Stack. The example below does the same thing as the result page of lesson 3's second example (Supplying Data to the Action), but using scriptlets:

    <%@ page import="com.opensymphony.xwork.util.OgnlValueStack" %>
    <html>
    <head>
    <title>WebWork Tutorial - Lesson 4.1 - Lesson 3's example modified</title>
    </head>
    <body>

    <%
    OgnlValueStack stack = (OgnlValueStack)request.getAttribute("webwork.valueStack");
    out.write("Hello, " + stack.findValue("person"));
    %>

    </body>
    </html>

WebWork tags, however, are recommended over scriptlets. For instance, `<ww:property />` tags do exactly what the scriptlet above does, with a cleaner syntax and also handles the case where the Value Stack doesn't exist.

## WebWork Tag Library:

We've already showed in lesson 3's example how to access an action's property using tags. This section describes and exemplifies the use of the WebWork Tag Library, which can be divided in seven categories:

- Common tags: the most frequently used, basic tags;
- Componentisation tags: foster componentisation within your views;
- Flow control tags: govern the flow of control within the JSP page;
- Iteration tags: iterate over elements and manipulate iterable objects;
- UI tags: generate HTML form fields and controls;
- VUI tags: volunteers needed to write this part;
- Internationalisation tags: internationalise your views.

### Common tags

`<ww:property />` | Gets the value of a result attribute. If the value isn't given, the top of the stack will be returned.
`<ww:push />` | Pushes a value onto the Value Stack.
`<ww:param />` | Sets a parent tag's parameter. This tag is used only inside another tag to set the value of some property of the parent tag.
`<ww:set />` | Sets the value of an object in the Value Stack to a scope (page, stack, application, session). If the value is not given, the top of the stack is used. If the scope is not given, the default scope of "webwork" is used.
`<ww:url />` | Builds an encoded URL.

    EXAMPLE NEEDED.

### Componentisation tags

`<ww:action />` | Executes an Action from within the context of a taglib. The body of the tag is used to display the results of the action invocation.
`<ww:bean />` | Creates a JavaBean, instantiate its properties and place it in the ActionContext for later use.
`<ww:include />` | Includes another page or action.

    EXAMPLE NEEDED.

### Flow control tags

This if-else set of tags works just like if-else scriptlets.

`<ww:if />` | Conditional execution path. That is, evaluates the tag body if a boolean expression is true.
`<ww:else />` | Negative execution path for the if tag. That is, if the preceeding conditional tag's boolean expression evaluated to false, then evaluate this tag's body.
`<ww:elseif />` | Negative conditional execution path for the if tag. That is, if the preceeding conditional tag's boolean expression evaluated to false and if this tag's boolean expression evaluates to true, then evaluate this tag's body.

    EXAMPLE NEEDED.

### Iteration tags

`<ww:iterator />` | Iterates over a collection.
`<ww:generator />` | Generates iterators.
`<ww:append />` | Appends several iterators.
`<ww:subset />` | Gets a subset of an iterator.
`<ww:merge />` | Merges several iterators into one.
`<ww:sort />` | Sorts an iterator.

    EXAMPLE NEEDED.

### UI tags

The UI tags wrap generic HTML controls while providing tight integration with the core framework. The tags have been designed to minimize the amount of logic in compiled code and delegate the actual rendering of HTML to a template system. The UI tags attempt to cover the most common scenarios, while providing a Component Tag for creating custom components. The UI tags also provide built-in support for displaying inline error messages.

There is a separate lesson about WebWork UI Tags which explains in detail how they work, how you could cusomize their appearance through the use of templates, how to create custom components, etc.

Go to WebWork UI Tags Lesson.

### VUI(Voice UI) tags

`<ww:audio />` | ???
`<ww:prompt />` | ???
`<ww:filled />` | ???
`<ww:log />` | ???

Volunteers needed to write this part.

### Internationalisation tags

`<ww:text />` | Prints out an internationalized string.
`<ww:i18n />` | Places a resource bundle on the Value Stack, for access by the text tag.

Previous Lesson | Next Lesson