...
- Create a class to store the welcome message (the model)
- Create a server page to present the message (the view)
- Create an Action class to control the interaction between the user, the model, and the view (the controller)
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 GitHub repository at https://svngithub.com/apache.org/repos/asf/struts/sandbox/trunk/struts2examplesstruts-examples. The example projects use Maven
to manage the artifact dependencies and to build the .war files.
...
Info |
---|
Note that in the code shown below the JavaDoc comments are omitted. In the download example, JavaDoc comments are included. |
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
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; } } |
...
We need a server page to present the message that is stored in the model class MessageStore
. Create the below JSP in the WebContent
folder (if using Ant) or in src/main/webapp
(if using Maven).
Code Block | |||||||||
---|---|---|---|---|---|---|---|---|---|
| |||||||||
<%@ 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> |
...
Edit the struts.xml
file (in the Mvn project that file is in the src/main/resources folder) to add the action mapping. Place the action node (action name="hello") between the opening and closing package node, just after the action mapping with the name="index". Your complete struts.xml should look like:
Code Block | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
| |||||||||||
<?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> |
...
First add the taglib directive at the top of the jsp <%@ taglib prefix="s" uri="/struts-tags" %>
. Next add this p tag <p><a href="<s:url action='hello'/>">Hello World</a></p>
after the h1 tag. Your new index.jsp should look like:
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
<%@ 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> |
...