【问题标题】:Serve To Html From Spring RestController从 Spring RestController 服务到 Html
【发布时间】:2017-11-02 10:07:41
【问题描述】:

我有一个 MainController,它是一个 RestController,它使用单独的类 (CommandInterpreter.java) 中的 java 代码按价格顺序获取车辆列表。运行时,'/vehicles-by-price' 已经显示了我要格式化的列表。我想获取返回的列表并将其格式化为表格,但我不确定如何执行此操作。

我目前在主页上显示的“资源/静态”中有一个 index.html。有没有办法为“/vehicles-by-price”页面制作一个 html 文件以显示传递给它的列表(或我可以在 html 中使用的 js 文件)以显示在表格中?

我是 Spring 新手,因此文件的任何代码/必要结构都会有所帮助!

主控制器:

@RestController
public class MainController {
  //Get CommandInterpreter object
  private CommandInterpreter ci = new CommandInterpreter();
  private List<Vehicle> vehicles;

  MainController () throws Exception {
    //Set up list of vehicles from JSON
    vehicles = ci.parseJsonVehicles();
  }

  @RequestMapping("/vehicles-by-price")
  public List<Vehicle> getVehiclesByPrice() {
    vehicles = ci.getVehiclesByPrice(vehicles);
    return vehicles;
  }
}

【问题讨论】:

    标签: java html spring spring-boot spring-restcontroller


    【解决方案1】:

    @RestController 将以 Rest Style(默认 json)返回响应。因此,如果您正在构建单页应用程序并使用 ajax 调用来调用 Rest web 服务,那么 RestController 将很有用。

    如果您想为产品显示完整的新页面,那么您可以使用@Controller,它将使用 viewResolver 将您重定向到适当的视图,并且您可以显示存储在 ModelAndView 中的数据。

    对于使用 html,您可以使用 Thymeleaf,它使用 html 文件但有更多选项来呈现复杂对象并支持 El。

    示例代码:

    @Controller
    public class MainController {
        //Get CommandInterpreter object
        private CommandInterpreter ci = new CommandInterpreter();
        private List <Vehicle> vehicles;
    
        MainController() throws Exception {
            //Set up list of vehicles from JSON
            vehicles = ci.parseJsonVehicles();
        }
    
        @RequestMapping("/vehicles-by-price")
        public ModelAndView getVehiclesByPrice() {
            vehicles = ci.getVehiclesByPrice(vehicles);
    
            ModelAndView mv = new ModelAndView();
            mv.add("vehicles", vehicles);
            mv.setViewName("vehiclesByPrice");
            return mv;
        }
    }
    

    示例资源/静态/vehiclesByPrice.html 模板:

    <!DOCTYPE HTML>
    <html
        xmlns:th="http://www.thymeleaf.org">
        <head>
            <title>Getting Started: Serving Web Content</title>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        </head>
        <body>
            <table>
                <tr th:each="vehicle : ${vehicles}">
                    <td th:text="${vehicle}">1</td>
                </tr>
            </table>
        </body>
    </html>
    

    【讨论】:

      【解决方案2】:

      您需要将@RestController 更改为@Controller,并将返回值更改为您的视图文件路径

          @Controller
      public class MainController {
        //Get CommandInterpreter object
        private CommandInterpreter ci = new CommandInterpreter();
        private List<Vehicle> vehicles;
      
        MainController () throws Exception {
          //Set up list of vehicles from JSON
          vehicles = ci.parseJsonVehicles();
        }
      
        @RequestMapping("/vehicles-by-price")
        public String getVehiclesByPrice() {
          vehicles = ci.getVehiclesByPrice(vehicles);
      
          ModelAndView model = new ModelAndView();
          model.addObject("vehicles",vehicles)
          return "your view file path";
      
        }
      }
      

      【讨论】:

      • 谢谢,但我应该在哪里创建视图文件?视图文件是 HTML 吗?
      • 如果你使用的是spring boot,在resource文件夹下会有一个template文件夹,那么你可以把html文件放到这个文件夹下。例如return "index" 将被映射到 /template/index.html
      • 你可以放任何你想放的地方,但是位置需要在dispatcher servlet里面的viewresolver中指定
      猜你喜欢
      • 2018-02-03
      • 1970-01-01
      • 2021-01-06
      • 1970-01-01
      • 2017-06-21
      • 2015-07-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多