Question

I am Building a simple Project Using Sturts 1.0 and Hibernate. The main function of the Project is to Take the Entity Number from the Welcome page and Then Query the Database for the details regarding the entity and show the result on Index.jsp page.

Struts-config.xml

<struts-config>
<form-beans>
    <form-bean name="welcomeForm"
        type="tutorial.Struts.Form.welcomeForm" />
</form-beans> 

<action-mappings>
     <action path="/viewAsset" 
             name="welcomeForm"
             type="tutorial.Struts.Action.ViewAllAsset"     
             scope="request"         
             validate="true"
             input="welcome.jsp">
             <forward name="success" path="/index.jsp" />
             <forward name="failure" path="/welcome.jsp" />
     </action>
</action-mappings>

Action Class

public class ViewAllAsset extends Action {

    @SuppressWarnings("unused")
    public ActionForward execute(ActionMapping mapping , ActionForm form , 
            HttpServletRequest request , HttpServletResponse response)
    throws Exception{
        //HttpSession page_session = request.getSession(true);
        //String entity_id = page_session.getAttribute("entity_id").toString();
        List<Asset> result = null;
        String target = null;
        welcomeForm wForm = (welcomeForm)form;
        String entity_id = wForm.getEntity_id();
        AssetDataDAO ad = new AssetDataDAO();
        result = ad.GetAssetList(entity_id);                            /*Fetch the list*/
        List<Asset> asl = new ArrayList<Asset>();

        for(int i=0;i< result.size();i++) {
                Asset veh = new Asset();  
                veh.setAssetId(result.get(i).getAssetId());       /*set the asset POJO */
                veh.setEntityId(result.get(i).getEntityId());
                veh.setAssetTypeId(result.get(i).getAssetTypeId());
                veh.setFuelType(result.get(i).getFuelType());
                asl.add(veh);
        }
        if(result != null)
            target = "success";
        else
            target = "failure"; 


    ActionForward forward = mapping.findForward(target);    
    forward = new ActionForward( addParameterToUrl(forward.getPath(),"List",asl),forward.getRedirect() );
    return forward;
    //return new ActionForward(forward.getPath()+"list="+asl);

    }

    private String addParameterToUrl(String url, String parameter, List<Asset> asl) {
        // TODO Auto-generated method stub
        return  url + ( (url.indexOf("?")==-1) ? "?" : "&" ) + parameter + "=" +asl;
    }


}

DAO class

 public class AssetDataDAO {

    public List<Asset> GetAssetList(String entity_id)
            throws Exception{
                Session session = null;
                String sql = "from Asset asset where asset.entityId=:entityId and asset.isActive='Y'";
                try
                    {
                    session = HibernateSessionFactory.getSession();
                    List<Asset> assetList = session.createQuery(sql).setString("entityId", entity_id).list();
                    return assetList;
                    }
                catch(HibernateException hibernateException)
                    {
                    throw hibernateException;
                    }
                catch(Exception exception)
                    {
                    throw exception;
                    }
            }

}

But whenever I am Executing the project I am getting an error message indicating an error during passing of List through ActionForward. Please suggest any necessary changes that should be made to execute the project properly.

Était-ce utile?

La solution

@Arindam : do not append list object in the URL instead try to the use the below attached code:

public class ViewAllAsset extends Action {

    @SuppressWarnings("unused")
    public ActionForward execute(ActionMapping mapping , ActionForm form , 
            HttpServletRequest request , HttpServletResponse response)
    throws Exception{
        //HttpSession page_session = request.getSession(true);
        //String entity_id = page_session.getAttribute("entity_id").toString();
        List<Asset> result = null;
        String target = null;
        welcomeForm wForm = (welcomeForm)form;
        String entity_id = wForm.getEntity_id();
        AssetDataDAO ad = new AssetDataDAO();
        result = ad.GetAssetList(entity_id);                            /*Fetch the list*/
        List<Asset> asl = new ArrayList<Asset>();

        for(int i=0;i< result.size();i++) {
                Asset veh = new Asset();  
                veh.setAssetId(result.get(i).getAssetId());       /*set the asset POJO */
                veh.setEntityId(result.get(i).getEntityId());
                veh.setAssetTypeId(result.get(i).getAssetTypeId());
                veh.setFuelType(result.get(i).getFuelType());
                asl.add(veh);
        }
        if(result != null)
            target = "success";
        else
            target = "failure"; 

        request.setAttribute("AssetsList", asl);


    return mapping.findForward(target);    

}

And in the index.jsp, get the AssetsList from the request object in the following way:

request.getAttribute("AssetsList");

Check the above solution and let me know how it goes.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top