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 --ADS_TO_REPLACE_1
Directory structure of the Hello world annotation application using @Action and @Result-
First we create a JSP file named index.jsp as follows.ADS_TO_REPLACE_2
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"ADS_TO_REPLACE_3 "http://www.w3.org/TR/html4/loose.dtd"> <%@taglib uri="/struts-tags" prefix="s"%> <html>ADS_TO_REPLACE_4 <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Hello World</title>ADS_TO_REPLACE_5 </head> <body> <h1>Annotation Example using properties</h1>ADS_TO_REPLACE_6 <hr> <s:form action="welcome"> <s:textfield name="username" label="Insert Name" />ADS_TO_REPLACE_7 <s:submit /> </s:form> </body>ADS_TO_REPLACE_8 </html> |
Here is the welcome.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>ADS_TO_REPLACE_9 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html>ADS_TO_REPLACE_10 <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Welcome User</title>ADS_TO_REPLACE_11 </head> <body> <h1>${message}</h1>ADS_TO_REPLACE_12 </body> </html> |
Here is the web.xml
<?xml version="1.0" encoding="UTF-8"?>ADS_TO_REPLACE_13 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
id="WebApp_ID" version="2.5"> <display-name>HelloWorldusingProperties</display-name> <filter>ADS_TO_REPLACE_15 <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilterADS_TO_REPLACE_16 </filter-class> <init-param> <param-name>struts.devMode</param-name>ADS_TO_REPLACE_17 <param-value>true</param-value> </init-param> </filter>ADS_TO_REPLACE_18 <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern>ADS_TO_REPLACE_19 </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file>ADS_TO_REPLACE_20 </welcome-file-list> </web-app> |
The action class Welcome.java is as follows.
package roseindia.action;ADS_TO_REPLACE_21 import com.opensymphony.xwork2.ActionSupport; import org.apache.struts2.convention.annotation.Action; import org.apache.struts2.convention.annotation.Result;ADS_TO_REPLACE_22 public class Welcome extends ActionSupport { private String username; private String message;ADS_TO_REPLACE_23 @Action(value = "/welcome", results = { @Result(name = "success", location = "/results/welcome.jsp") }) public String execute() throws Exception {ADS_TO_REPLACE_24 message = "Welcome Dear " + "'" + username + "'"; return SUCCESS; }ADS_TO_REPLACE_25 public void setUsername(String username) { this.username = username; }ADS_TO_REPLACE_26 public void setMessage(String message) { this.message = message; }ADS_TO_REPLACE_27 public String getMessage() { return message; }ADS_TO_REPLACE_28 public String getUsername() { return username; }ADS_TO_REPLACE_29 } |
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:-ADS_TO_REPLACE_30
Download Select Source CodeADS_TO_REPLACE_31
Advertisements
Ads
Ads