简体   繁体   中英

How to retrieve an instance from the database and display it using struts2 and hibernate?

I want to display an instance of the company pojo from the database. All I am able to display is the CompanyId the rest of the details are not getting displayed.

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>

` I am trying to enter the companyId and the values corresponding to the id should be displayed to the display page.

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>

`

First of all I agree with @Aleksandr M, this is a very bizarre structure. But nevertheless, there are a few things wrong with this code.

1) You are returning 'success' result from action methods, so your result should be <result name="success">/companydetails.jsp</result>

2) You are storing Company object in local variable( Company viewCompany ). This will not be available in your JSP. What you should do is make a object variable. Add setter-getters for that variable and use it in your JSP as follows.

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

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM