1

I am new to struts and was trying to save some values to DB (mysql) from jsp page using struts and hibernate.

But, the application is saving null value every time and auto increment ID is increasing.

My Database structure. :

Table Name | osdetail
-------------------------
   Columns | os_name, 
           | os_version,
           | id,
           | created,
           | notes.

The index.jsp page

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
    <title>OS Manager - Struts2 Hibernate Example</title>
</head>
<body>
 
<h1>OS Manager</h1>
<s:actionerror/>
 
<s:form action="add" method="post">
    <s:textfield name="osdetail.OSname" label="name"/>
    <s:textfield name="osdetail.OSversion" label="version"/>
    <s:textfield name="osdetail.OSnotes" label="notes"/>
    <s:submit value="Add OS Details" align="center"/>

</s:form>
 
 
<h2>OS Details</h2>
<table>
<tr>
    <th>OS Name</th>
    <th>OS Version</th>
    <th>OS Notes</th>
</tr>
<s:iterator value="osdetails_list" var="osdetail">
    <tr>
        <td><s:property value="OSname"/></td>
        <td><s:property value="OSversion"/></td>
        <td><s:property value="OSnotes"/></td>
    </tr> 
</s:iterator>
</table>
</body>
</html>

My View : OSAction.java

 package net.ajeet.os.view;

     import java.util.List;

    import net.ajeet.os.controller.OSManager;
    import net.ajeet.os.model.OSDetail;

     import com.opensymphony.xwork2.ActionSupport;
     import com.opensymphony.xwork2.ModelDriven;


      public class OSAction extends ActionSupport implements ModelDriven<OSDetail> {

private static final long serialVersionUID = 9149826260758390091L;
private OSDetail osdetail= new OSDetail();
private List<OSDetail> osdetails_list;
private Long id;

private OSManager linkController= new OSManager();
@Override
public OSDetail getModel() {
    return osdetail;
}
public OSAction() {
    linkController = new OSManager();
}

public String execute() {
    this.osdetails_list = linkController.list();
    return SUCCESS;
}

public String add() {
    System.out.println("this is oS detail get ID"+osdetail.getId());
    try {
        //linkController.add(getOSDetail());
        linkController.add(osdetail);
        System.out.println("this is oS detail  after add "+getOSDetail());
    } catch (Exception e) {
        e.printStackTrace();
    }
    this.osdetails_list = linkController.list();
    return SUCCESS;
}

public String delete() {
    linkController.delete(getId());
    return SUCCESS;
}

public OSDetail getOSDetail() {
    return osdetail;
}

public List<OSDetail> getOSDetail_list() {


    return osdetails_list;
}   

public void setOSDetail(OSDetail osdetail) {
    this.osdetail = osdetail;

}

public void setOSDetail_list(List<OSDetail> osdetails_list) {
    this.osdetails_list = osdetails_list;
}

public Long getId() {
    return id;
}

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

}

My Model: OSDetail.java

 package net.ajeet.os.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="osdetail")

     public class OSDetail implements Serializable{

private static final long serialVersionUID = -8767337896773261247L;

private Long OSid;

private String OSname;
private String OSversion;
private String OSnotes;

private Date OScreated;

@Id
@GeneratedValue
@Column(name="id")
public Long getId() {

    System.out.println("set os name is os id"+OSid);
    return OSid;

}
@Column(name="os_name")
public String getOS_name() {
    return OSname;
}
@Column(name="os_version")
public String getOS_version() {
    return OSversion;
}
@Column(name="notes")
public String getNotes() {
    return OSnotes;
}

@Column(name="created")
public Date getCreated() {
    return OScreated;
}
public void setId(Long OSid) {
    this.OSid = OSid;
}
public void setOS_name(String OSname) {
    this.OSname = OSname;

}
public void setOS_version(String OSversion) {
    this.OSversion = OSversion;
}
public void setNotes(String OSnotes) {
    this.OSnotes = OSnotes;
}
public void setCreated(Date OScreated) {
    this.OScreated = OScreated;

}

My Contoller :OSManager.java

     import java.util.List;

     import org.hibernate.HibernateException;
     import org.hibernate.classic.Session;

       import net.ajeet.os.model.OSDetail;
       import net.ajeet.os.util.HibernateUtil;

      public class OSManager extends HibernateUtil {

public OSDetail add(OSDetail osdetail) {

    System.out.println("value of the os in OSManager"+osdetail.getId());

    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();
    session.save(osdetail);
    session.getTransaction().commit();
    return osdetail;
}

public OSDetail delete(Long id) {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();
    OSDetail osdetail = (OSDetail) session.load(OSDetail.class, id);
    if(null != osdetail) {
        session.delete(osdetail);
    }
    session.getTransaction().commit();
    return osdetail;
}
@SuppressWarnings("unchecked")
public List<OSDetail> list() {

    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();
    List<OSDetail> osdetails_list = null;
    try {

        osdetails_list = (List<OSDetail>)session.createQuery("from OSDetail").list();

    } catch (HibernateException e) {
        e.printStackTrace();
        session.getTransaction().rollback();
    }
    session.getTransaction().commit();
    return osdetails_list;
}
}

The values saved in DB are always null...except the ID..Please help

Changed the Action...updated getter/setter

package net.ajeet.os.view;

import java.util.List;

import net.ajeet.os.controller.OSManager;
import net.ajeet.os.model.OSDetail;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;


public class OSAction extends ActionSupport  {

    private static final long serialVersionUID = 9149826260758390091L;
    public OSDetail osdetail= new OSDetail();
    private List<OSDetail> osdetails_list;
    public OSDetail getOsdetail() {
        return osdetail;
    }

    public void setOsdetail(OSDetail osdetail) {
        this.osdetail = osdetail;
    }

    private Long id;

    private OSManager linkController= new OSManager();
/*  @Override
    public OSDetail getModel() {
        return osdetail;
    }*/
    public OSAction() {
        linkController = new OSManager();
    }

    public String execute() {
        this.osdetails_list = linkController.list();
        return SUCCESS;
    }

    public String add() {

        try {

            linkController.add(getOsdetail());
            //linkController.add(osdetail);
        } catch (Exception e) {
            e.printStackTrace();
        }
        this.osdetails_list = linkController.list();
        return SUCCESS;
    }

    public String delete() {
        linkController.delete(getid());
        return SUCCESS;
    }

    public List<OSDetail> getOsdetails_list() {
        return osdetails_list;
    }

    public void setOsdetails_list(List<OSDetail> osdetails_list) {
        this.osdetails_list = osdetails_list;
    }

    public Long getid() {
        return id;
    }

    public void setid(Long id) {
        this.id = id;
    }




}

Corrected OSDetail.java, automatically created getter/setter.

     package net.ajeet.os.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="osdetail")

public class OSDetail implements Serializable{

    private static final long serialVersionUID = -8767337896773261247L;

    private Long OSid;

    private String OSname;
    private String OSversion;
    private String OSnotes;

    private Date OScreated;


    @Id
    @GeneratedValue
    @Column(name="id")
    public Long getOSid() {
        return OSid;
    }

    public void setOSid(Long oSid) {
        OSid = oSid;
    }

    @Column(name="os_name")
    public String getOSname() {
        return OSname;
    }

    public void setOSname(String oSname) {
        OSname = oSname;
    }

    @Column(name="os_version")
    public String getOSversion() {

        return OSversion;
    }

    public void setOSversion(String oSversion) {
        OSversion = oSversion;

        System.out.println("value of the os in OSversion in setter"+OSversion);
    }

    @Column(name="notes")
    public String getOSnotes() {
        return OSnotes;
    }

    public void setOSnotes(String oSnotes) {
        OSnotes = oSnotes;
    }

    @Column(name="created")
    public Date getOScreated() {
        return OScreated;
    }

    public void setOScreated(Date oScreated) {
        OScreated = oScreated;
    }



}

Adding struts.xml to

    <?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>
    <constant name="struts.enable.DynamicMethodInvocation"
        value="false" />
    <constant name="struts.devMode" value="false" />

    <package name="default" extends="struts-default" namespace="/">

        <action name="add"
            class="net.ajeet.os.view.OSAction" method="add">
            <result name="success" type="chain">index</result>
            <result name="input" type="chain">index</result>
        </action>



        <action name="index"
            class="net.ajeet.os.view.OSAction">
            <result name="success">index.jsp</result>
        </action>
    </package>
</struts>

And hibernate.cfg.xml

     <?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

    <session-factory>

        <!-- Database connection settings -->
        <property name="connection.driver_class">
            com.mysql.jdbc.Driver
        </property>
        <property name="connection.url">
            jdbc:mysql://localhost:3306/test
        </property>
        <property name="connection.username">root</property>
        <property name="connection.password">Asmita24</property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>

        <!-- SQL dialect -->
        <property name="dialect">
            org.hibernate.dialect.MySQLDialect
        </property>

        <!-- Enable Hibernate's automatic session context management -->
        <property name="current_session_context_class">thread</property>

        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">
            org.hibernate.cache.NoCacheProvider
        </property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

        <!-- Drop and re-create the database schema on startup -->
        <property name="hbm2ddl.auto">update</property>

        <mapping class="net.ajeet.os.model.OSDetail" />

    </session-factory>

</hibernate-configuration>
2
  • Try putting a breakpoint in the OSManager add method and debug to see if you are getting all the details. Commented Jul 19, 2013 at 4:28
  • No , I am getting null as values of os_name, os_version and notes Commented Jul 19, 2013 at 5:49

2 Answers 2

1

Your getter function is wrongly named for the variable osdetail. It should be getOsdetail() instead of getOSDetail(). That is the reason your values from the form are not set and the variable osdetail has blank values. Try changing it. Same goes for the setter method, it should be setOsdetail(). Also, to prevent making such mistake in fututre, you can generate your getter and setter functions automatically from eclipse instead of manually creating it.

Sign up to request clarification or add additional context in comments.

10 Comments

I changed the getter/setter methods as you said. still bad luck
I see now. In your OSDetail.java all the getter/setters have wrong names e.g for OSname it should be getOSname() and setOSname(). Similarly for other fields. These naming forms are required by struts2 to fill up the respective values
I am not sure of the JSP name/values, am I putting them correctly?
It should work now. Try printing osdetail.getOSname() in you action before saving. See is it getting a value or not.
Tried that, still null...null values get inserted in DB..even in OSDetail.java it is null after setter/getter...Is my jsp page correct?
|
0

I don't see anywhere in your OSAction the code to read the values from the jsp. And that is the reason why I think the values are going in as null in the DB.

I suppose it'll work if you get the details from the jsp and set it to osdetail before calling linkController.add(osdetail);.

I am not too well versed with ActionSupport but I think you can read the values from the jsp using getText() method...

Try adding these lines before linkController.add(osdetail); line...

osdetail.setOS_name(getText("osdetail.OSname"));
osdetail.setOS_version(getText("osdetail.OSversion"));
osdetail.setNotes(getText("osdetail.OSnotes"));

6 Comments

osdetail.setOS_name(getText("osdetail.OSname")); value from jsp, that osdetail.OSname how to get it here, it is simply entering osdetail.OSname as value in DB, not the value which I am entering in jsp page
How to get values from the JSP to the Action??
Add getters and setters in your OSAction class. From what I read, Struts 2 will pick the values up with getters and setters.
Have a look at this answer
In struts2 you dont need to read and set values for every property of an object. It is done automatically if you have given getter/setter of the object in your action class and declared the name tags properly in your jsp
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.