java - 为什么 PetClinic 示例中 Vets 类的 VetList 方法使用 XMLElement?

标签 java spring jsp spring-mvc

我正在研究 Spring MVC 的 PetClinic 示例。

1) 我不确定 Vets class 什么时候叫?在评论中回答,链接到 call it 的类(class)位置.

2) 是 vetList.jsp 使用的吗? ?

3) 为什么它返回 XML?

Vets.java

package org.springframework.samples.petclinic.model;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
/**
* Simple domain object representing a list of veterinarians. Mostly here to be used for the 'vets' {@link
* org.springframework.web.servlet.view.xml.MarshallingView}.
*
* @author Arjen Poutsma
*/
@XmlRootElement
public class Vets {
    private List<Vet> vets;

    @XmlElement
    public List<Vet> getVetList() {
       if (vets == null) {
              vets = new ArrayList<Vet>();
       }
       return vets;
     }
}

最佳答案

这只是为了演示 xml 响应,没有具体原因。我认为这里使用的是它。

<!DOCTYPE html> 

<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="datatables" uri="http://github.com/dandelion/datatables" %>

<html lang="en">


<jsp:include page="../fragments/staticFiles.jsp"/>

<body>
<div class="container">
    <jsp:include page="../fragments/bodyHeader.jsp"/>

    <h2>Veterinarians</h2>

    <datatables:table id="vets" data="${vets.vetList}" row="vet" theme="bootstrap2" cssClass="table table-striped" pageable="false" info="false">
        <datatables:column title="Name">
            <c:out value="${vet.firstName} ${vet.lastName}"></c:out>
        </datatables:column>
        <datatables:column title="Specialties">
            <c:forEach var="specialty" items="${vet.specialties}">
                <c:out value="${specialty.name}"/>
            </c:forEach>
            <c:if test="${vet.nrOfSpecialties == 0}">none</c:if>
        </datatables:column>
    </datatables:table>

    <table class="table-buttons">
        <tr>
            <td>
                <a href="<spring:url value="/vets.xml" htmlEscape="true" />">View as XML</a>
            </td>
            <td>
                <a href="<spring:url value="/vets.atom" htmlEscape="true" />">Subscribe to Atom feed</a>
            </td>
        </tr>
    </table>

    <jsp:include page="../fragments/footer.jsp"/>
</div>
</body>

</html>

关于java - 为什么 PetClinic 示例中 Vets 类的 VetList 方法使用 XMLElement?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28735835/

相关文章:

jquery - Mysql 查询与 JSP 和 JQuery 无需刷新

java - 将 post 数据从 JSP 发送到 Java Servlet

Java Applet、AWT 刷新、Mac OS X 10.4 上的问题

java - Hibernate:记录到数据库后无法获取数据

java - 对多个属性使用 Spring @Value 注解

java - 使用选择选项标签从Java中的数据库中检索记录

java - 没有 onActivityResult 的 Activity 之间的 Android 通信

java - 拼写校正算法

java - 让我的程序分别读取 3 行

spring - 如何在 web.xml 中将 HttpServlet 与 Spring Application Context 连接起来?