Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从springrestcontroller服务于Html_Java_Html_Spring_Spring Boot_Spring Restcontroller - Fatal编程技术网

Java 从springrestcontroller服务于Html

Java 从springrestcontroller服务于Html,java,html,spring,spring-boot,spring-restcontroller,Java,Html,Spring,Spring Boot,Spring Restcontroller,我有一个MainController,它是一个RestController,在一个单独的类(commandexplorer.java)中使用java代码按价格顺序获取车辆列表。运行时,“/vehicles by price”已显示我要格式化的列表。我想把返回的列表格式化成一个表格,但我不知道怎么做 我目前在主页上显示的“resources/static”中有一个index.html。有没有一种方法可以为“/vehicles by price”页面创建一个html文件,以显示传递给它的列表(或者可

我有一个MainController,它是一个RestController,在一个单独的类(commandexplorer.java)中使用java代码按价格顺序获取车辆列表。运行时,“/vehicles by price”已显示我要格式化的列表。我想把返回的列表格式化成一个表格,但我不知道怎么做

我目前在主页上显示的“resources/static”中有一个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;
  }
}
@RestController
公共类主控制器{
//获取命令解释器对象
private commandexplorer ci=new commandexplorer();
私家车名单;
MainController()引发异常{
//从JSON设置车辆列表
vehicles=ci.parseJsonVehicles();
}
@请求映射(“/按价格列出的车辆”)
公共列表getVehiclesByPrice(){
车辆=ci.getVehiclesByPrice(车辆);
返回车辆;
}
}

您需要将@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";

  }
}
@控制器
公共类主控制器{
//获取命令解释器对象
private commandexplorer ci=new commandexplorer();
私家车名单;
MainController()引发异常{
//从JSON设置车辆列表
vehicles=ci.parseJsonVehicles();
}
@请求映射(“/按价格列出的车辆”)
公共字符串getVehiclesByPrice(){
车辆=ci.getVehiclesByPrice(车辆);
ModelAndView模型=新的ModelAndView();
模型.添加对象(“车辆”,车辆)
返回“查看文件路径”;
}
}

您需要将@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";

  }
}
@控制器
公共类主控制器{
//获取命令解释器对象
private commandexplorer ci=new commandexplorer();
私家车名单;
MainController()引发异常{
//从JSON设置车辆列表
vehicles=ci.parseJsonVehicles();
}
@请求映射(“/按价格列出的车辆”)
公共字符串getVehiclesByPrice(){
车辆=ci.getVehiclesByPrice(车辆);
ModelAndView模型=新的ModelAndView();
模型.添加对象(“车辆”,车辆)
返回“查看文件路径”;
}
}

@RestController将以Rest样式(默认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;
    }
}
@控制器
公共类主控制器{
//获取命令解释器对象
private commandexplorer ci=new commandexplorer();
私家车名单;
MainController()引发异常{
//从JSON设置车辆列表
vehicles=ci.parseJsonVehicles();
}
@请求映射(“/按价格列出的车辆”)
公共模型和查看getVehiclesByPrice(){
车辆=ci.getVehiclesByPrice(车辆);
ModelAndView mv=新ModelAndView();
mv.添加(“车辆”,车辆);
mv.setViewName(“车辆比价”);
返回mv;
}
}
示例资源/static/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>

入门:提供Web内容
1.

@RestController将以Rest样式(默认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;
    }
}
@控制器
公共类主控制器{
//获取命令解释器对象
private commandexplorer ci=new commandexplorer();
私家车名单;
MainController()引发异常{
//从JSON设置车辆列表
vehicles=ci.parseJsonVehicles();
}
@请求映射(“/按价格列出的车辆”)
公共模型和查看getVehiclesByPrice(){
车辆=ci.getVehiclesByPrice(车辆);
ModelAndView mv=新ModelAndView();
mv.添加(“车辆”,车辆);
mv.setViewName(“车辆比价”);
返回mv;
}
}
示例资源/static/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>

入门:提供Web内容
1.

谢谢,但是我应该在哪里创建视图文件呢?查看文件是HTML吗?如果您使用的是spring boot,则资源文件夹下会有一个模板文件夹,然后您可以将HTML文件放在该文件夹下。e、 g.return“index”将映射到/template/index.htmly您可以放在任何您想要的地方,但是需要在dispatcher Servlet内的viewresolver中指定位置谢谢,但是我应该在哪里创建视图文件?查看文件是HTML吗?如果您使用的是spring boot,则资源文件夹下会有一个模板文件夹,然后您可以将HTML文件放在该文件夹下。e、 g.返回“index”将映射到/template/index.htmlYou can p