<--Previous Next-->
In this application we will take the input from the textbox
and we will print it in another page using the struts2 framework. To create this example we need the following
things:
1. Views (jsps) which will take the input and which
will print the message. In our application we have two jsps. Index.jsp is used to take the input and
success.jsp is used to print the processed message.
2. Action which will take the message from the
index.jsp and process it.
3. Struts.xml which is having the details of
mapping for the actions and the views.
Now create the dynamic web project (First
Example) in the eclipse. Add the required jars in to the lib folder.
Configure filter dispatcher in the
following way in web.xml. We can find
the web.xml in the WEB-INF folder of the application (First Example) we
created.
Folder
structure:
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"
>< 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
><welcome-file-list
><welcome-file
>index.jsp
</welcome-file></welcome-file-list
></web-app
>Note: The FilterDispatcher (org.apache.struts2.dispatcher.FilterDispatcher) is used in the early Struts development, and it’s deprecated since Struts 2.1.3. If you are using Struts version >= 2.1.3, it’s always recommend to declared the new filter class as StrutsPrepareAndExecuteFilter
Index.jsp:
In the index.jsp we will have one text
box from which we will give the input to the application.
<%@ 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>Basic
Struts 2 Application - Welcome</title>
</head >
<body >
<h1>Welcome
To Struts 2!</h1>
<form action="hello" method="post" >
<br/ >
Hello: <input type="text" name="sayHello"
value=""/>
<br/ >
<input type="submit" name="submit"
value="say hello"/ >
</form >
</body >
</html >
Action
class:
package com.javaguide4beginners; public class HelloExample { private String sayHello; public String getSayHello() { return sayHello; } public void setSayHello(String sayHello) { this.sayHello = sayHello; } public String execute(){ return "success"; } }Success.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>Insert title here</title>
</head>
<body>
Hello ${request.sayHello}
</body>
</html>
Struts.xml:
<?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>
<include file="struts-default.xml"/ >
<package name="javaguide"
extends="struts-default" >
<action name="hello"
class="com.javaguide4beginners.HelloExample" >
<result name="success">/success.jsp</result>
</action>
</package>
</struts>