Hi all, as we all know that usually first application is HelloWorld. So lets make our first web application in Struts 2 .
So the requirements before creating the web application are given below
1. Eclipse
2. Tomcat
3. Struts library files.
Steps for creating web application in struts 2 using Eclipse is given below:
1. Create dynamic web project :-
Launch Eclipse, and select menu item File-> New-> Dynamic Web Project.
1.a. Enter Project Name MyFirstStrutsApp
and click Finish button.
2. Add Struts jar files in WEB-INF/lib folder.
we need some jar files for making the application which are given below:
commons-fileupload-2.x.x.jar
commons-io-2.x.x.jar
commons-lang-2.x.x.jar
freemarker-2.x.x.jar
ognl-2.x.x
struts2-core-2.x.x.jar
xwork-core-2.x.x.jar
3.Change web.xml
Add controller filter in web.xml file so it can handle all the request .
<?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>MyFirstStrutsApp</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</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>
4. Create index.jsp file under WEB-INF folder
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!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>
<s:set name="theme" value="'simple'" scope="page" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Hello World</title>
</head>
<body>
<s:form action="HelloWorld" >
<s:submit/>
</s:form>
</body>
</html>
So the requirements before creating the web application are given below
1. Eclipse
2. Tomcat
3. Struts library files.
Steps for creating web application in struts 2 using Eclipse is given below:
1. Create dynamic web project :-
Launch Eclipse, and select menu item File-> New-> Dynamic Web Project.
1.a. Enter Project Name MyFirstStrutsApp
and click Finish button.
2. Add Struts jar files in WEB-INF/lib folder.
we need some jar files for making the application which are given below:
commons-fileupload-2.x.x.jar
commons-io-2.x.x.jar
commons-lang-2.x.x.jar
freemarker-2.x.x.jar
ognl-2.x.x
struts2-core-2.x.x.jar
xwork-core-2.x.x.jar
3.Change web.xml
Add controller filter in web.xml file so it can handle all the request .
<?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>MyFirstStrutsApp</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</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>
4. Create index.jsp file under WEB-INF folder
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!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>
<s:set name="theme" value="'simple'" scope="page" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Hello World</title>
</head>
<body>
<s:form action="HelloWorld" >
<s:submit/>
</s:form>
</body>
</html>
5. Create success.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!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><s:property value="message" /></h1>
</body>
</html>
6.Create struts.xml in src folder
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="default" extends="struts-default">
<action name="HelloWorld" class="com.techArcher.HelloWorld" >
<result>JSP/success.jsp</result>
</action>
</package>
</struts>
7. Now create HelloWorld Action class in com.techArcher package
HelloWorld.java
package com.techArcher;
public class HelloWorld {
private String message;
public String execute()
{
message="Hello User";
return "success";
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
8.Now start the server
and enter
http://localhost:8080/MyFirstStrutsApp/HelloWorld.action
No comments:
Post a Comment