Как вернуть строку, если логическое значение истинно - PullRequest
0 голосов
/ 18 мая 2018

В классе Student конструктор записывается с некоторым значением.Я хочу напечатать Да или Нет в столбце VIP.т. е. да, когда логическое значение равно true и наоборот.

public class Student {

private String firstName;
private String lastName;
private boolean goldCustomer;

   public Student(String firstName, String lastName, boolean goldCustomer) {
      super();
      this.firstName = firstName;
      this.lastName = lastName;
      this.goldCustomer = goldCustomer;
   }

  public String getFirstName() {
    return firstName;
  }

  public void setFirstName(String firstName) {
    this.firstName = firstName;
  }

  public String getLastName() {
    return lastName;
  }

  public void setLastName(String lastName) {
    this.lastName = lastName;
  }

  public boolean isGoldCustomer() {
    return goldCustomer;
  }

  public void setGoldCustomer(boolean goldCustomer) {       
    this.goldCustomer = goldCustomer;       
  }

}

Моя страница JSP

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page import="java.util.* , com.jsptag.demo.Student" %>
<%
  List<Student> students = new ArrayList< >();
  students.add( new Student("Rahul" , "Rawat" , false));
  students.add( new Student("Rohit" , "Negi" , true));
  students.add( new Student("Mahesh" , "Gupta" , false));   
  pageContext.setAttribute("myStudents", students);
%>

<html>
<body>
<table border="1">
  <tr>
     <th>First Name</th>
     <th>Last Name</th>
     <th>VIP ?</th>
  </tr>
  <c:forEach var="data" items="${myStudents}">
    <tr> 
      <td>${data.firstName}</td> 
      <td>${data.lastName}</td>  
      <td>${data.goldCustomer}</td> 
    </tr>   
  </c:forEach>  
</table>
</body>
</html>

1 Ответ

0 голосов
/ 18 мая 2018

Должен работать с троичным оператором:

<td> ${data.goldCustomer ? 'Yes' : 'No'} </td>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...