有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何将数据从thymeleaf HTML页面发送到MVC spring引导控制器?

我不熟悉thymeleaf和spring,我试图将简单的输入数据从索引页发送到控制器,因此我创建了一个对象交换,如下所示,其中将包含要处理的输入字段。问题是,我不断收到图片中显示的错误

这是我的交换对象

package myprivate.work.MyCurrencyConverter.model;
public class Exchange
{
    private String fromcurrency;
    private String tocurrency;
    private double amount;
    public Exchange(){
       super();
    }
    public Exchange(String fromcurrency, String tocurrency, double amount) {
        super();
        this.fromcurrency = fromcurrency;
        this.tocurrency = tocurrency;
        this.amount = amount;
    }
    public String getFromcurrency() {
        return fromcurrency;
    }
    public void setFromcurrency(String fromcurrency) {
        this.fromcurrency = fromcurrency;
    }
    public String getTocurrency() {
        return tocurrency;
    }
    public void setTocurrency(String tocurrency) {
        this.tocurrency = tocurrency;
    }
    public double getAmount() {
        return amount;
    }
    public void setAmount(double amount) {
        this.amount = amount;
    }
}

这是我在索引中的表格

<form th:action="@{/newexchange}" th:object="${exchange}" method='POST'>
    <p>Select From Currency:</p>
    <p>
        <select th:field="*{fromcurrency}"> <!-- this is line 12 -->
            <option th:value="sek">SEK</option>
            <option th:value="eur">EUR</option>
            <option th:value="usd">USD</option>
            <option th:value="jpy">JPY</option>
        </select>
    </p>
    <p>Select To Currency:</p>
    <p>
        <select th:field="*{tocurrency}">
            <option th:value="sek">SEK</option>
            <option th:value="eur">EUR</option>
            <option th:value="usd">USD</option>
            <option th:value="jpy">JPY</option>
        </select>
    </p>
    <p class="form"><label>Insert New Rate:</label><input type="number" th:field="*{amount}"/>
    </p>
    <p><input name="Convert" type="submit" value="submit"/></p>
</form>

在我的控制器里

@RequestMapping(method = RequestMethod.POST , value = "/newexchange")
public String toExchange(Exchange exchange, BindingResult result)
{
    return "..ok..";
}

这是errorand


共 (2) 个答案

  1. # 1 楼答案

    使用@modeldattribute从索引页获取数据到控制器-

     @RequestMapping(method = RequestMethod.POST , value = "/newexchange")
        public String toExchange(@ModelAttribute Exchange exchange, BindingResult result)
        {
           logger.logInfo("Exchange Amount->>"+exchange.getAmount());
          return "..ok..";
        }
    
  2. # 2 楼答案

    我认为您应该在控制器中使用@ModelAttribute来捕获Exchange对象,如下所示:

    Controller:
    
        @RequestMapping(value = "/showForm", method=RequestMethod.GET)
    public String showForm(Model model) {
      Foo foo = new Foo();
      foo.setBar("bar");
    
      model.addAttribute("foo", foo);
      ...
    }
    
    @RequestMapping(value = "/processForm", method=RequestMethod.POST)
    public String processForm(@ModelAttribute(value="foo") Foo foo) {
      ...
    }
    
    
    html :
    
    <form action="#" th:action="@{/processForm}" th:object="${foo}" method="post">
      <input type="text" th:field="*{bar}" />
      <input type="submit" />
    </form>
    
    Model Data :
    
    public class Foo {
      private String bar;
    
      public String getBar() {
        return bar;
      }
    
      public void setBar(String bar) {
        this.bar = bar;
      }
    }