I want to call action class While Application loads First Time in Struts2

shafsi's Avatar, Join Date: May 2012
Newbie Member
Hi,

I have an application which Displays the Contact information when i submit through provided text box. But i want that when application loads first time it should display the data from database with submitting the data from JSP. at first time it does'nt shows data but when i press submitt it shows current with all data present in database table.

My JSP is as below

Code:
<html>
<head>
    <title>Contact Manager - Struts2 Hibernate Example</title>
</head>
<body>

<h1>Contact Manager</h1>
<s:actionerror/>
<s:form action="add" method="post">
    <s:textfield name="contact.firstName" label="Firstname"/>
    <s:textfield name="contact.lastName" label="Lastname"/>
    <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>

<tr>
    <th>Name</th>
    <th>Email</th>
    <th>Cell No.</th>
    <th>Birthdate</th>
    <th>Homepage</th>
    <th>Delete</th>
</tr>



</table>
</body>
</html>
web.xml

Code:
  <display-name>ContactManager</display-name>
      <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
      <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>
  
</web-app>
Action Class

Code:
package net.shafquat.contact.view;
 
import java.util.List;

import net.shafquat.contact.controller.ContactManagerController;
import net.shafquat.contact.model.Contact;
 
import com.opensymphony.xwork2.ActionSupport;

 
public class ContactAction extends ActionSupport {
 
    private static final long serialVersionUID = 9149826260758390091L;
    private Contact contact;
    private List<Contact> contactList;
    private Long id;
 
    private ContactManagerController contactManagerController;
 
    public ContactAction() {
        System.out.println("ContactAction called before execute ");
        contactManagerController = new ContactManagerController();
        System.out.println("ContactAction called before execute1 ");
    }
    public String execute() {
        System.out.println("entering into execute of ContactAction");
        this.contactList = contactManagerController.list();
        System.out.println("entering into execute of ContactAction1"+SUCCESS);
        return SUCCESS;
    }
 
    public String view() {
        try {
            System.out.println("add called after execute()");
            contactManagerController.view(getContact());
            System.out.println("add called after execute()1");
        } catch (Exception e) {
            e.printStackTrace();
        }
        this.contactList = contactManagerController.list();
        System.out.println("add called after execute()2"+SUCCESS);
        return SUCCESS;
    }
    
    public String add() {
        try {
            System.out.println("add called after execute()");
            contactManagerController.add(getContact());
            System.out.println("add called after execute()1");
        } catch (Exception e) {
            e.printStackTrace();
        }
        this.contactList = contactManagerController.list();
        System.out.println("add called after execute()2"+SUCCESS);
        return SUCCESS;
    }
    public String delete() {
        System.out.println("delete called after execute()");
        contactManagerController.delete(getId());
        System.out.println("delete called after execute()1"+SUCCESS);
        return SUCCESS;
    }
 
    public Contact getContact() {
        System.out.println("getContact called after execute()======"+contact);
        return contact;
    }
 
    public List<Contact> getContactList() {
        System.out.println("getContactList called after execute()======"+contactList);
         
        return contactList;
    }
 
    public void setContact(Contact contact) {
        System.out.println("setContact called after execute()======"+contact);
        this.contact = contact;
        System.out.println("setContact called after execute()1======"+contact);
    }
 
    public void setContactList(List<Contact> contactsList) {
        System.out.println("setContactList called after execute()======"+contactsList);
        this.contactList = contactsList;
        System.out.println("setContactList called after execute()1======"+contactsList);
    }
 
    public Long getId() {
        System.out.println("getId called after execute()======"+id);
        return id;
    }
 
    public void setId(Long id) {
        System.out.println("setId called after execute()======"+id);
        this.id = id;
        System.out.println("setId called after execute()1======"+id);
    }
}
Connection Class

Code:
package net.shafquat.contact.util;
 
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
 
public class HibernateUtil {
 
    private static final SessionFactory sessionFactory = buildSessionFactory();
 
    private static SessionFactory buildSessionFactory() {
        
        try {
            // Create the SessionFactory from hibernate.cfg.xml
            System.out.println("SessionFactory buildSessionFactory()");
            return new AnnotationConfiguration().configure().buildSessionFactory();
            
        } catch (Throwable ex) {
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
        
    }
    public static SessionFactory getSessionFactory() {
        System.out.println("SessionFactory getSessionFactory() --- "+sessionFactory);
        return sessionFactory;
    
    }
}


Model Class

Code:

package net.shafquat.contact.model;
 
import java.io.Serializable;
import java.sql.Date;
 
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
 
@Entity
@Table(name="Contacts")
public class Contact implements Serializable{
 
    private static final long serialVersionUID = -8767337896773261247L;
 
    private Long id;
    private String firstName;
    private String lastName;
    private String emailId;
    private String cellNo;
    private Date birthDate;
    private String website;
 
    private Date created;
 
    @Id
    @GeneratedValue
    @Column(name="id")
    public Long getId() {
        System.out.println("In getId()=="+id);
        return id;
    }
    @Column(name="firstname")
    public String getFirstName() {
        System.out.println("In getFirstName()=="+firstName);
        return firstName;
    }
    @Column(name="lastname")
    public String getLastName() {
        System.out.println("In getLastName()=="+lastName);
        return lastName;
    }
    @Column(name="email_id")
    public String getEmailId() {
        System.out.println("In getEmailId()=="+emailId);
        return emailId;
    }
    @Column(name="cell_no")
    public String getCellNo() {
        System.out.println("In getCellNo()=="+cellNo);
        return cellNo;
    }
    @Column(name="birthdate")
    public Date getBirthDate() {
        System.out.println("In getBirthDate()=="+birthDate);
        return birthDate;
    }
    @Column(name="website")
    public String getWebsite() {
        System.out.println("In getWebsite()=="+website);
        return website;
    }
    @Column(name="created")
    public Date getCreated() {
        System.out.println("In getCreated()=="+created);
        return created;
    }
    public void setId(Long id) {
        System.out.println("In setId()=="+id);
        this.id = id;
        System.out.println("In setId()=="+id);
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
        System.out.println("In setFirstName()=="+firstName);
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
        System.out.println("In setLastName()=="+lastName);
    }
    public void setEmailId(String emailId) {
        this.emailId = emailId;
        System.out.println("In setEmailId()=="+emailId);
    }
    public void setCellNo(String cellNo) {
        this.cellNo = cellNo;
        System.out.println("In setCellNo()=="+cellNo);
    }
    public void setBirthDate(Date birthDate) {
        this.birthDate = birthDate;
        System.out.println("In setBirthDate()=="+birthDate);
    }
    public void setCreated(Date created) {
        this.created = created;
        System.out.println("In setCreated()=="+created);
    }
    public void setWebsite(String website) {
        this.website = website;
        System.out.println("In setWebsite()=="+website);
    }
}

Controller Class
package net.shafquat.contact.controller; import java.util.List; import org.hibernate.HibernateException; import org.hibernate.classic.Session; import net.shafquat.contact.model.Contact; import net.shafquat.contact.util.HibernateUtil; public class ContactManagerController extends HibernateUtil { public Contact add(Contact contact) { System.out.println("In == public Contact add(Contact contact) == ContactManagerController"); Session session = HibernateUtil.getSessionFactory().getCurrentSession(); System.out.println("In == public Contact add(Contact contact) == ContactManagerController1"); session.beginTransaction(); System.out.println("In == public Contact add(Contact contact) == ContactManagerController2"); session.save(contact); System.out.println("In == public Contact add(Contact contact) == ContactManagerController3"); session.flush(); System.out.println("In == public Contact add(Contact contact) == ContactManagerController4"); session.getTransaction().commit(); System.out.println("In == public Contact add(Contact contact) == ContactManagerController5"); return contact; } public Contact view(Contact contact2) { System.out.println("In == public Contact view(Contact contact2) == ContactManagerController"); Session session = HibernateUtil.getSessionFactory().getCurrentSession(); System.out.println("In == public Contact view(Contact contact2) == ContactManagerController1"); session.beginTransaction(); System.out.println("In == public Contact view(Contact contact2) == ContactManagerController2"); return contact2; } public Contact delete(Long id) { System.out.println("In == public Contact view(Contact contact2) == ContactManagerController2"); Session session = HibernateUtil.getSessionFactory().getCurrentSession(); System.out.println("In == public Contact view(Contact contact2) == ContactManagerController2"); session.beginTransaction(); System.out.println("In == public Contact view(Contact contact2) == ContactManagerController2"); Contact contact = (Contact) session.load(Contact.class, id); System.out.println("In == public Contact view(Contact contact2) == ContactManagerController2"); if(null != contact) { System.out.println("In == if(null != contact) == ContactManagerController2.public Contact delete(Long id)"); session.delete(contact); System.out.println(contact+" ==deleted == ContactManagerController2.public Contact delete(Long id)"); } session.getTransaction().commit(); System.out.println("session.getTransaction().commit();"); return contact; } @SuppressWarnings("unchecked") public List<Contact> list() { System.out.println("Inside public List<Contact> list() "); Session session = HibernateUtil.getSessionFactory().getCurrentSession(); System.out.println("Inside public List<Contact> list() 1"); session.beginTransaction(); System.out.println("Inside public List<Contact> list() 2"); List<Contact> contacts = null; System.out.println("Inside public List<Contact> list() 3"); try { System.out.println("Inside public List<Contact> list()4 "); contacts = (List<Contact>)session.createQuery("from Contact").list(); System.out.println("Inside public List<Contact> list() 5"); } catch (HibernateException e) { System.out.println("in exception HibernateException"); e.printStackTrace(); session.getTransaction().rollback(); } session.getTransaction().commit(); System.out.println("Inside public List<Contact> list()==session.getTransaction().commit(); "); return contacts; } }
Please suggest how i can achive the same.

Thanks in Advance
Shafquat
0
jogep's Avatar, Join Date: May 2012
Newbie Member
1.) Use the new PrepareAndExecute Filter

Code:
<filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter>
2.) How looks your Action Configuration?

3.) Do you call the action or the JSP?