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/struts2examplescom/apache/struts-examples. |
Introduction
Coding a Struts 2 Action involves several parts:
...
Code Block |
---|
| xml |
---|
title | Action Mapping |
---|
| xml |
---|
|
<action name="hello" class="org.apache.struts.helloworld.action.HelloWorldAction" method="execute">
<result name="success">/HelloWorld.jsp</result>
</action>
|
...
Code Block |
---|
| java |
---|
title | Method execute of HelloWorldAction |
---|
| java |
---|
|
public String execute() throws Exception {
messageStore = new MessageStore() ;
helloCount++;
return SUCCESS;
}
|
...
Code Block |
---|
| html |
---|
title | Struts 2 Form Tags |
---|
| html |
---|
|
<s:form action="hello">
<s:textfield name="userName" label="Your name" />
<s:submit value="Submit" />
</s:form>
|
...
Code Block |
---|
| java |
---|
title | Add userName to HelloWorldAction |
---|
| java |
---|
|
private String userName;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
|
...
Code Block |
---|
| java |
---|
title | Add userName value to message |
---|
| java |
---|
|
if (userName != null) {
messageStore.setMessage( messageStore.getMessage() + " " + userName);
}
|
...