当前位置: 首页 > 知识库问答 >
问题:

ThymeLeaf不打印列表的值

蔡宏大
2023-03-14

我有以下html:

<html lang="en" xmlns:th="http://www.thymeleaf.org">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">

    <title>Sightings</title>
  </head>
<body>

    <div class="container">
                <div class="row m-4">
                <div class="col text-center border border-dark">
                    <h1><a href="/" class="text-dark">Sightings Manager</a></h1>
                </div>
            </div>
            <div class="row m-4 border border-dark">
                <div class="col text-center m-3">
                    <a href="heroes" class="btn btn-outline-primary btn-lg">Heroes</a>
                </div>

                <div class="col text-center m-3">
                    <a href="" class="btn btn-outline-primary btn-lg">Organizations</a>
                </div>

                <div class="col text-center m-3">
                    <a href="locations" class="btn btn-outline-primary btn-lg">Locations</a>
                </div>
                <div class="col text-center m-3">
                    <a href="sightings" class="btn btn-outline-primary btn-lg">Sightings</a>

                </div>
               
                
            </div>
              <div class="row m-4 border border-dark">
               <div class="col text-left border border-dark">
              <input type="radio" id="selectByDate" name="selectOption" value="selectByDate">
              <label for="selectByDate">Sightings By Date</label><br>

              <input type="radio" id="selectByHero" name="selectOption" value="selectByHero">
              <label for="selectByHero">Sightings by Hero</label>

              
              <div class="col text-center" id="date" style="display:none;">
              <form action="getLocationsByDate" method="GET">
              <select id="chooseDate" name="chooseDate">
              <option selected disabled>Choose a date</option>
              </select>
              <input type="submit" value="View Locations">
              </form>
              </div>
              <!-- Other div to go here with other form -->
              </div>
            </div>
              
            <div class="row m-4 border border-dark">
                <div class="col text-center m-3">
                    <table class="table table-striped">
                        <thead>
                            <tr>
                                <th>ID</th>
                                <th>Name</th>
                                <th>Address</th>
                                <th>Latitude</th>
                                <th>Longitude</th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr th:each="location : ${locations}">
                                <td th:text="${location.id}">Location ID</td>
                                <td th:text='${location.name}'>Location Name</td>
                                <td th:text="${location.address}">Location Address</td>
                                <td th:text="${location.latitude}">Location Latitude</td>
                                <td th:text="${location.longitude}">Location Longitude</td>

                                
          
                             
                            </tr>
    
                        </tbody>
                    </table>
                </div>
            
            
            </div>
            
            
            
            </div>
 <script src="https://code.jquery.com/jquery-3.5.1.min.js" integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"></script>
 <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
 <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js" integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI" crossorigin="anonymous"></script>
<script src="sightings.js"></script>
</body>
</html>

以及以下javascript:

$(document).ready(selectOption);

function selectOption()
{
    $('input:radio[name="selectOption"]').change(function()
                                        {
                                            if($(this).val() == "selectByDate")
                                            {
                                                $("#date").show();
                                                $.ajax({
                                                    type:"GET",
                                                    url:"http://localhost:8080/getDates",
                                                    success: function(dates)
                                                    {
                                                        console.log(dates.length);
                                                        for(let i=0;i<dates.length;i++)
                                                        {
                                                                    optionText=dates[i];
                                                                    optionValue=dates[i];
                                                                    $("#chooseDate").append(new Option(optionText,optionValue));
                                                        }
                                                        //if viewlocations has been clicked
                                        
                                                    }
                                                    
                                                    
                                                });
                                                
                                                
                                            }
                                            else if($(this).val() == "selectByHero")
                                            {
                                                $('#chooseDate')
                                                    .empty()
                                                    .append('<option selected disabled>Choose a date</option>'); //remove all data as will stack on top of each other
                                                $("#date").hide();
                                            }
                                        });
}

两者都通过Spring mvc中的控制器运行:

@Controller
public class SightingsController 
{
@Autowired
HeroDAO heroDao;

@Autowired
LocationDao locationDao;


@GetMapping("sightings")
public String displayLocations()
{
    return "sightings";
    
}
@GetMapping(value="getDates",produces="application/json")
@ResponseBody
public List<LocalDate> getDates()//returns all dates of herosightings and adds it to model
{
    List<Sighting> sightings = locationDao.getAllSightings();
    List<LocalDate> dates = new ArrayList<LocalDate>();
    
    for(int i=0;i<sightings.size();i++)
    {
        dates.add(sightings.get(i).getDate());
    }
    return dates;
}
@GetMapping("getLocationsByDate")
public String getLocationsByDate(HttpServletRequest request,Model model)
{
    String date =request.getParameter("chooseDate");
    LocalDate localDate = LocalDate.parse(date);
    List<Location> locations = locationDao.sightingsOnDate(localDate);
    model.addAttribute("locations",locations);
    return "redirect:/sightings";
    
}

我的javascript通过使用ajax调用控制器public List getDates()来填充选择框,该调用返回日期列表并将其添加到html中。然后用户选择其中一个项目并将其再次提交给控制器以公共String getLocationsByDate(HttpServletRequest,模型模型)。此函数应该在页面底部(在tbody中)添加一个模型并打印出所有结果。我的问题是thymeleaf似乎根本没有读取值(tbody中没有出现任何内容)。我确实知道提交给 /getLocationsByDate的值和列表位置=locationDao.sightingsOnDate(localDate);接收到的值是正确的,但是在html页面上执行. add属性似乎什么都不是。任何帮助都将不胜感激,我希望这不是一些语法错误。

编辑:此外,如果需要查看位置对象中的内容:

public class Location 
{
private int id;
private String name;
private String description;
private String address;
private String latitude;
private String longitude;
public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public String getDescription() {
    return description;
}
public void setDescription(String description) {
    this.description = description;
}
public String getAddress() {
    return address;
}
public void setAddress(String address) {
    this.address = address;
}
public String getLatitude() {
    return latitude;
}
public void setLatitude(String latitude) {
    this.latitude = latitude;
}
public String getLongitude() {
    return longitude;
}
public void setLongitude(String longitude) {
    this.longitude = longitude;
}
@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((address == null) ? 0 : address.hashCode());
    result = prime * result + ((description == null) ? 0 : description.hashCode());
    result = prime * result + id;
    result = prime * result + ((latitude == null) ? 0 : latitude.hashCode());
    result = prime * result + ((longitude == null) ? 0 : longitude.hashCode());
    result = prime * result + ((name == null) ? 0 : name.hashCode());
    return result;
}
@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    Location other = (Location) obj;
    if (address == null) {
        if (other.address != null)
            return false;
    } else if (!address.equals(other.address))
        return false;
    if (description == null) {
        if (other.description != null)
            return false;
    } else if (!description.equals(other.description))
        return false;
    if (id != other.id)
        return false;
    if (latitude == null) {
        if (other.latitude != null)
            return false;
    } else if (!latitude.equals(other.latitude))
        return false;
    if (longitude == null) {
        if (other.longitude != null)
            return false;
    } else if (!longitude.equals(other.longitude))
        return false;
    if (name == null) {
        if (other.name != null)
            return false;
    } else if (!name.equals(other.name))
        return false;
    return true;
}



}

共有1个答案

黄隐水
2023-03-14

我已经解决了这个问题(虽然我不知道为什么这个解决方案有效……还需要做进一步的研究)。我在控制器中返回html,而不是使用重定向。这似乎是造成问题的原因。

 类似资料:
  • 我试图使用递归打印链表中每个节点中的数据,但是我得到了越界错误,所以我认为递归函数有问题。 这是头文件: 基本上,我从公共函数调用私有助手函数。下面是两个函数的代码: 我认为问题出在if块中,因为如果没有下一个节点,我需要停止,但在返回之前还需要打印当前节点中的数据,但因为我已经调用了

  • 问题内容: 我在Python中有一个列表 我想在没有正常的“ []的情况下在单行中打印数组 将给出的输出为; 那不是我想要的格式,而是我希望它像这样; 注意:它必须在一行中。 问题答案: 听起来很简单,它只接受列表中的所有元素,然后将它们加入。

  • 问题内容: 我在Python中有一个列表 我想在没有正常的“ []的情况下在一行中打印数组 将给出的输出为; 那不是我想要的格式,而是我希望它像这样; 注意:它必须在一行中。 问题答案: 听起来,这只是获取列表中的所有元素,然后将它们加入。

  • 问题内容: 我有一个清单清单: 我想要以下格式的输出: 我已经按照以下方式尝试过,但是输出的方式不是理想的: 输出: 在更改打印调用以代替使用时: 输出: 有任何想法吗? 问题答案: 遍历原始列表中的每个子列表,并在打印调用中使用以下命令将其解压缩: 默认情况下,分隔设置为,因此无需显式提供分隔。打印: 在您的方法中,您要遍历每个子列表中的每个元素,并分别进行打印。通过使用您在打印调用中 解压缩

  • 问题内容: 我正在读取文件并将其存储在t1中。如何访问t1中的元素?当我尝试打印时,我得到的是地址而不是值。还有和之间的区别是什么? 输出: 问题答案: String []是一个字符串数组,因此其未如您所愿打印的原因,请尝试: 或更简洁: 或者更好:

  • 问题内容: 考虑使用此Python代码来打印逗号分隔值列表 如果列表中的最后一个元素是不显示逗号的首选打印方法。 前 问题答案: