Info |
---|
This tutorial assumes you've completed the Using Struts 2 Tags tutorial and have a working using_tags project. The example code for this tutorial, coding_action, is available for checkout from the Struts 2 sandbox subversion GitHub repository: https://svngithub.apache.org/repos/asf/struts/sandbox/trunk/struts2examples Image Removedcom/apache/struts-examples. |
Introduction
Coding a Struts 2 Action involves several parts:
...
Code Block |
---|
| xml |
---|
| xml |
---|
title | Action Mapping |
---|
|
<action name="hello" class="org.apache.struts.helloworld.action.HelloWorldAction" method="execute">
<result name="success">/HelloWorld.jsp</result>
</action>
|
...
Code Block |
---|
| java |
---|
| java |
---|
title | Method execute of HelloWorldAction |
---|
|
public String execute() throws Exception {
messageStore = new MessageStore() ;
helloCount++;
return SUCCESS;
}
|
...
Code Block |
---|
| html |
---|
| html |
---|
title | Struts 2 Form Tags |
---|
|
<s:form action="hello">
<s:textfield name="userName" label="Your name" />
<s:submit value="Submit" />
</s:form>
|
...
Code Block |
---|
| java |
---|
| java |
---|
title | Add userName to HelloWorldAction |
---|
|
private String userName;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
|
...
Code Block |
---|
| java |
---|
| java |
---|
title | Add userName value to message |
---|
|
if (userName != null) {
messageStore.setMessage( messageStore.getMessage() + " " + userName);
}
|
...