1

I have lots of JSPs containing code which have statements which could be reused like this select statements, inputs, etc.

Here is a sample of a JSP

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
(needed includes)
<%
  ArrayList<Student> studentList = (from database)
%>
<html>
  <head>
    <title>Students</title>
  </head>
  <body>
    <form>
      (other inputs)
      ...
      <select class="combobox">
        <%for (Student studentObj:studentList) { %>
          <option value="<%=studentObj.getId()">
            <%=studentObj.getLastName() %>, <%=studentObj.getFirstName() %>
          </option>
        <%} %>
      </select>
      ...
      (other inputs)
    </form>
  </body>
</html>

What I did do is make a function as follows. This allows me to be able to pass an object parameter and get html code back.

public static getStudentSelect(ArrayList<Student> studentList) {
    String htmlCode = "<select class=\"combobox\">";
    for (Student studentObj:studentList) {  
        htmlCode += "<option value=\"" + studentObj.getId() + "\">" +
        studentObj.getLastName() + ", " + studentObj.getFirstName() +
        "</option>";
    }
    htmlCode += "</select>"
    return htmlCode;
}

Is there a better way of doing this? Because escaping quotes can get messy.

I can't send objects through jsp includes.

I was thinking of using Gagawa

GeekyDaddy
  • 384
  • 2
  • 12

2 Answers2

3

Please don't use scriptlets in JSP. You should use tag files, JSTL and EL to make your own tag library. This way you can easily pass variables as parameters into the reusable components, unlike with JSP fragments and these are much simpler than writing custom tags, when you're dealing with simple logic like looping or creating a table.

Below is an example based on your sample JSP code in the question.

/WEB-INF/tags/student/select.tag:

<%@ taglib prefix=”c” uri=”http://java.sun.com/jsp/jstl/core” %>
<%@ attribute name="studentList" required="true" type="java.util.List" %>

<select class="combobox">
  <c:forEach var="student" items="${studentList}">
    <option value="${student.id}">
      <c:out value="${student.lastName}" />,
      <c:out value="${student.firstName}" />
    </option>
  </c:forEach>
</select>

sample.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix=”student” tagdir=”/WEB-INF/tags/student” %>

<!DOCTYPE html>
<html>
  <head>
    <title>Students</title>
  </head>
  <body>
    <form>
      (other inputs)
      ...
      <student:select studentList="${sessionScope.studentList}" />
      ...
      (other inputs)
    </form>
  </body>
</html>
Community
  • 1
  • 1
t0mppa
  • 3,983
  • 5
  • 37
  • 48
0

To avoid escaping issues. Better try using plain javascript to create html like

  var el = document.createElement('select');
  el.className = "combobox";
  for (Student studentObj:studentList) { 
     var optel = document.createElement('option');
     optel.value = studentObj.getId();
     optel.text = studentObj.getLastName() + ", " + studentObj.getFirstName();
     el.append(optel);
   }
   return el;
Avi
  • 246
  • 2
  • 10