Info |
---|
This tutorial assumes you've completed the Hello World tutorial and have a working helloworld project. The example code for this tutorial, using_tags, is available for checkout from the Struts 2 subversion sandbox GitHub repository at https://svngithub.com/apache.org/repos/asf/struts/sandbox/trunk/struts2examples Image Removedstruts-examples. The example projects use Maven to manage the artifact dependencies and to build the .war files. |
...
Code Block |
---|
language | html |
---|
title | index.jsp |
---|
borderStyle | solid |
---|
|
<%@ 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>
|
...
Code Block |
---|
| xml |
---|
| xml |
---|
title | struts.xml |
---|
borderStyle | solid |
---|
|
...
<action name="hello" class="org.apache.struts.helloworld.action.HelloWorldAction" method="execute">
<result name="success">/HelloWorld.jsp</result>
</action>
...
|
...
Code Block |
---|
| html |
---|
| html |
---|
title | url tag with param |
---|
|
<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>
|
...
Code Block |
---|
| html |
---|
| html |
---|
title | Struts 2 Form |
---|
|
<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 |
---|
| html |
---|
| html |
---|
title | Struts Form Tags Converted To HTML |
---|
|
<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>
|
...
Code Block |
---|
| html |
---|
| html |
---|
title | Struts Property Tag |
---|
|
<s:property value="messageStore.message" />
|
...
Code Block |
---|
| java |
---|
| java |
---|
title | Add Static Field |
---|
|
private static int helloCount = 0;
public int getHelloCount() {
return helloCount;
}
public void setHelloCount(int helloCount) {
HelloWorldAction.helloCount = helloCount;
}
|
...
Code Block |
---|
| java |
---|
| java |
---|
title | Increase helloCount |
---|
|
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.
...
Code Block |
---|
| html |
---|
| html |
---|
title | Use Property Tag To Display helloCount Value |
---|
|
<p>I've said hello <s:property value="helloCount" /> times!</p>
|
...
Code Block |
---|
| java |
---|
| java |
---|
title | Add toString Method To Class MessageStore |
---|
|
public String toString() {
return message + " (from toString)";
}
|
...
Code Block |
---|
| html |
---|
| html |
---|
title | Using Property Tag to Call toString |
---|
|
<p><s:property value="messageStore" /></p>
|
...