我开始使用百里香。
我正在关注此页:Spring MVC view layer: Thymeleaf vs. JSP
我上课了:
public class MyMainObject {
private String a;
private String b;
private String c;
private String d;
private String e;
// getters and setters
}
我也有一个控制器:
@Controller
public class MyMainObjectController extends AbstractController
@RequestMapping({"/subscribeth"})
public String getObj(final MyMainObject subscription) {
return "subscribeth";
}
}
这是我的HTML代码:
<form action="#" th:object="${subscription}" th:action="@{/subscribeth}">
<fieldset>
<div>
<label for="a" th:text="#{subscription.a}">a:
</label> <input type="text" th:field="*{a}" />
</div>
<div>
<label for="b" th:text="#{subscription.b}">b:
</label> <input type="text" th:field="*{b}" />
</div>
<div>
<label for="c" th:text="#{subscription.c}">c: </label>
<input type="text" th:field="*{c}" />
</div>
<div>
<label for="d" th:text="#{subscription.d}">d:
</label> <input type="text" th:field="*{d}" />
</div>
<div>
<label for="e" th:text="#{subscription.e}">e:
</label> <input type="text" th:field="*{e}" />
</div>
<div class="submit">
<button type="submit" name="save" th:text="#{subscription.submit}">Subscribe me!</button>
</div>
</fieldset>
</form>
当我运行mu应用程序时出现错误:
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateProcessingException: Error during execution of processor 'org.thymeleaf.spring3.processor.attr.SpringInputGeneralFieldAttrProcessor'
org.thymeleaf.exceptions.TemplateProcessingException: Error during execution of processor 'org.thymeleaf.spring3.processor.attr.SpringInputGeneralFieldAttrProcessor'
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'subscription' available as request attribute
看起来,我必须创建bean subscription
。
这是我的问题,我该怎么做?我从之前提到的教程中下载了源代码,但是找不到它。
提前谢谢
答案 0 :(得分:3)
使用@ModelAttribute("subscription")
注释控制器处理程序方法的参数。默认情况下,如果没有给定值,Spring将根据参数的类型生成属性名称。因此MyMainObject
将成为myMainObject
。
答案 1 :(得分:2)
我是初学者,当我遇到这个问题时,通过添加model.addAttribute(&#34; modelname&#34;,new modelObj())解决了这个问题。在返回视图名称之前,例如:
@RequestMapping(value="/login", method=RequestMethod.GET)
public String showLogin(Model model, HttpSession session)
{
model.addAttribute("userForm", new UserForm());
return "users";
}