如下所示:
<span><input type="text" th:name="dbType" th:value="${tDbSource.dbType} == null ? 'MySQL5.1' : ${tDbSource.dbType}"/></span>
注意:这里不能用th:field属性,根据官方文档解释,th:field会被解析为th:name和th:value,因此,th:field只支持“${...}”或“*{...}”类型表达式
......${tDbSource.dbType}" is not valid: only variable expressions ${...} or selection expressions *{...} are allowed in Spring field bindings (template: "mvcdemo" - line 23, col 39)
这时若还要使用默认值,可以在对象的构造函数中或是初始化对象时设定默认值
@GetMapping("/")
public ModelAndView index(){
tDbSource.setHostip("127.0.0.1");
tDbSource.setDbType("MySQL5.1");
ModelAndView modelAndView = new ModelAndView("mvcdemo");
modelAndView.addObject("tDbSource",tDbSource);
return modelAndView;
}
网友评论