Documente Academic
Documente Profesional
Documente Cultură
com/in/ravigpandey
And also, while working with Struts, always be careful for naming of
variables. Whatever name you will provide as the PROPERTY in a JSP page,
must be supported by a setter and getter with similar name in ActionForm.
Like, if the name of TextBox Property is TxtAddrs; then the setter and
getter would be setTxtAddrs and getTxtAddrs respectively.
Go to File->New->Project
Choose Java Web -> Web Application
Press next, then Select your web server and context path (by default application name appear as
context path, here I am using the same)
Press Next, from the list of Frameworks select strut. Here we are providing default Action
Servlet Name and Action URL Pattern (i.e. action, *.do). Check Add Struts TLDs.
Create a new JSP file, with name sample. Insert following codes, which includes some Struts
html tags.
<%@ page language="java" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<<a href="mailto:%@page">%@page</a> contentType="text/html"
pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"<a
href="http://www.w3.org/TR/html4/loose.dtd">http://www.w3.org/TR/html4/loose.
dtd</a>">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Sample Page</title>
</head>
<body>
<html:form action="Name">
<table width="200" border="0">
<tr>
<td>Name:</td>
<td><html:text property="name" /></td>
</tr>
Right-click the SimpleStruts project node and Choose New > Other.
Under Categories choose Struts, then under File Types choose Struts ActionForm Bean.
Click Next
Inside Source Packages, inside example package we can find created NameForm ActionBean.
Edit NameForm code to be like below.
package example;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
/**
*
* @author Ravi
*/
public class NameForm extends org.apache.struts.action.ActionForm {
private String name = null;
public String getName() {
return (name);
}
public void setName(String name) {
this.name = name;
}
public void reset(ActionMapping mapping, HttpServletRequest request) {
this.name = null;
}
}
Provide Class name as NameAction, Package as example and Action Path /name. Other option
can be left as default.
Right click on the NameForm action, and click add success forward and failure forward.
Now, replace the index page content with below code and run.
<<a href="mailto:%@page">%@page</a> contentType="text/html"%>
<<a href="mailto:%@page">%@page</a> pageEncoding="UTF-8"%>
<jsp:forward page="sample.jsp"/>
The running WebApp will display a form with a textbox to accept the name of a person. After
submitting the user would be redirected to displayname.jsp. But is the textbox is empty then
the user will be redirected to the first page always.
After developing this much, you can also try HttpServlets as a redirection location for your
webapp. That means despite ending the response at some JSP or HTML page, one can also
proceed ahead (if necessary) to redirect the control to some Servlet file. While doing this you
just need to add the URL of your servlet to the Forward section of struts-config.xml, just like
adding a url of JSP page.
I hope you have enjoyed this tutorial and Struts as well. Happy Coding