Search code examples
jsonspringhibernatestruts2struts

Null value in json whereas retrieving all the values in log in struts,spring and hibernate


I am using Struts,Spring and Hibernate Integration. I have written a method in DAO implementation to return list object from db.When I trigger the action to call the DAO ,I get correct values in log whereas i get null values in json file. Kindly suggest me some solution.

My basic requirement is that I want to get Json response(for List object) to be sent from action to jsp.

Thanks in advance.

Struts.xml

  <action name="loadJsonAction" class="loadJsonActionClass"
        method="loadGrid">
        <result name="success" type="json">/pages/jsp/index.jsp
        </result>
        <result name="error">/pages/jsp/index.jsp
        </result>
     </action>

Application Context.xml

<bean id="deviceDao" class="com.example.daoImpl.DeviceDaoImpl">
    <property name="hibernateTemplate" ref="hibernateTemplate"></property>
</bean>
<bean id="deviceService"
    class="com.example.serviceImpl.DeviceServiceImpl">
    <property name="deviceDao" ref="deviceDao" />
    <property name="transactionManager" ref="transactionManager"></property>
</bean>

<bean id="loadJsonActionClass" class="com.example.action.DeviceInfoAction">
    <property name="deviceService" ref="deviceService" />
</bean>

Code in Action Class

public String loadGrid() {
    getDeviceService().getDeviceInfoById(1);
    return "success";
}

Code in Service Class

@Override
public List<Device> getDeviceInfoById(Integer id) {
    return getDeviceDao().getDeviceInfoById(id);

}

Code in Dao Class

@Override
public List<Device> getDeviceInfoById(final Integer id) {


    return this.hibernateTemplate.execute(new HibernateCallback() {

        public Object doInHibernate(Session session) {
            Device deviceInfo;
            Criteria criteria = session.createCriteria(Device.class);
            criteria.add(Restrictions.eq("id", id));
            List<Device> deviceList = criteria.list();
            if (deviceList .size() != 0) {
                logger.debug("device name from db "
                        + deviceList .get(0).getDeviceName());
                deviceInfo = deviceList .get(0);

            }

            return deviceList ;

        }
    });
}

JSON Respose

{"accountId":null,"androidVersion":null,"baseLocationId":null,"basebandVersion":null,"bluetoothAddress":null,"brand":null,"buildNumber":null,"device":null,"deviceAddedDate":null,"deviceDetails":null,"deviceFileInfo":null,"deviceId":null,"deviceImageUrl":null,"deviceName":null,"deviceNetworkInfo":null,"deviceSettingsInfo":null,"deviceType":null,"emailId":null,"firstName":null,"groupId":null,"imeiNo1":null,"imeiNo2":null,"isDeviceTracked":null,"isDualMode":null,"isSdCardAvailable":null,"kernelVersion":null,"lastName":null,"lastUpdateBy":null,"lastUpdatedDate":null,"manufacturer":null,"model":null,"nickName":null,"osVersion":null,"phoneNumber":null,"product":null,"rowStatus":null,"screensize":null,"serialNumber":null}

values in log

DEBUG[DeviceDaoImpl$2]: device name from db: generic


Solution

  • Since I am using Spring and ModelDriven in Action, it processes everything as a Model.As there was no field for deviceList before in model,I was not able to get the response as List. Now I added deviceList to model class and set the deviceList to the value retrieved from database using the following code :

    deviceBean.setDeviceList(getDeviceService().getDeviceInfoById(1));
    

    Now it works fine