如何在单个视图中从两个模型中获取价值

时间:2012-05-15 13:10:40

标签: jsp spring-mvc jstl

I have to return values from two modelandview methods to a single jsp `mySoftwarelist.jsp` where i have two tables. Here i have two seperate methods for returning array list 

    ModelAndView getLegacySuiteList()
    ModelAndView getSuiteList()

i returned the arraylist `mySoftwareList`,`mySoftwareLegacyList` to the same view `swl_mySoftwareList` as

public ModelAndView getSuiteList(HttpServletRequest request, HttpServletResponse response) throws Exception
{
ArrayList mySoftwareList = new ArrayList();    
 try{
MySoftwareListHelper mySoftwareListHelper = new MySoftwareListHelper();
userEmailAddr =user.getEmailaddress();      
mySoftwareList = mySoftwareListHelper.getSuites(userEmailAddr);

}
catch(Exception e) {
 e.printStackTrace();
}
ModelAndView mnv = new ModelAndView("swl_mySoftwareList","mySoftwareList",mySoftwareList);
return mnv;

}

public ModelAndView getLegacySuiteList(HttpServletRequest request, HttpServletResponse response) throws Exception
{       
ArrayList mySoftwareLegacyList = new ArrayList();       
try{
MySoftwareListHelper mySoftwareListHelper = new MySoftwareListHelper();
userEmailAddr =user.getEmailaddress();
mySoftwareLegacyList = mySoftwareListHelper.getLegacySuites(userEmailAddr);
}catch(Exception e){
e.printStackTrace();
}

ModelAndView mnv = new ModelAndView(“swl_mySoftwareList”,“mySoftwareLegacyList”,mySoftwareLegacyList); 返回mnv;

}

但它仅返回模型mySoftwareList。 我希望arraylist的数据可以在同一个jsp上使用。

2 个答案:

答案 0 :(得分:2)

在返回视图之前,您需要在同一个mnv对象中单独添加两个列表。

ModelAndView mnv = new ModelAndView("swl_mySoftwareList","mySoftwareList",mySoftwareList); 
mnv.addObject("mySoftwareLegacyList",mySoftwareLegacyList);

return mnv;

希望这有帮助。

干杯。

答案 1 :(得分:0)

我认为最佳做法是使用DTO对象从两个实体获取所需的属性,然后将该DTO对象传递给视图。