Java 我想使用来自Spring控制器的Iterable在JSP文件中创建和填充列表项

Java 我想使用来自Spring控制器的Iterable在JSP文件中创建和填充列表项,java,spring,jsp,spring-mvc,spring-boot,Java,Spring,Jsp,Spring Mvc,Spring Boot,我有下面的控制器,我想得到一个JSP文件的MediaFile对象的可变长度数组,这样我就可以生成一个html列表 package project.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.Requ

我有下面的控制器,我想得到一个JSP文件的MediaFile对象的可变长度数组,这样我就可以生成一个html列表

package project.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import project.MediaFileRepository;
import project.MediaFile;
import org.springframework.ui.Model;

@Controller
public class SearchController {

    @Autowired
    private MediaFileRepository repository;

    @RequestMapping(value = "/searchmedia", method = RequestMethod.GET)
    public String searchForm(Model model) {
        // This is what I want to get to the JSP file
        Iterable<MediaFile> mediaFiles = repository.findAll();

        // I could also generate the html here and put that into the file
        for (MediaFile mediaFile: repository.findAll()) {
            model.addAttribute("mediaFile", mediaFile);
        }

        return "search";
    }
}
package project.controller;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.stereotype.Controller;
导入org.springframework.web.bind.annotation.RequestMapping;
导入org.springframework.web.bind.annotation.RequestMethod;
导入org.springframework.web.bind.annotation.RequestParam;
导入project.MediaFileRepository;
导入project.media文件;
导入org.springframework.ui.Model;
@控制器
公共类搜索控制器{
@自动连线
私有MediaFileRepository存储库;
@RequestMapping(value=“/searchmedia”,method=RequestMethod.GET)
公共字符串搜索表单(模型){
//这就是我想要得到的JSP文件
Iterable mediaFiles=repository.findAll();
//我也可以在这里生成html并将其放入文件中
对于(MediaFile MediaFile:repository.findAll()){
model.addAttribute(“mediaFile”,mediaFile);
}
返回“搜索”;
}
}
我想在jsp文件中从长度为N的对象数组中获得如下内容:

<ul>
  <li>Info from Object 1 from array</li>
  <li>Info from Object 2 from array</li>
  .
  .
  .
  <li>Info from Object N from array</li>
</ul>
  • 来自数组中对象1的信息
  • 来自数组中对象2的信息
  • . . .
  • 来自对象N的信息来自数组

我已经试着解决这个问题一个小时了。关于它的一件事是,这是一个小组项目,它被决定不使用模板引擎,如百里香叶(虽然,根据这里的答案,我可能不得不说服他们改变这一点)。

Iterable
放入模型中:

model.addAttribute(“mediaFiles”,repository.findAll());
然后使用JSTL在JSP中循环:



Spring与UI组件(JSP)松散耦合,因此如果实际的JSP正在重定向,则需要从Java代码创建任何html

像这样更改代码

@RequestMapping(value = "/searchmedia", method = RequestMethod.GET)
public String searchForm(HttpServletRequest request, Map<String, Object> map) {

    // This is what I want to get to the JSP file
    Iterable<MediaFile> mediaFiles = repository.findAll();

    map.put("mediaFiles", mediaFiles);

    return "search";
}
使用下面的代码段

<c:choose>
<c:when test="${fn:length(mediaFiles) >= 1}">
<ul>
<c:forEach var="mediaFile" items="${mediaFiles}">
  <li>${mediaFile.file_name}</li>
</c:forEach>
</ul>
</c:when>
<c:otherwise>
   // Nothing to Display.
</center>
</c:otherwise>
</c:choose>

  • ${mediaFile.file_name}
//没什么可展示的。
<c:choose>
<c:when test="${fn:length(mediaFiles) >= 1}">
<ul>
<c:forEach var="mediaFile" items="${mediaFiles}">
  <li>${mediaFile.file_name}</li>
</c:forEach>
</ul>
</c:when>
<c:otherwise>
   // Nothing to Display.
</center>
</c:otherwise>
</c:choose>