使用Hibernate 3和Struts 2. cgf文件没有被读取。是因为JARS吗?

时间:2016-06-21 07:27:05

标签: java hibernate

这是我的`DAO

    package com.sheeba;

    import com.sheeba.LeavePojo;

    import java.io.Serializable;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Map;

    import org.hibernate.Criteria;
    import org.hibernate.Query;
    import org.hibernate.SQLQuery;
    import org.hibernate.Session;

    import org.hibernate.Transaction;
    import org.hibernate.transform.Transformers;

    import com.googlecode.s2hibernate.struts2.plugin.annotations.SessionTarget;
    import com.googlecode.s2hibernate.struts2.plugin.annotations.TransactionTarget;

    public class LeaveDAO {

        @SessionTarget
        Session session;

        @TransactionTarget
        Transaction trans;

        List<LeavePojo> myList = new ArrayList<LeavePojo>();

        // LeavePojo pojoObj=new LeavePojo();
        public String saveToDb(LeavePojo pojoObj) {
            try {

                System.out.println("inside saveToDb method-before query");
                System.out.println("bank id" + pojoObj.getBank_id());
                // Serializable isSaved =session.save(pojoObj);
                System.out.println("session" + session);
                session.saveOrUpdate(pojoObj);

                // System.out.println("llllllllllllllllllllllll"+session.save(pojoObj).getClass().getName());

            } catch (Exception e) {
                e.printStackTrace();
            }

            return "SUCCESS";
        }

        public List<LeavePojo> retrieveData() {
            myList = session.createQuery("from LeavePojo").list();
            return myList;
        }

        /*public String fetchBankId()
            {
              try{
                  System.out.println("inside fetch  bank id -- before query");
                  String hql = "SELECT l.bank_id FROM LeavePojo l";
                  Query query = session.createQuery(hql);
                  System.out.println("deeeeeeeeeeeeeeeeeeeeeee"+query.list());
                  List results = query.list();
                 System.out.println("RESSSSSSSSSSSUUUUUUUUUUUUUULLLLLLLLLTttasssss"+results);
                  }catch (Exception e) {
                   e.printStackTrace();
                  }

        return"success";


        }*/}



service

    package com.sheeba;

import java.util.ArrayList;


import java.util.List;

import com.opensymphony.xwork2.ActionSupport;

import com.opensymphony.xwork2.ModelDriven;

import com.sheeba.*;

import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.ParentPackage;

@ParentPackage("hibernate-default")
@InterceptorRef("basicStackHibernate")
public class LeaveService implements ModelDriven {
    LeavePojo modobj = new LeavePojo();
    LeaveDAO leaveDAO=new LeaveDAO();
    //private LeaveDAO leaveDAO;                                   // this is the dependency injection

    public LeaveDAO getLeaveDAO() {
        return leaveDAO;
    }

    public void setLeaveDAO(LeaveDAO leaveDAO) {
        this.leaveDAO = leaveDAO;
    }


    public List<LeavePojo> myList = new ArrayList<LeavePojo>();

    public List<LeavePojo> getMyList() {
        return myList;
    }

    public void setMyList(List<LeavePojo> myList) {
        this.myList = myList;
    }

    public List<LeavePojo> saveDetailsService(LeavePojo modobj) {

        System.out.println("inside savedetails service");
        leaveDAO.saveToDb(modobj);
        myList = leaveDAO.retrieveData();

        return myList;

    }



    @Override
    public Object getModel() {

        return modobj;
    }


}

这是行动

package com.sheeba;

import java.util.ArrayList;
import java.util.List;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.sun.org.apache.bcel.internal.generic.NEW;

public class LeaveAction extends ActionSupport implements ModelDriven<LeavePojo> {
    LeaveService ls=new LeaveService();
    LeavePojo pojo =new LeavePojo();
    public List<LeavePojo> myList = new ArrayList<LeavePojo>();

    public List<LeavePojo> getMyList() {
        return myList;
    }

    public void setMyList(List<LeavePojo> myList) {
        this.myList = myList;
    }
    private String bank_id;
    private String emp_name;
    private String leaves_applied;

    public String getBank_id() {
        return bank_id;
    }

    public void setBank_id(String bank_id) {
        this.bank_id = bank_id;
    }

    public String getEmp_name() {
        return emp_name;
    }

    public void setEmp_name(String emp_name) {
        this.emp_name = emp_name;
    }

    public String getLeaves_applied() {
        return leaves_applied;
    }

    public void setLeaves_applied(String leaves_applied) {
        this.leaves_applied = leaves_applied;
    }

    public String getServiceLasyer(){
        try{
        System.out.println(pojo.getBank_id()+"is bank id");
         myList=ls.saveDetailsService(pojo);
        }catch(Exception e){
            e.printStackTrace();
        }
        return SUCCESS;

    }

    @Override
    public LeavePojo getModel() {
        // TODO Auto-generated method stub
        return pojo;
    }



}

在这里使用过这些罐子/AAAAANEWPROJECT/WebContent/WEB-INF/lib/hibernate3.jar /AAAAANEWPROJECT/WebContent/WEB-INF/lib/hibernate-annotations-3.2.1.ga.jar /AAAAANEWPROJECT/WebContent/WEB-INF/lib/hibernate-core.jar /AAAAANEWPROJECT/WebContent/WEB-INF/lib/struts2-fullhibernatecore-plugin-2.2.2-GA.jar。对于休眠。

支柱:

<?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.devMode" value="false" />
<package name="default" extends="struts-default,hibernate-default">
<action name="saveAction" class="com.sheeba.LeaveAction" method="getServiceLasyer">
<result name="success">/index.jsp</result>
</action>

</package> 
</struts>

hib.config

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM  
"hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
        <property name="hibernate.connection.url">jdbc:sqlserver://10.112.178.78:1433;databaseName=Test_DB</property>
        <property name="hibernate.connection.username">TestUser1</property>
        <property name="connection.password">Test123</property>
        <property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>

        <property name="show_sql">true</property>
 <mapping resource="LeavePojo.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

hib.mapping:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping SYSTEM  
"hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="com.sheeba.LeavePojo" table="LeavePojo">
        <id name="bank_id" type="string" column="BANK_ID">
        </id>
        <property name="emp_name" column="EMP_NAME" type="string" />
        <property name="leaves_applied" column="LEAVES_APPLIED" type="string" />
    </class>
</hibernate-mapping>

我无法获得输出。 404错误来了。罐子有问题吗?

1 个答案:

答案 0 :(得分:0)

我解决了!我从sourceforge DTD下载并将其添加到我的根目录,即SRC文件夹。 并且,编辑了hibernate.cfg.xml,如下所示:

<!DOCTYPE hibernate-configuration SYSTEM  
"hibernate-configuration-3.0.dtd">

通过这种方式,应用程序不必点击URL,但它指向并在src文件夹中找到DTD。您可以在线使用系统时执行此操作。 经过大量研究后,IT对我有用!!