Steps to write Struts sample in NetBeans

Category: Education

Presentation Description

No description available.


Presentation Transcript

Steps to write Struts sample in NetBeans:

Steps to write Struts sample in NetBeans

Step 1:

Step 1 Choose File > New Project. Under Categories, select Web. Under Projects, select Web Application and click Next .

Step 2:

Step 2 In the Name and Location panel, enter StrutsExample for Project Name and click Next.

Step 3:

Step 3 In the Server and Settings panel, select the server to which you want to deploy your application. Only servers that are registered with the IDE are listed .

Step 4:

Step 4 In the Frameworks panel, select Struts: See next slide

Step 5:

Step 5 Click Finish.

Step 6: Create login page:

Step 6: Create login page Right-click the StrutsExample project node, choose New > JSP, and name the new file login. Click Finish. The login.jsp file opens in the Source Editor.

Step 7:Include this in login.jsp:

Step 7:Include this in login.jsp <%@page contentType ="text/html" pageEncoding ="UTF-8"%> <%@ taglib uri ="" prefix="bean" %> <%@ taglib uri ="" prefix="html" %> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset =UTF-8"> <title>JSP Page</title> </head> <body> <h1> < html:form action="/login"> <table border="0"> < tbody > < tr > <td colspan ="2"> < bean:write name=" LoginForm " property="error" filter="false"/> & nbsp ;</td> </ tr > < tr > <td>Enter your name:</td> <td>< html:text property="name" /></td> </ tr > < tr > <td>Enter your email:</td> <td>< html:text property="email" /></td> </ tr > < tr > <td></td> <td>< html:submit value="Login" /></td> </ tr > </ tbody > </table> </ html:form > </h1> </body> </html>

Step 8: Create success.jsp:

Step 8: Create success.jsp Right-click the StrutsExample project node, choose New > JSP, and name the new file success

Step 9:Include this in success.jsp:

Step 9:Include this in success.jsp <%@ taglib uri ="" prefix="bean" %> <head> <meta http-equiv="Content-Type" content="text/html; charset =UTF-8"> <title>Login Success</title> </head> <body> <h1>Congratulations!</h1> <p>You have successfully logged in.</p> <p>Your name is: < bean:write name=" LoginForm " property="name" />.</p> <p>Your email address is: < bean:write name=" LoginForm " property="email" />.</p> </body>

Step10:Create ActionForm bean:

Step10:Create ActionForm bean Right-click the StructsExample project node and choose New > Other. Under Categories choose Struts, then under File Types choose Struts ActionForm Bean. Click Next.

Step 11::

Step 11: Type in LoginForm for the Class Name. Then select com.myapp.struts in the Package drop-down list and click Finish.

Step 12::

Step 12: Add the following: 1. private String email; 2. private String error; 3. public String getEmail () { return email; }

Slide 15:

4. public void setEmail (String i ) { email = i ; } 5. public void setError () { this.error = "<span style=' color:red '>Please provide valid entries for both fields</span>"; } public String getError (){ return error; }

Step 13: Create Action class:

Step 13: Create Action class In the Projects window, right-click the StrutsExample project node and choose New > Other. From the Struts category choose Struts Action and click Next. In the Name and Location panel, change the name to LoginAction . Select com.myapp.struts in the Package drop-down list. Type /login in Action Path.

Slide 17:

In the third step of the wizard, you are given the opportunity to associate the Action class with a form bean. Make the following adjustments to the panel: 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

Step 14:

Step 14 Add the following in LoginAction private final static String FAILURE = "failure"; Add this in execute() method: LoginForm formBean = ( LoginForm )form; String name = formBean.getName (); String email = formBean.getEmail (); // perform validation if ((name == null) || // name parameter does not exist email == null || // email parameter does not exist name.equals ("") || // name parameter is empty email.indexOf ("@") == -1) { // email lacks '@' formBean.setError (); return mapping.findForward (FAILURE); }

Step 15:Adding forward Entries to struts-config.xml :

Step 15: Adding forward Entries to struts-config.xml Open struts-config.xml in the Source Editor, right-click anywhere in the action entry for LoginForm , and choose Struts > Add Forward. Enter as shown in the next two slides

Step 16:

Step 16 Right click on StrutsExample node Select Deploy Then open browser and type http://localhost:8080/StrutsExample/login.jsp

authorStream Live Help