1
<c:forEach items="<% EasyLookUp.lookUpList(EasyLookUp.PriceSources); %>" var="priceSourceItem">
                                    ${priceSourceItem }
                                </c:forEach>

this is my code, but the page display

enter image description here

there is just one record, and the java code did not work. can anybody tell me how to handle it?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Jesse
  • 517
  • 5
  • 24

2 Answers2

2

Don't use scriptlets:

Write a Servlet that forwards to your JSP resource. In that Servlet's service(..) method (or the HttpServlet's doXXX(..) method), add a request attribute with the return value of your EasyLookup method.

request.setAttribute("lookupResult", EasyLookUp.lookUpList(EasyLookUp.PriceSources));
// forward to JSP

Then use EL to resolve the attribute in your JSP

<c:forEach items="${lookupResult}" var="priceSourceItem">
     ${priceSourceItem }
</c:forEach>
Community
  • 1
  • 1
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • I got my solution:`<% pageContext.setAttribute("PriceSources", EasyLookUp.lookUpList(EasyLookUp.PriceSources)); %> ${priceSourceItem } ` but I think your solution is better, but not fit for me. – Jesse Mar 24 '14 at 04:57
  • @jesse Yes, you are using page attributes, which are a scope more restrictive than request attributes. If you take anything away from my answer, it should be this: **don't use scriptlets**. – Sotirios Delimanolis Mar 24 '14 at 04:58
  • your anwser is excellent, we should avoid scriptlets, but the attributes in my bean is all original, it can not display on page. – Jesse Mar 24 '14 at 05:04
  • @jesse Sorry, I did not understand. What did you mean by _bean_ and _original_? You mean you don't know what type `lookUpList` will return? – Sotirios Delimanolis Mar 24 '14 at 05:07
  • for example country, the column in bean is countryId, just like 1, 2... but on page, it should show as "US", "UK". My English is not good~ I don't know how to express it accurately – Jesse Mar 24 '14 at 05:10
1

In foreach loop of jstl there is no need to pass the list with jstl tag

<c:forEach items="${EasyLookUp.PriceSources}" var="priceSourceItem">
       ${priceSourceItem }
</c:forEach>
Harshal Patil
  • 6,659
  • 8
  • 41
  • 57