Sunteți pe pagina 1din 11

Ravi Gaurav Pandey | https://cn.linkedin.

com/in/ravigpandey

Simple Example on Struts with Netbeans


Hi, after a long time Im uploading any content on scribd due to lack of time
and other projetcs. This time I have given a very precise step-by-step tutorial
on struts. This is mainly targeted for a novice in the field. Here, first of all,
you have to go through the Architecture; so as to understand the process
flow of struts. Without knowing the basics of data and control flow in struts,
one can develop a good struts program but cant debug whenever required.
Here we go :

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

Ravi Gaurav Pandey | https://cn.linkedin.com/in/ravigpandey

Press Next, Provide Project name and Project Location

Press next, then Select your web server and context path (by default application name appear as
context path, here I am using the same)

Ravi Gaurav Pandey | https://cn.linkedin.com/in/ravigpandey

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.

Click on finish, project will contains following content.

Ravi Gaurav Pandey | https://cn.linkedin.com/in/ravigpandey

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>

Ravi Gaurav Pandey | https://cn.linkedin.com/in/ravigpandey


<tr>
<td><html:submit /></td>
</tr>
</table>
</html:form>
</body>
</html>

Right-click the SimpleStruts project node and Choose New > Other.
Under Categories choose Struts, then under File Types choose Struts ActionForm Bean.
Click Next

Provide a class name, NameForm.


Provide example as a package (It is recomended by netbeans that the java class should not be
placed in default package).

Ravi Gaurav Pandey | https://cn.linkedin.com/in/ravigpandey

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;
}
}

Create a new JSP file displayname.jsp, with following code.


<html>
<head>

Ravi Gaurav Pandey | https://cn.linkedin.com/in/ravigpandey


<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Sample Struts Display Name</title>
</head>
<body>
<H1>Hello <%= request.getAttribute("NAME") %> !!</H1>
</body>
</html>

In the Projects window, right-click the SimpleStruts project node.


Choose New > Other.
From the Struts category choose Struts Action and click Next.

Provide Class name as NameAction, Package as example and Action Path /name. Other option
can be left as default.

Ravi Gaurav Pandey | https://cn.linkedin.com/in/ravigpandey

Delete the forward slash for the Input Resource field


Set Scope to Request (Session is the default scope setting in Struts.)
Deselect the Validate ActionForm Bean option

Add folloing code inside the execute() method.


package example;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

Ravi Gaurav Pandey | https://cn.linkedin.com/in/ravigpandey


import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
/**
*
* @author Ravi
*/
public class NameAction extends org.apache.struts.action.Action {
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
String target = new String("success");
String name=null;
if ( form != null ) {
// Use the NameForm to get the request parameters
NameForm nameForm = (NameForm)form;
name = nameForm.getName();
}
// if no mane supplied Set the target to failure
if ( name == null || name.equals("")) {
target = new String("failure");
}
else {
request.setAttribute("NAME", name);
}
return (mapping.findForward(target));
}
}

Adding forward Entries to struts-config.xml


in struts-config.xml, go to action-mapping tag
<action-mappings>
<action name="NameForm" path="/name" scope="request"
type="example.NameAction" validate="false"/>
<action path="/Welcome" forward="/welcomeStruts.jsp"/>
</action-mappings>

Right click on the NameForm action, and click add success forward and failure forward.

Ravi Gaurav Pandey | https://cn.linkedin.com/in/ravigpandey

Ravi Gaurav Pandey | https://cn.linkedin.com/in/ravigpandey

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

S-ar putea să vă placă și