c:forEach
1 2 3 4 5 | <body> <c:forEach var= "i" begin= "1" end= "100" > <c:out value= "${i}" /><br/> </c:forEach> </body> |
Accessing Value in Bean
SampleMenu.java(bean)
01 02 03 04 05 06 07 08 09 10 11 12 13 14 | public class SampleMenu { String name; public String getName() { return name; } public void setName(String name) { this .name = name; } } |
MenuList.java
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 | List<SampleMenu> arrMenuList = new ArrayList<SampleMenu>(); SampleMenu objSampleMenu = new SampleMenu(); objSampleMenu.setName( "Link1" ); arrMenuList.add(objSampleMenu); objSampleMenu.setName( "Link2" ); arrMenuList.add(objSampleMenu); objSampleMenu.setName( "Link3" ); arrMenuList.add(objSampleMenu); request.setAttribute( "arrMenuList" , arrMenuList); RequestDispatcher dispatcher = request.getServletContext().getRequestDispatcher( "/Sample.jsp" ); dispatcher.forward(request, response); |
Sample.java
1 2 3 4 5 | <body> <c:forEach var= "i" items= "${arrMenuList}" > <c:out value= "${i.name}" /> </c:forEach> </body> |
Output
Link1 Link2 Link3
If Else
1 2 | <c: if test= "${user.userGender eq 1}" >Male</c: if > <c: if test= "${user.userGender eq 0}" >Female</c: if > |
(or)
1 2 3 4 | <c:choose> <c:when test= "${user.userGender eq 1}" >Male</c:when> <c:otherwise>Female</c:otherwise> </c:choose> |
Alternative to If Else Statement
1 | <c:out value= "${user.userGender eq 1 ? 'Male': 'Female'}" /> |
If Else If
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 | <c:choose> <c:when test= "${empty example1}" > <!-- do stuff --> </c:when> <c:otherwise> <c:choose> <c:when test= "${empty example2}" > <!-- do different stuff --> </c:when> <c:otherwise> <!-- do default stuff --> </c:otherwise> </c:choose> </c:otherwise> </c:choose> |
If Else If
1 2 3 4 5 6 7 8 | <c:choose> <c:when test= "${empty example1}" > </c:when> <c:when test= "${empty example2}" > </c:when> <c:otherwise> </c:otherwise> </c:choose> |