Magazine
 
Struts2
Writing JSP, Java and Configuration for Hello World Application

Now, we will write JSP, Java and required configuration files for our Struts 2 Hello World application. In struts 2, struts.xml is used to configure the applications. Our application is very simple application that displays Hello World
message along with current date and time of the server. When the user requests for the resource then this request is sent to the struts framework. Then struts framework sends the input to the action class (in our case Struts2HelloWorld.java) and selects the resource “/pages/HelloWorld.jsp” to render as response to the user. In this example we have to develop three parts view, Action class and mapping (struts.xml) to couple action and page.

Developing View:

This page is used to display the result on the browser. The HelloWorld.jsp is view part of our application. Create “HelloWorld.jsp” in the struts2example\pages directory and add the following content:

<%@ taglib prefix=”s” uri=”/struts-tags” %>
<html>
<head>
<title>Struts 2 Hello World Application!
</title>
</head>
<body>
<h2><s:property value=”message” /></h2>
<p>Current date and time is:
<b><s:property value=”currentTime” /></b>
</body>
</html>

The line <%@ taglib prefix=”s” uri=”/strutstags” %> declares data tag library of the struts. The struts data tag is used to display the dynamic data. The tag <s:property value=”message” /> and <s:property value=”currentTime” /> calls the methods getMessage() and getCurrentTime() respectively of the Struts2HelloWorld action class and merges the values with response.Developing

 

Action (to interact with Model):

Now create Struts2HelloWorld.java and saves it to the “ struts2example\WEBINF\ src\java\net\javajazzup” directory. This action class creates the message to be displayed on the screen. Here is the code of Struts2HelloWorld.java:

Struts2HelloWorld.java

package net.javajazzup;
import
com.opensymphony.xwork2.ActionSupport;
import java.util.Date;
public class Struts2HelloWorld extends
ActionSupport {
public static final String MESSAGE = “Struts 2
Hello World”;
public String execute() throws Exception {
setMessage(MESSAGE);
return SUCCESS;
}
private String message;
public void setMessage(String message){
this.message = message;
}
public String getMessage() {
return message;
}
public String getCurrentTime(){
return new Date().toString();
}
}

Developing Controller Configuration File:

Struts 2 uses the struts.xml file for configuring the application. Create struts.xml file and save it in the “struts2example\WEBINF\src” directory with the following content.


Nov 2007 | Java Jazz Up | 34
 
previous
index
next
 
View All Topics
All Pages of this Issue
Pages: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,

30
, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53 , 54, 55, 56, 57,

58
, 59, 60, 61, 62, 63 , 64, 65 , 66 , 67 , 68 , 69   Download PDF