2014-02-17 1 views
0

나는 Sturts 1.0과 Hibernate를 사용하는 간단한 프로젝트를 만들고있다. 프로젝트의 주요 기능은 시작 페이지에서 엔티티 번호를 가져온 다음 엔티티에 관한 세부 정보를 데이터베이스에 쿼리하고 Index.jsp 페이지에 결과를 표시하는 것입니다.ActionForward를 사용하여 List를 매개 변수로 전달하는 방법은 무엇입니까?

스트럿츠 - 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; 
        } 
      } 

} 

그러나 wheneve r 프로젝트를 실행 중입니다. ActionForward를 통해 List를 전달하는 동안 오류를 나타내는 오류 메시지가 표시됩니다. 프로젝트를 올바르게 실행하기 위해 필요한 변경 사항을 제안하십시오.

+0

스택 추적을 참조하십시오. – Sanjeev

답변

0

@Arindam :

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를 얻을 : URL에 목록 개체를 추가 않는다 대신 사용하려면 아래에 부착 된 코드를 시도

request.getAttribute("AssetsList"); 

위의 해결책을 확인하고 어떻게 진행되는지 알려주십시오.

관련 문제