Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Use of JSTL tag

    • 0
    • 1
    • 1
    • 2
    • 0
    • 0
    • 0
    • 0
    • 566
    Comment on it

    1) Iterate List/HashMap in JSP using JSTL

    To use JSTL in your jsp file, you should first include below statements

    <%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
    

    Suppose you have a generic list of Address class type and there are around 10 rows which you want to render in your JSP using jstl tag. In Address class, there are five properties like laneNo, streetName, houseNo, houseName, ownerName. Suppose there is any method getAddress() which return type is Address

    In Java:

    POJO Class Address

    public class Address 
    {
        private String laneNo;
        private String streetName; 
        private String houseNo; 
        private String houseName;
        private String ownerName;
    
        public String getLaneNo() {
            return laneNo;
        }
        public void setLaneNo(String laneNo) {
            this.laneNo = laneNo;
        }
        public String getStreetName() {
            return streetName;
        }
        public void setStreetName(String streetName) {
            this.streetName = streetName;
        }
        public String getHouseNo() {
            return houseNo;
        }
        public void setHouseNo(String houseNo) {
            this.houseNo = houseNo;
        }
        public String getHouseName() {
            return houseName;
        }
        public void setHouseName(String houseName) {
            this.houseName = houseName;
        }
        public String getOwnerName() {
            return ownerName;
        }
        public void setOwnerName(String ownerName) {
            this.ownerName = ownerName;
        }   
    }
    

    In Controller/Servlet

    List<Address> addressList;
    ...... some other codes here
    .......
    // A function which is returning addressList of type Address class
    addressList = getAddress();
    return request.setAttribute("addressList ", addressList);
    

    In JSP

    <c:forEach var="address" items="addressList">
      <div>
           <label>Lane Number: </label><b> ${address.laneNo} </b>
           <label>Street Name: </label><b> ${address. streetName } </b>
           <label>House Number: </label><b> ${address. houseNo } </b>
           <label>House Name: </label><b> ${address.houseName} </b>
           <label>Name of Owner: </label><b> ${address. ownerName} </b>
       </div>
       <br/><br/>
    </c:forEach>
    

    2) To convert float value into Integer using JSTL

    You first include taglib for fmt like below

    <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
    

    Now you will convert float value into Integer so use this code

    <fmt:parseNumber var="i" integerOnly="true" 
        type="number" **value**="<float_variable_or_value>"/>
    

    value example: 3.0, 2.0 etc

    And this variable 'i' will be a Integer value, now you can put this value into the 'end' attribute of .

    <c:forEach begin="1" end="${i}">
        // HTML code will be here
    </c:forEach>
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: