Simple Example on Struts | Working with Struts : Step-by-step for a novice in Netbeans
-
Upload
ravi-gaurav-pandey -
Category
Documents
-
view
13 -
download
0
description
Transcript of Simple Example on Struts | Working with Struts : Step-by-step for a novice in Netbeans
-
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.
Sample Page
Name:
-
Ravi Gaurav Pandey | https://cn.linkedin.com/in/ravigpandey
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.
-
Ravi Gaurav Pandey | https://cn.linkedin.com/in/ravigpandey
Sample Struts Display Name
Hello !!
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
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.
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