Simple Example on Struts with Netbeans 9

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->Project

Choose
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.

Beginning with Struts

Struts is most popular framework for java web application. It is an MVC framework that provides for flexible, extensible application development. Struts let you focus on the model, view, and controller elements important to the application’s use case. This leaves the infrastructure work, such as reading the configuration file that contains mapping between URLs and actions, to the framework.

Fig : Application Work Flow Model with Struts (from netbeans.com)

I began struts few days back. Here are some url, which help me to understand the beginning concept of it.

http://www.netbeans.com
http://www.developer.com

Still I am searching some more useful, if you know any URL or any other resource please post me as comment.