如何使用select形式将对象传递给Thymeleaf + Spring中的控制器方法

时间:2017-02-04 12:27:19

标签: java spring spring-mvc thymeleaf

嗨,我是Thymeleaf + Spring的新手。我想使用with将多个对象传递给控制器​​,但不知道该怎么做。 这是我的网页:

<!DOCTYPE html>
<html lang="en"
      xmlns:th="http://www.thymeleaf.org">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <title>Aviation Fan</title>
</head>
<body>

<form action="#" th:action="@{/compare}" th:object="${model}" method="post">
    <p>
        <select>
            <option th:each="air:${airports}" th:text="${air.getAirport()}" th:value="${air}">Airports</option>
        </select>

        <select>
            <option th:each="air:${airports}" th:text="${air.getAirport()}" th:value="${air}">Airports</option>
        </select>

        <select>
            <option th:each="air:${aircrafts}" th:text="${air.getType()}" th:value="${air}">Aircraft Model</option>
        </select>
    </p>
    <button type="submit">Submit</button>
</form>

<p><a href="/">Back home</a></p>
</body>
</html>

这是我的控制器:

package application;


import entities.Aircraft;
import entities.Airport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import repositories.AircraftRepository;
import repositories.AirportRepository;

import java.util.ArrayList;

@Controller
public class CompareCotroller {
    @Autowired
    private AircraftRepository aircraftRepository;

    @Autowired
    private AirportRepository airportRepository;

    @GetMapping("/compare")
    public String compare(Airport org, Airport des, Aircraft type, Model model) {

        ArrayList aircrafts = new ArrayList<>();
        for (Aircraft aircraft : aircraftRepository.findAll()) {
            aircrafts.add(aircraft);
        }
        model.addAttribute("aircrafts", aircrafts);

        ArrayList airports = new ArrayList<>();
        for (Airport airport : airportRepository.findAll()) {
            airports.add(airport);
        }
        model.addAttribute("airports", airports);

        return "compare";
    }

    @PostMapping("/compare")
    public String compareRange(Airport org, Airport des, Aircraft type, Model model){
        int range = type.getRange();
        int distance = Calculator.range(org, des);
        if (range > distance) {
            model.addAttribute("enough", "true");
        } else {
            model.addAttribute("enough", "false");
        }
        return "compare";
    }

}

这些是我的实体类:

package entities;

import org.springframework.data.annotation.Id;

public class Aircraft {

    @Id
    private String id;

    private String manufacturer;
    private String model;
    private String subModel;
    private int range;

    public Aircraft() {
    }

    public Aircraft(String manufacturer, String model, String subModel, int range) {
        this.manufacturer = manufacturer;
        this.model = model;
        this.subModel = subModel;
        this.range = range;
    }

    public String getManufacturer() {
        return manufacturer;
    }

    public void setManufacturer(String manufacturer) {
        this.manufacturer = manufacturer;
    }

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }

    public String getSubModel() {
        return subModel;
    }

    public void setSubModel(String subModel) {
        this.subModel = subModel;
    }

    public int getRange() {
        return range;
    }

    public void setRange(int range) {
        this.range = range;
    }

    public String getType() {
        return String.format("%s %s-%s", manufacturer, model, subModel);
    }
}

package entities;

import org.springframework.data.annotation.Id;

public class Airport {

    @Id
    private String id;

    private String iataCode;
    private String city;
    private double[] coordinate;

    public Airport() {
    }

    public Airport(String iataCode, String city, double[] coordinate) {
        this.iataCode = iataCode;
        this.city = city;
        this.coordinate = coordinate;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getIataCode() {
        return iataCode;
    }

    public void setIataCode(String iataCode) {
        this.iataCode = iataCode;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public double[] getCoordinate() {
        return coordinate;
    }

    public void setCoordinate(double[] coordinate) {
        this.coordinate = coordinate;
    }

    public String getAirport() {
        return String.format("%s %s", iataCode, city);
    }
}

我想通过两个机场和一架飞机(所有用户在选择列表中选中)来比较兰格级进行一些计算,但不知道如何...提前致谢

0 个答案:

没有答案