Thursday 28 June 2012

struts2 with spring 3 integration in netbeans

Add following jar for spring integration.

Create applicationContext.xml file in WEB-INF  folder copy the following  code.


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">


      <bean id="UserClass" class="contact.action.User" scope="prototype"  >
      <property name="firstName" value="sandy" />
      <property name="lastName" value="barasker" />
   </bean>


</beans>
create new package contact.action  inside source package .
create the new java class User.java  inside contact.action package
and copy the following code.
User.java


package contact.action;

import com.opensymphony.xwork2.ActionSupport;

/**
 *
 * @author sandy
 */
public class User extends ActionSupport{
    
    private String firstName;
   private String lastName;
    public String execute()
   {
      return "success";
   }

   public String getFirstName() {
      return firstName;
   }

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

   public String getLastName() {
      return lastName;
   }

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


}
create the jsp page inside web folder user.jsp
copy the code .
user.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<%@taglib  uri="/struts-tags" prefix="s" %>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>user  jsp page!</h1>
        <s:form>
      <s:textfield name="firstName" label="First Name"/><br/>
      <s:textfield name="lastName" label="Last Name"/><br/>
   </s:form>
    </body>
</html>
Declared all the relationship here.
struts.xml


<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

 <constant name="struts.devMode" value="true" />
 <package name="default" extends="struts-default" >
         <action name="user" class="UserClass">
         <result name="success">/user.jsp</result>
      </action>
</package>

  </struts>


web.xml

<?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.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
     <listener>
      <listener-class>
         org.springframework.web.context.ContextLoaderListener
      </listener-class>
   </listener>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>/contact.jsp</welcome-file>
    </welcome-file-list>
</web-app>
deploye the project on web server
type the url: http://localhost:8084/ContactDetails/user
your output like 



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