Simple Example on Struts with Netbeans 9
September 28, 2010 11 Comments
While starting struts, I had browsed lots for a simple example, which can demonstrate the basic concept of Struts with Netbeans IDE but i found vary less (those I had posted in my previous post). So after learning little bit about Struts, here I am posting a simple Struts example which may help you to get some basic concept of Struts.
Go to
File->New->ProjectChoose
Java Web -> Web Application
Press Next, Provide Project name and Project Location
Press next, then Select your webserver and context path (defaultly apllication nam 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 Servlate Name and Action URL Pattern (i.e. action, *.do). Check Add Struts TLDs.
Click on finish, project will contains following content.
Create a new JSP file, with name sample. Insert following codes, which includes some Struts html tags.
<%-- Document : sample Created on : Sep 29, 2010, 1:42:19 AM Author : Prashant --%> <%@ 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> <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).
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 Prashant */ 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.
In the Projects window, right-click the SimpleStruts project node.
Choose New > Other.
From the Struts category choose Struts Action and click Next.
</span> <%-- Document : displayname Created on : Sep 29, 2010, 2:03:56 AM Author : Prashant --%> <<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 Struts Display Name</title> </head> <body> <H1>Hello <%= request.getAttribute("NAME") %> !!</H1> </body> </html> <span style="font-family: Verdana; font-size: x-small;">
Provide Class name as NameAction, Package as example and Action Path /name. Other option can be left as default.
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; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; /** * * @author Prashant */ 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.
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"/>
Output
Download zip archive of this project.
I want 10th model paper.
Sorry, I don’t understand what you are asking for.
Code Not Working
Will you please give more examples for this struts application
not understanding give every detail how file is made and what the coding do
i can’t run this code..i got an error. please help me
Very nice!!
thanks prashant
When Form is Submited then Action form is called….right ??
but i dont understand these two lines in class NameAction
”
// Use the NameForm to get the request parameters
NameForm nameForm = (NameForm)form;
name = nameForm.getName(); ”
how NameForm class is used……????
Vijay Hirpara:
Thanks Prashant for helping in strut demo program , but It’s necessary to do some change in strut-config.xml to run this program successfully.
.
Thank you vary much..
this is very helpfull for struts beginner..
thanks a lot.