1) Download the Struts 2 framework jars and other dependent jars from the link here and copy them into the WebContent/WEB-INF/lib folder of the web application (in Eclipse).
2) Copy the following configuration for the Struts2 FilterDispatcher (which acts as the Controller) in the web.xml file of the Web Application
3) Create the HelloWorld.java class which is a POJO (and acts as the action class in Struts 2)
4) Create the NameCollector.jsp in the WebContent folder
5) Create the Hello.jsp in the WebContent folder
6) Create the Struts2 Configuration file, struts.xml in the src folder of the Web Application (in the default package)
7) Test the code by typing the following URL in the browser:
2) Copy the following configuration for the Struts2 FilterDispatcher (which acts as the Controller) in the web.xml file of the Web Application
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
3) Create the HelloWorld.java class which is a POJO (and acts as the action class in Struts 2)
package info.icontraining.struts2;
public class HelloWorld {
private static final String GREETING = "Hello ";
public String execute() {
setCustomGreeting( GREETING + getName() );
return "success";
}
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
private String customGreeting;
public String getCustomGreeting() {
return customGreeting;
}
public void setCustomGreeting( String customGreeting ) {
this.customGreeting = customGreeting;
}
}
4) Create the NameCollector.jsp in the WebContent folder
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Name Collector</title>
</head>
<body>
<h4>Enter your name!</h4>
<s:form action="HelloWorld">
<s:textfield name="name" label="Your name"/>
<s:submit/>
</s:form>
</body>
</html>
5) Create the Hello.jsp in the WebContent folder
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>HelloWorld</title>
</head>
<body>
<h1><s:property value="customGreeting"/></h1>
</body>
</html>
6) Create the Struts2 Configuration file, struts.xml in the src folder of the Web Application (in the default package)
<?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>
<constant name="struts.devMode" value="true" />
<package name="myPackage" extends="struts-default">
<action name="Name">
<result>/NameCollector.jsp</result>
</action>
<action name="HelloWorld" class="info.icontraining.struts2.HelloWorld">
<result name="success">/Hello.jsp</result>
</action>
</package>
</struts>
7) Test the code by typing the following URL in the browser:
http://localhost:8080/WebAppName/Name.action
No comments:
Post a Comment