java - 提交值的空值
问题描述
关于
这是关于带有 Thymeleaf 模板引擎的 Spring Boot 2 框架
问题详情
尝试汇总输入页面中的单选按钮值时,输出页面显示空提交值。
枚举
public enum GameOptions {
A,
B,
C
}
模型
public class UserData {
private GameOptions _gameOption;
public GameOptions get_gameOption() {
return _gameOption;
}
public void set_gameOptions(GameOptions _gameOption) {
this._gameOption = _gameOption;
}
}
控制器
@Controller
public class myController {
@GetMapping({"/input"})
public String inputPage(Model model) {
model.addAttribute("UserData", new UserData());
return "input";
}
@PostMapping("/output")
public String outputPage(UserData UserData, Model model) {
model.addAttribute("UserData", UserData);
return "output";
}
}
模板 - 输入页面
<form method="post" th:action="@{/output}" th:object="${UserData}">
<div th:each="model : ${T(com.example.demo.GameOptions).values()}">
<div>
<input type="radio" th:field="${UserData._gameOption}" th:value="${model}">
<label th:for="${model}" th:text="${model}">model</label>
</div>
</div>
<input type="submit" value="Submit" />
</form>
模板 - 输出页面
<p th:text="'id: ' + ${UserData._gameOption}" />
输出页面显示 id: null
解决方案
您遇到的问题是由于您在 UserData 上的 getter 和 setter。
这将起作用
class UserData {
private GameOptions gameOption;
public GameOptions getGameOption() {
return gameOption;
}
public void setGameOption(GameOptions gameOption) {
this.gameOption = gameOption;
}
}
输入.html
...
<input type="radio" th:field="*{gameOption}" th:value="${model}">
...
输出.html
<p th:text="'id:' + ${UserData.gameOption}" />
推荐阅读
- reactjs - 如何将道具从组件传递到 NextJS 中的 getServerSideProps()?
- ios - 如何使用 URLSession 在后台上传 zip 文件?
- python - 在 pandas read_csv() 中指定有序类别
- kubernetes - 如何用破折号设置环境?
- opengl - 纹理参数 GL_GENERATE_MIPMAP 是否只更新被更新的纹理区域?
- asp.net - 如何验证asp net core中的唯一属性
- python - 有没有办法检测两个文本是否与同一个品牌有关?
- c++ - 逐字节读取文件(C++)
- floating-point - 绕线算法和浮点误差
- python - 如何在 Seaborn 传奇中结合色调和风格组?