Info |
---|
This tutorial assumes you've completed the Hello World tutorial and have a working Hello_World_Struts2_Ant (or Hello_World_Struts2_Mvn) helloworld project. The example code for this tutorial, Using_Tags_Struts2_Ant or Using_Tags_Struts2_Mvn, is available on Google Code - http://code.google.com/p/struts2-examples/downloads/list. After downloading and unzipping the file, you'll have a folder named Using_Tags_Struts2_Ant (or Using_Tags_Struts2_Mvn). In that folder will be a README.txt file with instructions on how to build and run the example applicationusing_tags, is available for checkout from the Struts 2 GitHub repository at https://github.com/apache/struts-examples. The example projects use Maven to manage the artifact dependencies and to build the .war files. |
In the Hello World lesson, we added to the index.jsp a Struts 2 url tag to create a hyperlink to the hello.action. This tutorial will explore the url and other Struts 2 tags further.
...
A very common use case in web applications is linking to other pages. In the Hello World tutorial we added to index.jsp a link to the hello.action using the Struts 2 url tag. Please refer to the url documentation for more information about the url tag.
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Basic Struts 2 Application - Welcome</title>
</head>
<body>
<h1>Welcome To Struts 2!</h1>
<p><a href="<s:url action='hello'/>">Hello World</a></p>
</body>
</html>
|
...
Examine the struts.xml configuration in the Hello World tutorial and you will find this:
Code Block | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
...
<action name="hello" class="org.apache.struts.helloworld.action.HelloWorldAction" method="execute">
<result name="success">/HelloWorld.jsp</result>
</action>
...
|
...
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
<s:url action="hello" var="helloLink">
<s:param name="userName">Bruce Phillips</s:param>
</s:url>
<p><a href="${helloLink}">Hello Bruce Phillips</a></p>
|
...
Add the following markup to index.jsp after the Hello Bruce Phillips link.
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
<p>Get your own personal hello by filling out and submitting this form.</p>
<s:form action="hello">
<s:textfield name="userName" label="Your name" />
<s:submit value="Submit" />
</s:form>
|
...
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
<form id="hello" name="hello" action="/Using_Tags_Struts2_Mvn/hello.action;jsessionid=3471d76027b5342cab44f297b567" method="post">
<table class="wwFormTable">
<tr>
<td class="tdLabel"><label for="hello_userName" class="label">Your name:</label></td>
<td><input type="text" name="userName" value="" id="hello_userName"/></td>
</tr>
<tr>
<td colspan="2"><div align="right"><input type="submit" id="hello_0" value="Submit"/>
</div></td>
</tr>
</table>
</form>
|
...
Struts 2 property tag
In the Hello World tutorial's example application on JSP HelloWorld.jsp was this code:
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
<s:property value="messageStore.message" />
|
The most common use of the property tag is used to "get" the value returned by calling a public get method (of the Action class) and then to include that value in the HTML returned to the browser.
As discussed in the Hello World tutorial, the value of "messageStore.message" instructs Struts 2 to first call method getMessageStore of the Action class. That method call returns a MessageStore object. The ".message" part instructs Struts 2 to call the getMessage method of the MessageStore object. The getMessage method returns a String which will be included in the HTML returned to the browser.
On One very useful feature of the Struts 2 property tag is that it will automatically convert the most common data types (int, double, boolean) to their String equivalents. To demonstrate this feature let's add a static int variable to class HelloWorldAction.
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
private static int helloCount = 0;
public int getHelloCount() {
return helloCount;
}
public void setHelloCount(int helloCount) {
HelloWorldAction.helloCount = helloCount;
}
|
Each time the execute method is called we'll increase helloCount by 1. So add this code to the execute method of class HelloWorldAction.
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
helloCount++;
|
Whenever a user clicks one of the links on page index.jsp (or submits the form), method execute of class HelloWorldAction will be run and the static field helloCount will be increased by one.
To include the value of the helloCount attribute in the HelloWorld.jsp we can use the Struts 2 property tag. Add the following to HelloWorld.jsp after the h2 tag.
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
<p>I've said hello <s:property value="helloCount" /> times!</p>
|
...
If the value returned by the get method is an object, then the property tag will cause Struts 2 to call the object's toString method. Of course, you should always override Class Object's toString method in your model classes. Add the following toString method to the MessageStore class:
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
public String toString() {
return message + " (from toString)";
}
|
Add the following to HelloWorld.jsp
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
<p><s:property value="messageStore" /></p>
|
...
We covered a lot in this tutorial, but we've really only scratched the surface of how to use the Struts 2 tags. Consult the Struts 2 Tag Reference for much more information about all the Struts 2 tags.
Next | Onward to Coding Struts 2 Actions |
---|---|
Prev | Return to Hello World Using Struts 2 |