如何使用ActionForward传递List作为参数?

时间:2014-02-17 06:29:11

标签: java jsp struts

我正在使用Sturts 1.0和Hibernate构建一个简单的项目。项目的主要功能是从欢迎页面获取实体编号,然后查询数据库以获取有关实体的详细信息,并在Index.jsp页面上显示结果。

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>

行动类

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课程

 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;
                    }
            }

}

但每当我执行项目时,我收到一条错误消息,表明在通过ActionForward传递List时出错。请建议为正确执行项目而进行的任何必要的更改。

1 个答案:

答案 0 :(得分:0)

@Arindam:不要在URL中附加列表对象,而是尝试使用下面附带的代码:

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);    

}

index.jsp中,以下列方式从请求对象获取AssetsList:

request.getAttribute("AssetsList");

检查上述解决方案,让我知道它是怎么回事。