Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

...

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).

Code Block
xml
xml
titleAction Mappingxml
<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
java
java
titleMethod execute of HelloWorldActionjava
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
html
html
titleStruts 2 Form Tagshtml
<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
java
java
titleAdd userName to HelloWorldActionjava
	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
java
java
titleAdd userName value to messagejava
if (userName != null) {
			
	messageStore.setMessage( messageStore.getMessage() + " " + userName);
			
}

...