简体   繁体   中英

Serve To Html From Spring RestController

I have a MainController which is a RestController that gets a list of vehicles in order of price using java code in a separate class (CommandInterpreter.java). When run, '/vehicles-by-price' already shows the list I want to format. I want to take the returned list and format it into a table, but I'm unsure how to do this.

I currently have an index.html within 'resources/static' which is displayed on the home page. Is there a way to make a html file for the '/vehicles-by-price' page to display which has the list passed to it (or a js file which I can use in the html) to display in a table?

I'm new to Spring so any code/necessary structure of the files would be helpful!

MainController:

@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;
  }
}

The @RestController will return the response in Rest Style(default json). So if you are building single page application and calling your Rest webservice using ajax call then RestController will be useful.

If you want to show complete new page for products then you can use @Controller which will redirect you to appropriate view using viewResolver and you can show the data stored in ModelAndView.

For using html you can use Thymeleaf which uses html file but have more options to render complex object and supports El.

sample code:

@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;
    }
}

Sample resources/static/vehiclesByPrice.html template:

<!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>

you need to change the @RestController to @Controller, and change return value to your view file path

    @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";

  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM