2016-09-23 94 views
0

我有傳遞ARGS從HTML麻煩使用thymeleaf 3.傳遞參數與thymeleaf

這裏Spring MVC的控制器4彈簧控制器,所述控制器:

// Handler method for inserting currency: 
@RequestMapping(value = "/saveCurrency", method = RequestMethod.POST) 
public ModelAndView saveCurrency(@ModelAttribute("currency") Currency currency, BindingResult bindingResult, Model model) 
{ 
    model.addAttribute("currency", currency); 
    currDAO.insert(currency); 

    return new ModelAndView("redirect:/listCurr"); 
} 

和HTML形式:

<form action="/saveCurrency" th:action="@{/saveCurrency}" th:object="${currency}" th:method="post"> 
    <input id="nameTB" type="text" th:field="*{name}" class="form-control" maxlength="3" style="text-transform:uppercase" /> 
    ... 
</form> 

我也有班級「ID」和「名稱」,並與他們的getter和setter類貨幣。 現在有了這個代碼,我得到一個錯誤:

SEVERE: Servlet.service() for servlet [dispatcher] in context with path [] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "ServletContext resource [/WEB-INF/views/listCurr.html]")] with root cause

java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'currency' available as request attribute

任何想法,我究竟做錯了什麼?

回答

1

問題是以前未設置模型屬性。你需要有一定的要求映射方法設置模型屬性:

@RequestMapping(value = "/currency", method = RequestMethod.GET) 
public String currencyPage(Model model) { 
    model.addAttribute("currency", new Currency()); 
    return "listCurr"; 
} 

圖片的標題說明:在你的方法,你並不需要設置「貨幣」模型ATTR。再次。此外,要檢查是否BindingResult有錯誤您保存貨幣之前:

if(!bindingResult.hasErrors()) { 
    currDAO.insert(currency); 
} 
+0

我得到相同的錯誤... – CoffeePen

0

不重定向,有鑑於收益模型。

model.addAttribute("currency", currency); 
return new ModelAndView("listr");