Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Class name typo, started on formatting changes

...

  1. Create a class to store the welcome message (the model)
  2. Create a server page to present the message (the view)
  3. Create an Action class to control the interaction between the user, the model, and the view (the controller)
  4. Create a mapping (struts.xml) to couple the Action class and view

    Tip

    By creating these components, we are separating the work flow into three well-known concerns: the View, the Model, and the Controller. Separating concerns makes it easier to manage applications as they become more complex.

    Let's look at an example model class, Action, server page, and mapping. If you like, fire up your Java IDE, and enter the code as we go.

    Info

    This tutorial assumes you've completed the How To Create A Struts 2 Web Application tutorial and have a working basic Struts project. The example code for this tutorial, helloworld, is available for checkout from the
    Struts 2 subversion sandbox at https://svn.apache.org/repos/asf/struts/sandbox/trunk/struts2examples. The example projects use Maven
    to manage the artifact dependencies and to build the .war files.

...

Code Block
java
titleMessageStore.java
java

package org.apache.struts.helloworld.model;

public class MessageStore {
	
	private String message;
	
	public MessageStore() {
		
		setMessage("Hello Struts User");
	}

	public String getMessage() {

		return message;
	}

	public void setMessage(String message) {

		this.message = message;
	}

}

...

Note the package and import statements below.

java
Code Block
formatjava
titleHelloWorldHelloWorldAction.java
borderStylesolidjava

package org.apache.struts.helloworld.action;

import org.apache.struts.helloworld.model.MessageStore;
import com.opensymphony.xwork2.ActionSupport;

public class HelloWorldAction extends ActionSupport {

	private static final long serialVersionUID = 1L;

	private MessageStore messageStore;
	
	public String execute() throws Exception {
		
		messageStore = new MessageStore() ;
		return SUCCESS;
	}

	public MessageStore getMessageStore() {
		return messageStore;
	}

	public void setMessageStore(MessageStore messageStore) {
		this.messageStore = messageStore;
	}

}

The Struts 2 framework will create an object of the HelloWorldAction class  class and call the execute method in response to a user's action (clicking on a hyperlink that sends a specific URL to the Servlet container).

In this example, the execute method creates an object of class MessageStore and  and then returns the String constant SUCCESS.

Note also the public set getter and get setter methods for the private MessageStore object object. Since we want to make the MessageStore object  object available to the view page (, HelloWorld.jsp) we  we need to follow the JavaBean-style of providing get and set methods.

...

We need a server page to present the message that is stored in the model class MessageStore. Create the below jsp JSP in the WebContent folder  folder (for the if using Ant project) and or in src/main/webapp for the Mvn project (if using Maven).

Code Block
html
titleHelloWorld.jsp
borderStylesolid
html

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Hello World!</title>
  </head>

  <body>
    <h2><s:property value="messageStore.message" /></h2>
  </body>
</html>

The taglib directive tells the Servlet container that this page will be using the Struts 2 tags and that these tags will be preceded by an s.

The s<s:property tag property> tag displays the value returned by calling the method getMessageStore of  of the HelloWorldAction controller  controller class. That method returns a MessageStore object object. By adding the .message onto  onto the messageStore part of the value attribute we are telling the Struts 2 framework to then call the getMessage method  method of that MessageStore object object. The getMessage method  method of class MessageStore returns  returns a String. It is that String that will be displayed by the s<s:property tagproperty> tag.

We'll learn more about tags in the next tutorial. See the Struts Tags for more information about tags.

...

We need a mapping to tie the URL, the HelloWorldAction class  class (controller), and
the HelloWorld.jsp (the view) together. The mapping tells the Struts 2 framework which class will respond to the user's action (the URL), which method of that class will be executed, and what view to render based on the String result that method returns.

...

Code Block
xml
formatxml
titlestruts.xml
borderStylesolid
xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

  <constant name="struts.devMode" value="true" />

  <package name="basicstruts2" extends="struts-default">

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

  </package>

</struts>

Step 5 - Create The URL Action

...

Code Block
html
titleindex.jsp
html

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Basic Struts 2 Application - Welcome</title>
</head>
<body>
<h1>Welcome To Struts 2!</h1>
<p><a href="<s:url action='hello'/>">Hello World</a></p>
</body>
</html>

...