如何从数据库中检索实例并使用struts2和hibernate显示它?

时间:2015-03-16 10:50:40

标签: java hibernate jsp struts2

我想从数据库中显示公司pojo的实例。我能够显示的只是CompanyId,其余的细节都没有显示出来。

viewCompany.jsp

`<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<s:form action="ViewCompany">
<s:textfield name="companyId" label="Enter Company ID">
    <s:param name="companyId" value="company.companyId"></s:param>
</s:textfield>
<s:submit/>
</s:form>
</body>
</html>

` 我正在尝试进入companyId,并且对应于id的值应该显示在显示页面上。

CompanyBean:

`package com.buhin.POJO;

public class Company {
    private int companyId;
    public String companyName;
    public String contactPerson;
    public String mobileNumber;
    public String officeNumber;
    public String mail;
    public String addressLine1;
    public String addressLine2;
    public String cityName;
    public String stateName;
    public String zipcode;

    //public Address address;
    public Company(){}
    public Company(int companyId){
        this.companyId = companyId; 
    }

    public int getCompanyId() {
        return companyId;
    }

    public void setCompanyId(int companyId) {
        this.companyId = companyId;
    }

    public String getCompanyName() {
        return companyName;
    }
    public String getContactPerson() {
        return contactPerson;
    }
    public String getMobileNumber() {
        return mobileNumber;
    }
    public String getOfficeNumber() {
        return officeNumber;
    }
    public String getMail() {
        return mail;
    }
    public String getAddressLine1() {
        return addressLine1;
    }

    public String getAddressLine2() {
        return addressLine2;
    }

    public String getCityName() {
        return cityName;
    }

    public String getStateName() {
        return stateName;
    }

    public String getZipcode() {
        return zipcode;
    }
    public void setCompanyName(String companyName) {
        this.companyName = companyName;
    }
    public void setContactPerson(String contactPerson) {
        this.contactPerson = contactPerson;
    }
    public void setMobileNumber(String mobileNumber) {
        this.mobileNumber = mobileNumber;
    }
    public void setOfficeNumber(String officeNumber) {
        this.officeNumber = officeNumber;
    }
    public void setMail(String mail) {
        this.mail = mail;
    }
    public void setAddressLine1(String addressLine1) {
        this.addressLine1 = addressLine1;
    }

    public void setAddressLine2(String addressLine2) {
        this.addressLine2 = addressLine2;
    }

    public void setCityName(String cityName) {
        this.cityName = cityName;
    }

    public void setStateName(String stateName) {
        this.stateName = stateName;
    }

    public void setZipcode(String zipcode) {
        this.zipcode = zipcode;
    }


    @Override
    public String toString() {
        return "Company [CompanyName=" + getCompanyName()
                + ", ContactPerson=" + getContactPerson()
                + ", MobileNumber=" + getMobileNumber()
                + ", OfficeNumber=" + getOfficeNumber() + ", Mail="
                + getMail() + ", AddressLine1=" + getAddressLine1()
                + ", AddressLine2()=" + getAddressLine2()
                + ", CityName()=" + getCityName() + ", StateName="
                + getStateName() + ", Zipcode=" + getZipcode()
                + "]";
    }   
}

ViewCompany.java(ActionClass)

`

package com.buhin.Action;

import com.buhin.POJO.*;
import com.buhin.DAO.*;
import com.opensymphony.xwork2.ActionSupport;

    public class ViewCompany extends ActionSupport {

        /**
         * 
         */
        private static final long serialVersionUID = 1L;
        private int companyId;
        Company company;

        public Company getCompany(){
            return company;
        }

        public void setCompany(Company company){
            this.company = company;
        }

        public int getCompanyId(){
            return companyId;
        }

        public void setCompanyId(int companyId){
            this.companyId = companyId;
        }
        public String execute(){
            CompanyDAO cdao = new CompanyDAO();
            company = cdao.viewCompany(companyId);      
            //System.out.println(company.toString());

            return "success";
        }

    }

CompanyDAO:

`package com.buhin.DAO;

import com.buhin.POJO.*;
import com.buhin.hibernate.*;
import com.opensymphony.xwork2.ActionSupport;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.SessionFactory;

public class CompanyDAO extends ActionSupport{

    public CompanyDAO(){

    }

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    Company vCompany;
    public String addCompany(Company company){
        SessionFactory sessionfactory = HibernateUtil.getSessionFactory();
        Session session = sessionfactory.openSession();
        Transaction transaction = session.beginTransaction();
        session.save(company);
        transaction.commit();
        session.close();
        return "success";
    }

    public Company viewCompany(int companyId){
        SessionFactory sessionfactory = HibernateUtil.getSessionFactory();
        Session session = sessionfactory.openSession();
        vCompany = (Company) session.get(com.buhin.POJO.Company.class, companyId);
        return vCompany;
    }
}

` companydetails.jsp:

`<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <s:property value="company.companyId"/>
    <s:property value="company.companyName"/>
    <s:property value="company.contactPerson"/>
    <s:property value="company.mobileNumber"/>
    <s:property value="company.officeNumber"/>
    <s:property value="company.mail"/>
    <s:property value="company.addressLine1"/>
    <s:property value="company.addressLine2"/>
    <s:property value="company.cityName"/>
    <s:property value="company.stateName"/>
    <s:property value="company.zipcode"/>

</body>
</html>`
`

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.devMode" value="true"></constant>
    <package name="com.buhin.POJO" extends="struts-default">
        <!-- <action name="AddCompany" class="com.buhin.Action.AddCompany" method="execute">
            <result name="success">/success.jsp</result>
        </action>
 -->        <action name="ViewCompany" class="com.buhin.Action.ViewCompany" method="execute">
            <result name="success">/companydetails.jsp</result>
        </action>
    </package>  
</struts>

`

1 个答案:

答案 0 :(得分:1)

首先,我同意@Aleksandr M,这是一个非常奇怪的结构。但是,这段代码有一些问题。

1)您将从操作方法返回'success'结果,因此您的结果应为<result name="success">/companydetails.jsp</result>

2)您正在将公司对象存储在本地变量(Company viewCompany)中。这在JSP中不可用。 你应该做的是使对象变量。为该变量添加setter-getters并在JSP中使用它,如下所示。

<s:property value="viewCompany.zipcode"/>

希望这有帮助。