Monday, 18 June 2012

Struts 2 Example Step by Step in net beans

Open NetBeans and goto File -> New Project 
click on next button Give project Name ContactDetails 
 click on next button select server
select  Struts2 mvc and click on finish

the Directory structure of project       

Right click on source package ->new->java package give package name contact.model click on finish button


 add the contact class in contact.model package Right click on contact.model package goto new->java class give class name Contact click on finish button
Contact.java class



package contact.model;


import java.io.Serializable;
import java.sql.Date;


/**
 *
 * @author sandy
 */
public class Contact implements Serializable{
    private int id;
    private String firstName;
    private String lastName;
    private String emailId;
    private String cellNo;
    private Date birthDate;
    private String website;
    private Date created;


    public Date getBirthDate() {
        return birthDate;
    }


    public void setBirthDate(Date birthDate) {
        this.birthDate = birthDate;
    }


    public String getCellNo() {
        return cellNo;
    }


    public void setCellNo(String cellNo) {
        this.cellNo = cellNo;
    }


    public Date getCreated() {
        return created;
    }


    public void setCreated(Date created) {
        this.created = created;
    }


    public String getEmailId() {
        return emailId;
    }


    public void setEmailId(String emailId) {
        this.emailId = emailId;
    }


    public String getFirstName() {
        return firstName;
    }


    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }


    public int getId() {
        return id;
    }


    public void setId(int id) {
        this.id = id;
    }


    public String getLastName() {
        return lastName;
    }


    public void setLastName(String lastName) {
        this.lastName = lastName;
    }


    public String getWebsite() {
        return website;
    }


    public void setWebsite(String website) {
        this.website = website;
    }
}
add new package in source package right click on source package new->java package give package name contact.action click on finish button
add action class right click on contact.action package new->java class give classname ContactAction click on finish button

ContactAction.java class
package contact.action;


import com.opensymphony.xwork2.ActionSupport;
import contact.model.Contact;


/**
 *
 * @author sandy
 */
public class ContactAction extends ActionSupport{
  private String msg;
private Contact contact;


    public Contact getContact() {
        return contact;
    }


    public void setContact(Contact contact) {
        this.contact = contact;
    }
   
  public String addContact()
  {
  
      return SUCCESS;
  
  
  }
}
add contact.jsp page right click on web pages folder new-> jsp give file name contact.jsp click on finish button
contact.jsp file

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE html>
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Contact Information!</h1>
        <s:form action="add" method="post">
            <s:textfield name="contact.firstName" label="First Name"/>
    <s:textfield name="contact.lastName" label="Last Name"/>
    <s:textfield name="contact.emailId" label="Email"/>
    <s:textfield name="contact.cellNo" label="Cell No."/>
    <s:textfield name="contact.website" label="Homepage"/>
    <s:textfield name="contact.birthDate" label="Birthdate"/>
    <s:submit value="Add Contact" align="center" />

        </s:form>
        <h2>Contacts</h2>
<table border="2">
<tr>
    <th>Name</th>
    <th>Email</th>
    <th>Cell No.</th>
    <th>Birthdate</th>
    <th>Homepage</th>
    <th>Delete</th>
</tr>

<tr>
        <td><s:property value="contact.firstName"/> <s:property value="contact.lastName"/> </td>
        <td><s:property value="contact.emailId"/></td>
        <td><s:property value="contact.cellNo"/></td>
        <td><s:property value="contact.birthDate"/></td>
        <td><a href="<s:property value="contact.website"/>">link</a></td>
       
    </tr>
    
</table>
    </body>
</html>
double click on source packge  extracct default package inside default package struts.xml file open struts.xml file and change the code to given below
struts.xml file
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
   
    <!-- Configuration for the default package. -->
    <package name="default" extends="struts-default">
          <action name="add" class="contact.action.ContactAction" method="addContact">
        <result name="success">/contact.jsp</result>
          </action>
    </package>
</struts>
change the code of web.xml file inside the WEB-INF folder 
web.xml file 

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>/contact.jsp</welcome-file>
    </welcome-file-list>
</web-app>
Deploy the project on server and open web browser type the url http://localhost:8084/ContactDetails 
 input the data into textbox and click on add contact buttion




8 comments:

  1. Hi u have done great job.
    Keep updating

    ReplyDelete
  2. Your blog is very useful to me.

    ReplyDelete
  3. this is nice tutorial update it

    ReplyDelete
  4. Hi, Can you provide a source on how to create Rest Web service using struts 2

    ReplyDelete
  5. Hey how can i put validation on the text box???
    Still i didn't get the concept of struts but ur code is working very fine...
    Plz help me with the validation... asap... Actually it's a homework... :)

    ReplyDelete
    Replies
    1. Hi Abhinav

      add the below method in your action class for validation text field validate all the text field ony by one

      public void validate()
      {

      if(contact.getFirstName() .length()<=0)
      {
      addFieldError("contact.firstName", "Name is not null or empty");
      }
      if (contact.getEmailId() .length()<=0)
      {
      addFieldError("contact.emailId","email id is not null or empty");
      }
      }

      Delete
  6. sir help me whenever I click on Add Contact n jsp page it looks for the url :

    http://localhost:8084/StrutsDemo/add.action

    and displays the error as given below :

    HTTP Status 404 - No result defined for action contact.action.ContactAction and result input
    type Status report

    message No result defined for action contact.action.ContactAction and result input

    description The requested resource is not available.

    HELP me ASAP thanks in advance

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete