Frameworks| Hibernate| Struts| JSF| JavaFX| Ajax| Spring| DOJO| JDO| iBatis| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Struts 2 configuration files 
 

For developing "Hello World" application you need the following files: Add the following code snippet into the index.html file.

 

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 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 
Hello World Application</b><br>
        &nbsp;&nbsp;&nbsp;</font></td>
    </tr>
    
    <tr>
      <td>
        <ul>
          <li><a href="roseindia/HelloWorld.action">Run Struts Hello World Application</a></li>
        </ul>
       
  </td>
    </tr>
    
  </table>
  </center>
</div>
<p align="center">&nbsp;</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 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 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

                           

» View all related tutorials
Related Tags: c mvc com framework diff struts io components vi component this frame if work learn ram ear e il section

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.