Home Struts Struts Struts2.2.1 Struts2.2.1 hello world annotations Example using Properties

Ask Questions?

View Latest Questions


 
 

Struts2.2.1 hello world annotations Example using Properties
Posted on: February 1, 2011 at 12:00 AM
Advertisement
In this tutorial, We will discuss about hello world annotation application using properties.

Struts2.2.1  hello world annotations Example using Properties

 In this tutorial, We will discuss about hello world annotation application using properties. 

In this example we use the /result where we put the result file.The @Result annotation do the mapping of result with the result page. In this example the result "success" is mapped to the result "/results/success.jsp".

The following Example will shows how to implement the annotation in the Struts2.2.1 --

Directory structure of the Hello world annotation application using @Action and @Result-

First we create a JSP file named index.jsp as follows.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"

pageEncoding="ISO-8859-1"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

"http://www.w3.org/TR/html4/loose.dtd">

<%@taglib uri="/struts-tags" prefix="s"%>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Hello World</title>

</head>

<body>

<h1>Annotation Example using properties</h1>

<hr>

<s:form action="welcome">

<s:textfield name="username" label="Insert Name" />

<s:submit />

</s:form>

</body>

</html>

Here is the welcome.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"

pageEncoding="ISO-8859-1"%>

<!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>Welcome User</title>

</head>

<body>

<h1>${message}</h1>

</body>

</html>

Here is the  web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web
="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

id="WebApp_ID" version="2.5">

<display-name>HelloWorldusingProperties</display-name>

<filter>

   <filter-name>struts2</filter-name>

         <filter-class>

                     org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

         </filter-class>

        <init-param>

                    <param-name>struts.devMode</param-name>

                    <param-value>true</param-value>

       </init-param>

</filter>

<filter-mapping>

        <filter-name>struts2</filter-name>

       <url-pattern>/*</url-pattern>

</filter-mapping>

<welcome-file-list>

               <welcome-file>index.jsp</welcome-file>

</welcome-file-list>

</web-app>

The action class Welcome.java is as follows.

package roseindia.action;

import com.opensymphony.xwork2.ActionSupport;

import org.apache.struts2.convention.annotation.Action;

import org.apache.struts2.convention.annotation.Result;

public class Welcome extends ActionSupport {

private String username;

private String message;

@Action(value = "/welcome", results = {

@Result(name = "success", location = "/results/welcome.jsp") })

public String execute() throws Exception {

message = "Welcome Dear " + "'" + username + "'";

return SUCCESS;

}

public void setUsername(String username) {

this.username = username;

}

public void setMessage(String message) {

this.message = message;

}

public String getMessage() {

return message;

}

public String getUsername() {

return username;

}

}

The struts.properties file is as follows.

struts.convention.result.path=/results

This Program produces output on the basis of the Annotation evaluation, This  give the output as-

Output:-

 

Download Select Source Code

Related Tags for Struts2.2.1 hello world annotations Example using Properties:


DMCA.com