2017-10-09 321 views
0

我要分配該對象($ {的categorys})到ArrayList的如何將EL值分配給一個變量在Java

這是JSP

<% 

    ArrayList<Category> al =${categorys}; 

%>   

這是控制器

@RequestMapping(value = "/cat", method = RequestMethod.GET) 
public ModelAndView getTrancationHistory() { 

    ArrayList<Category> allData = service.viewAllCategory(); 
    //handle your code here... 
    System.out.println(allData); 
    for (Category allData1 : allData) { 
     System.out.println(allData1.getCategoryName()); 
    } 


    ModelAndView modelAndView = new ModelAndView(); 
    modelAndView.setViewName("WEB-INF/views/cat"); 
    modelAndView.addObject("categorys", allData); 
    return modelAndView; 
} 

回答

1

你不能像使用jsp scriptlet那樣使用${}。您必須手動獲取它。你應該嘗試這樣的事情;

<% 
List<Category> al = (List<Category>) request.getAttribute("categorys"); 
%> 

<% 
List<Category> al = (List<Category>) pageContext.getAttribute("categorys", pageContext.REQUEST_SCOPE); 
%>