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 class

package contact.model;

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) { = id;

    public String getLastName() {
        return lastName;

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

    public String getWebsite() {
        return website;

    public void setWebsite(String 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 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) { = 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"%>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
        <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="" label="Homepage"/>
    <s:textfield name="contact.birthDate" label="Birthdate"/>
    <s:submit value="Add Contact" align="center" />

<table border="2">
    <th>Cell No.</th>

        <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=""/>">link</a></td>
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" ?>
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

    <!-- 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>
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="" xmlns:xsi="" xsi:schemaLocation="">
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


  1. Hi u have done great job.
    Keep updating

  2. Your blog is very useful to me.

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

  4. 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... :)

    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");

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


    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

    1. This comment has been removed by the author.