Struts2.2.1 hello world annotations Example
Posted on: February 1, 2011 at 12:00 AM
 In this tutorial, We will discuss about hello world application using annotation and how to apply annotations to java classes and methods.

Struts2.2.1  hello world annotations Example

 In this tutorial, We will discuss about hello world application using annotation and how to apply annotations to java classes and methods. In the Struts 2.2.1 examples we use a struts configuration file 'struts.xml' for configure and action mapping of the application with the Action.java class and jsp file. Now for action mapping and configuration of struts application struts2.2.1 provide a alternative to the struts.xml file as standard naming convention and annotations for action mapping and configuration.

In this example we enter the username and get a welcome message form the application as a output.

We can enable annotations and standard naming convention by including struts2-convention-plugin in WEB-INF/lib folder. In the struts2.2.1 we use the struts2-convention-plugin-2.2.1.jar file for enabling annotation in the application and use the <init-param> tag as given in the web.xml file.ADS_TO_REPLACE_1

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

Directory structure of the Hello world application using annotation-

ADS_TO_REPLACE_2

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

<%@ page language="java" contentType="text/html; charset=UTF-8"

pageEncoding="UTF-8"%>ADS_TO_REPLACE_3

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

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

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

<html>

<head>

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

<title>Annotation example</title>

</head>

<body>ADS_TO_REPLACE_6

<h1>Annotation Example</h1>

<hr>

<s:form action="welcome">ADS_TO_REPLACE_7

              <s:textfield name="username" label="UserName" />

              <s:submit />

</s:form>ADS_TO_REPLACE_8

</body>

</html>

Here is the welcome.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"ADS_TO_REPLACE_9

pageEncoding="UTF-8"%>

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

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

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

<html>

<head>ADS_TO_REPLACE_11

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

<title>Annotation example</title>

</head>ADS_TO_REPLACE_12

<body>

<h1>${message}</h1>

</body>ADS_TO_REPLACE_13

</html>

Here is the  web.xml

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

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

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">ADS_TO_REPLACE_15

<display-name>AnnotationExample</display-name>

<filter>

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

         <filter-class>

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

         </filter-class>ADS_TO_REPLACE_17

        <init-param>

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

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

       </init-param>

</filter>

<filter-mapping>ADS_TO_REPLACE_19

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

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

</filter-mapping>ADS_TO_REPLACE_20

<welcome-file-list>

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

</welcome-file-list>ADS_TO_REPLACE_21

</web-app>

The action class Welcome.java is as follows.

package roseindia.action;

import com.opensymphony.xwork2.ActionSupport;ADS_TO_REPLACE_22

public class Welcome extends ActionSupport {

private String username;

private String message;ADS_TO_REPLACE_23

public String execute() throws Exception {

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

return SUCCESS;ADS_TO_REPLACE_24

}

public void setUsername(String username) {

this.username = username;ADS_TO_REPLACE_25

}

public void setMessage(String message) {

this.message = message;ADS_TO_REPLACE_26

}

public String getMessage() {

return message;ADS_TO_REPLACE_27

}

public String getUsername() {

return username;ADS_TO_REPLACE_28

}

}

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

Output:-ADS_TO_REPLACE_29

 

Download Select Source CodeADS_TO_REPLACE_30

Related Tags for Struts2.2.1 hello world annotations Example:

Advertisements

Ads

Ads

 
Advertisement null

Ads