{"id":292,"date":"2013-09-04T11:39:06","date_gmt":"2013-09-04T11:39:06","guid":{"rendered":"http:\/\/codethataint.com\/blog\/?p=292"},"modified":"2018-02-22T11:52:54","modified_gmt":"2018-02-22T11:52:54","slug":"jstl-using-basic-tags","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/jstl-using-basic-tags\/","title":{"rendered":"JSTL Using Basic Tags"},"content":{"rendered":"<p><strong>c:forEach<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n &lt;body&gt;\r\n   &lt;c:forEach var=&quot;i&quot; begin=&quot;1&quot; end=&quot;100&quot;&gt;\r\n     &lt;c:out value=&quot;${i}&quot;\/&gt;&lt;br\/&gt;\r\n   &lt;\/c:forEach&gt;\r\n &lt;\/body&gt;\r\n<\/pre>\n<p><strong>Accessing Value in Bean<\/strong><br \/>\n<strong>SampleMenu.java(bean)<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n  public class SampleMenu\r\n  {\r\n    String name;\r\n  \r\n    public String getName()\r\n    {\r\n      return name;\r\n    }\r\n\t\r\n    public void setName(String name)\r\n    {\r\n      this.name = name;\r\n    }\r\n  }\r\n<\/pre>\n<p><strong>MenuList.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n   List&lt;SampleMenu&gt; arrMenuList = new ArrayList&lt;SampleMenu&gt;();\t\t\r\n   SampleMenu objSampleMenu     = new SampleMenu();\r\n\r\n   objSampleMenu.setName(&quot;Link1&quot;);\r\n   arrMenuList.add(objSampleMenu); \r\n\r\n   objSampleMenu.setName(&quot;Link2&quot;);\r\n   arrMenuList.add(objSampleMenu);\r\n\r\n   objSampleMenu.setName(&quot;Link3&quot;);\r\n   arrMenuList.add(objSampleMenu);\r\n\r\n   request.setAttribute(&quot;arrMenuList&quot;, arrMenuList);\r\n   RequestDispatcher dispatcher = request.getServletContext().getRequestDispatcher(&quot;\/Sample.jsp&quot;);\r\n   dispatcher.forward(request, response);\r\n<\/pre>\n<p><strong>Sample.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\"> \r\n   &lt;body&gt;\r\n     &lt;c:forEach\tvar=&quot;i&quot; items=&quot;${arrMenuList}&quot;&gt;\r\n\t     &lt;c:out value=&quot;${i.name}&quot;\/&gt;\r\n     &lt;\/c:forEach&gt;\r\n   &lt;\/body&gt;\r\n<\/pre>\n<p><strong>Output<\/strong><br \/>\nLink1 Link2 Link3<\/p>\n<p><strong>If Else <\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\"> \r\n&lt;c:if test=&quot;${user.userGender eq 1}&quot;&gt;Male&lt;\/c:if&gt;\r\n&lt;c:if test=&quot;${user.userGender eq 0}&quot;&gt;Female&lt;\/c:if&gt;\r\n<\/pre>\n<p><strong>(or)<br \/>\n<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\"> \r\n  &lt;c:choose&gt;\r\n    &lt;c:when test=&quot;${user.userGender eq 1}&quot;&gt;Male&lt;\/c:when&gt;\r\n    &lt;c:otherwise&gt;Female&lt;\/c:otherwise&gt;\r\n  &lt;\/c:choose&gt;\r\n<\/pre>\n<p><strong>Alternative to If Else Statement<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\"> \r\n &lt;c:out value=&quot;${user.userGender eq 1 ? 'Male': 'Female'}&quot;\/&gt;\r\n<\/pre>\n<p><strong>If Else If<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n&lt;c:choose&gt;\r\n    &lt;c:when test=&quot;${empty example1}&quot;&gt; \r\n        &lt;!-- do stuff --&gt;\r\n    &lt;\/c:when&gt; \r\n    &lt;c:otherwise&gt; \r\n        &lt;c:choose&gt;\r\n            &lt;c:when test=&quot;${empty example2}&quot;&gt; \r\n                &lt;!-- do different stuff --&gt;\r\n            &lt;\/c:when&gt; \r\n            &lt;c:otherwise&gt; \r\n                &lt;!-- do default stuff --&gt;\r\n            &lt;\/c:otherwise&gt;\r\n        &lt;\/c:choose&gt;\r\n    &lt;\/c:otherwise&gt; \r\n&lt;\/c:choose&gt;\r\n<\/pre>\n<p><strong>If Else If<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n&lt;c:choose&gt;\r\n    &lt;c:when test=&quot;${empty example1}&quot;&gt;\r\n    &lt;\/c:when&gt;\r\n    &lt;c:when test=&quot;${empty example2}&quot;&gt;\r\n    &lt;\/c:when&gt;\r\n    &lt;c:otherwise&gt;\r\n    &lt;\/c:otherwise&gt;              \r\n&lt;\/c:choose&gt;\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>c:forEach &lt;body&gt; &lt;c:forEach var=&quot;i&quot; begin=&quot;1&quot; end=&quot;100&quot;&gt; &lt;c:out value=&quot;${i}&quot;\/&gt;&lt;br\/&gt; &lt;\/c:forEach&gt; &lt;\/body&gt; Accessing Value in Bean SampleMenu.java(bean) public class SampleMenu { String name; public String getName() { return name; } public void setName(String name) { this.name = name; } } MenuList.java List&lt;SampleMenu&gt; arrMenuList = new ArrayList&lt;SampleMenu&gt;(); SampleMenu objSampleMenu = new SampleMenu(); objSampleMenu.setName(&quot;Link1&quot;); arrMenuList.add(objSampleMenu); objSampleMenu.setName(&quot;Link2&quot;); arrMenuList.add(objSampleMenu); objSampleMenu.setName(&quot;Link3&quot;); arrMenuList.add(objSampleMenu);&hellip; <a href=\"https:\/\/codethataint.com\/blog\/jstl-using-basic-tags\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[52],"tags":[],"class_list":["post-292","post","type-post","status-publish","format-standard","hentry","category-jstl"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/292","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=292"}],"version-history":[{"count":12,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/292\/revisions"}],"predecessor-version":[{"id":2680,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/292\/revisions\/2680"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=292"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=292"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=292"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}