2017-05-08 3 views
0

데이터가 일부 있는데 표시하고 싶습니다. telnumbers 및 'teltype`은 소유자의 한 열에 있어야합니다.표를 올바르게 표시하는 방법은 무엇입니까?

enter image description here

이 내 코드 :

나는이 가지고

<?xml version="1.0" encoding="UTF-8" standalone="no"?> 
 
<div xmlns:jsp="http://java.sun.com/JSP/Page" 
 
    xmlns:c="http://java.sun.com/jsp/jstl/core" 
 
    xmlns:spring="http://www.springframework.org/tags" 
 
    version="2.0"> 
 
    <jsp:directive.page contentType="text/html;charset=UTF-8"/> 
 
    <jsp:output omit-xml-declaration="yes"/> 
 

 
    <style> 
 
     table, th, td { 
 
      border: 1px solid black; 
 
     } 
 
    </style> 
 

 
    <h1>Contact list</h1> 
 

 
    <c:if test="${not empty contacts}"> 
 
     <table> 
 
      <thead> 
 
      <tr> 
 
       <th>First Name</th> 
 
       <th>Last Name</th> 
 
       <th>Birth Date</th> 
 
       <th>tel_type</th> 
 
       <th>tel_number</th> 
 
       <th>hobby</th> 
 
      </tr> 
 
      </thead> 
 
      <tbody> 
 
      <c:forEach items="${contacts}" var="contact"> 
 
       <jsp:useBean id="contact" scope="page" type="org.training.support.model.Contact"/> 
 
       <tr> 
 
       <td>"${contact.firstName}"</td> 
 
       <td>"${contact.lastName}"</td> 
 
       <td>"${contact.birthDate}"</td> 
 
        <c:forEach items="${contact.contactDetails}" var="telDetail"> 
 
         <jsp:useBean id="telDetail" scope="page" type="org.training.support.model.ContactTelDetail"/> 
 
          <td>"${telDetail.telNumber}"</td> 
 
          <td>"${telDetail.telType}"</td> 
 
        </c:forEach> 
 
        <c:forEach items="${contact.hobbies}" var="hobby"> 
 
         <jsp:useBean id="hobby" scope="page" type="org.training.support.model.Hobby"/> 
 
          <td>"${hobby.id}"</td> 
 
        </c:forEach> 
 
       </tr> 
 
      </c:forEach> 
 
      </tbody> 
 
     </table> 
 
    </c:if> 
 
</div>

+0

각 데이터에 대한 TD를 사용하지 마십시오 다음됩니다. 대신에 두 개의 구분 기호를 사용하십시오. –

+0

이것이 도움이 될 것이라고 생각합니다. "두 열에 걸쳐있는 셀"을 확인하십시오. https://www.w3schools.com/html/tryit.asp?filename=tryhtml_table_span. 도움이되지 않으면 다시 물어보십시오 –

+0

미안하지만, 내가 어떻게 임의의 colspan 내 예제 (이 좋은 예제를 만들 수 있지만, 내 경우에는 작동하지 않는 원인, contact.contactDetails.size() 정의되지 않은 (무작위) ( –

답변

1

여기에 크기는 fn 수 있도록 페이지의 상단에있는이 태그 라이브러리를 추가 귀하의 솔루션. JSP에 다음 태그를 추가하십시오. 여기

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

그리고

는 변경

<h1>Contact list</h1> 

<c:if test="${not empty contacts}"> 
    <table> 
     <thead> 
     <tr> 
      <th>First Name</th> 
      <th>Last Name</th> 
      <th>Birth Date</th> 
      <th colspan="${fn:length(contact.contactDetails)}">tel_type & tel_number</th> 
      <th colspan="${fn:length(contact.hobbies)}">hobby</th> 
     </tr> 
     </thead> 
     <tbody> 
     <c:forEach items="${contacts}" var="contact"> 
      <jsp:useBean id="contact" scope="page" type="org.training.support.model.Contact"/> 
      <tr> 
      <td>"${contact.firstName}"</td> 
      <td>"${contact.lastName}"</td> 
      <td>"${contact.birthDate}"</td> 
       <c:forEach items="${contact.contactDetails}" var="telDetail"> 
        <jsp:useBean id="telDetail" scope="page" type="org.training.support.model.ContactTelDetail"/> 
         <td>"${telDetail.telType}" - "${telDetail.telNumber}"</td>       
       </c:forEach> 
       <c:forEach items="${contact.hobbies}" var="hobby"> 
        <jsp:useBean id="hobby" scope="page" type="org.training.support.model.Hobby"/> 
         <td>"${hobby.id}"</td> 
       </c:forEach> 
      </tr> 
     </c:forEach> 
     </tbody> 
    </table> 
</c:if> 

입니다 그리고 그 결과는 이미지

Example result

+0

오, 이보다 더 좋습니다. 나는 이런 것을 찾고 있었다. –

0

테이블이 제대로 '일'의 수를 표시 할 일치 할 필요가 'tr'내의 'td'수 :

당신의 테이블에 대한 액세스를하지 않고귀하의 경우에는
<table> 
    <thead> 
    <tr> 
     <th>"Header 1"</th> 
     <th>"Header 2"</th> 
     <th>"Header 3"</th> 
    </tr> 
    </thead> 
    <tbody> 
    <tr> 
     <td> "Value1"</td> 
     <td> "Value2"</td> 
     <td> "Value3"</td> 
    </tr> 
    </tbody> 

알 어렵지만 때문에 당신이 사용하는 :

<c:forEach items="${contact.contactDetails}" var="telDetail">

이로 인해 어쩌면 헤더의 당신의 #에서 당신에게 6 이상의 TD를주고있다 테이블의 값이 비어 있거나 null 값이 있는지 확인하십시오. null 값을 없애고 해당 필드에 단일 공간을 남기고 다시 시도하거나 단일 for 루프로 여러 foreach를 바꾸고 모든 인덱스를 해당 인덱스로 지정하십시오. 예를 들어

:

<c:if test="${not empty contacts}"> 
    <table> 
     <thead> 
     <tr> 
      <th>First Name</th> 
      <th>Last Name</th> 
      <th>Birth Date</th> 
      <th>tel_type</th> 
      <th>tel_number</th> 
      <th>hobby</th> 
     </tr> 
     </thead> 
     <tbody> 
     <c:forEach begin="0" end="${fn:length(contacts)}" var="i"> 
      <jsp:useBean id="contact" scope="page" type="org.training.support.model.Contact"/> 
      <tr> 
       <td>"${contacts[i].firstName}"</td> 
       <td>"${contacts[i].lastName}"</td> 
       <td>"${contacts[i].birthDate}"</td> 
       <td>"${contact.contactDetails[i].telNumber}"</td> 
       <td>"${contact.contactDetails[i].telType}"</td> 
       <td>"${contact.hobbies[i].id}"</td> 
      </tr> 
     </c:forEach> 
     </tbody> 
    </table> 
</c:if> 

있는지 확인하십시오 당신은 내가 관련된 코드를 추가 한

<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> 
+0

당신이 의미하는 바를 깨닫도록 도와 줄 수 있습니까? 나는 여러 가지 예제를 시도하지만 성공하지 못했다. ( –

+0

나는 당신의 콜렉션이나 테이블을 가지고 있지 않기 때문에 내가 테스트 할 수 없었던 방법을 제안했다. 누군가가 에러를 발견한다면 알려주십시오, 난 그냥 도우려고 노력하고있어 – AbelSurace

+0

오 마이 갓)
)을 잊어 버렸습니다.)) 그러나, 감사합니다. –

관련 문제