1.如果需要传递的参数较少,比如只需要两个字段id和name,后台方法参数可以直接写这两个字段的类型及字段名 例如:
前台页面:
<form action="" th:action="@{/mttitle/query}" method="post">
Id:<input type="text" name="id" th:field="${mtTitleBean.id}"/><br>
title_cd:<input type="text" name="titleCd" th:field="${mtTitleBean.titleCd}"/><br>
<button type="submit">検索</button>
</form>
后台方法:参数直接写字段
@ResponseBody
@RequestMapping("query")
public List<MttitleBean> queryList(BigInteger id, String titleCd) {
List<MttitleBean> list = mttitleService.queryList(id, titleCd);
return list;
}
2.如果需要的参数较多可以利用javabean来传递 例如:
前台页面:
form表单中,需要 th:object="${mtTitleBean}" 替换对象,th:field="*{id}" 用来绑定后台对象和表单数据
<form th:object="${mtTitleBean}" action="" th:action="@{/mttitle/objectQuery}" method="post">
Id:<input type="text" name="id" th:field="*{id}"/><br>
title_cd:<input type="text" name="titleCd" th:field="*{titleCd}"/><br>
<button type="submit">検索</button>
</form>
后台:参数利用Javabean类传递
@ResponseBody
@RequestMapping("objectQuery")
public List<MttitleBean> queryList(MttitleBean mtTitleBean) {
List<MttitleBean> list = mttitleService.queryList(mtTitleBean.getId(),mtTitleBean.getTitleCd());
return list;
}
本文介绍使用Thymeleaf处理简单的表单数据两种方式:直接传递字段与使用JavaBean。前者适用于少量参数场景,后者适用于参数较多的情况。通过示例展示了如何从前端收集数据并传递到后端。
653





