1

私はストラットが初めてで、ストラットと休止状態を使用してjspページからDB(mysql)にいくつかの値を保存しようとしていました。

ただし、アプリケーションは毎回 null 値を保存しており、自動インクリメント ID が増加しています。

私のデータベース構造。:

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

index.jspページ_

<%@ 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>

私の見解: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;
}

}

私のモデル: 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;

}

私のコントローラー: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;
}
}

DBに保存された値は常にnullです...IDを除いて..助けてください

アクションを変更しました...ゲッター/セッターを更新しました

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;
    }




}

OSDetail.java を修正し、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;
    }



}

struts.xml の追加

    <?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>

そして 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>
4

2 に答える 2