Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 4.0
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 repository: https://svn.apache.org/repos/asf/struts/sandbox/trunk/struts2examplesImage Removed.

Introduction

...

In the previous tutorials we covered how to configure Struts to map a URL such as hello.action to a Action class such as HelloWorldAction (specifically the execute method).

xml
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>

...

In method execute is where we placed what we want this controller to do in response to the hello.action.

Code Block
javajava
titleMethod execute of HelloWorldAction
java

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

}

...

In the Using Struts 2 Tags example application we added a Struts 2 form to index.jsp.

Code Block
htmlhtml
titleStruts 2 Form Tags
html

<s:form action="hello">

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

</s:form>


...

For the example application associated with this tutorial add the following Java code to class HelloWorldAction.

Code Block
javajava
titleAdd userName to HelloWorldAction
java

	private String userName;

	public String getUserName() {
		return userName;
	}

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

To personalize the MessageStore message (recall that class MessageStore is storing the message to display) add the following Java code to the HelloWorldAction's execute method after the statement that instantiates the MessageStore object.

Code Block
javajava
titleAdd userName value to message
java

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

...