Struts 2 configuration files

For developing "Hello World" application
you need the following files:
Add the following code snippet into the index.html
file.
index.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>RoseIndia.Net Struts 2 Hello World Application</title>
</head>
<body>
<div align="center">
<center>
<table border="0" cellpadding="0" cellspacing="0" width="400">
<tr>
<td><font color="#000080" size="5">
<b>RoseIndia.net Struts 2 Hello World Application</b><br>
</font></td>
</tr>
<tr>
<td>
<ul>
<li><a href="roseindia/HelloWorld.action">Run Struts 2 Hello World Application</a></li>
</ul>
</td>
</tr>
</table>
</center>
</div>
<p align="center"> </p>
</body>
</html>
|
Developing Controller Configuration File:
Struts 2 uses the struts.xml file for
configuring the application. Create struts.xml file and save it in the
"struts2helloworld\WEB-INF\src" directory with the following
content.
struts.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>
<!-- Rose India Struts 2 Tutorials -->
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" />
<package name="roseindia" namespace="/roseindia" extends="struts-default">
<action name="HelloWorld" class="net.roseindia.Struts2HelloWorld">
<result>/pages/HelloWorld.jsp</result>
</action>
</package>
</struts>
|
The struts.xml file should be present in the
class path of the application, you can either include it in the jar and place in
the lib directory of the application or place it in the classes directory of the
web application. In our application we are using ant build tool which is
including it in the jar file.
Developing Action (to interact with Model):
Now create Struts2HelloWorld.java and saves it
to the "struts2helloworld\WEB-INF\src\java\net\roseindia"
directory. This action class creates the message to be displayed on the screen.
Here is the code of Struts2HelloWorld.java:
package net.roseindia;
import com.opensymphony.xwork2.ActionSupport;
import java.util.Date;
public class Struts2HelloWorld extends ActionSupport {
public static final String MESSAGE = "Struts 2 Hello World Application!";
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;
}
}
|
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
struts2helloworld\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>Welcome to Roseindia Hello World Application</h2>
<b><s:property value="message" /></b>
</body>
</html>
|
The line <%@ taglib prefix="s" uri="/struts-tags"
%> declares data tag library of struts. The struts data tag is used to
display the dynamic data. The tag <s:property
value="message" /> calls the
methods getMessage() respectively of the Struts2HelloWorld action
class and merges the values with response.
Download "Hello
World" Application
|