Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

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
XMLxmlXML
xml
titleAction Mapping

<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
JAVAjavaJAVA
java
titleMethod execute of HelloWorldAction

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
HTMLhtmlHTML
html
titleStruts 2 Form Tags

<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
JAVAjavaJAVA
java
titleAdd userName to HelloWorldAction

	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
JAVAjavaJAVA
java
titleAdd userName value to message

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

...