Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
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
titleAction Mapping
xml


<action name="hello" class="org.apache.struts.helloworld.action.HelloWorldAction" method="execute">
	<result name="success">/HelloWorld.jsp</result>
</action>

...

Code Block
java
titleMethod execute of HelloWorldAction
java


public String execute() throws Exception {
		
	messageStore = new MessageStore() ;
		
	helloCount++;
		
	return SUCCESS;

}

...

Code Block
html
titleStruts 2 Form Tags
html


<s:form action="hello">

	<s:textfield name="userName" label="Your name" />
	
	<s:submit value="Submit" />

</s:form>


...

Code Block
java
titleAdd userName to HelloWorldAction
java


	private String userName;

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

...

Code Block
java
titleAdd userName value to message
java


if (userName != null) {
			
	messageStore.setMessage( messageStore.getMessage() + " " + userName);
			
}

...