java - 如何显示不同的标题取决于动作?
问题描述
我有两种方法可以重定向到同一个.html
文件。第一种方法是负责保存的方法,第二种方法是更新。它有相同的视图,所以我只想将用户移动到相同的视图。我想拥有<h1>..</h1>
添加新映射以及何时更新映射的标题。这些是完成所有工作的方法(重定向到 .html 文件).html
"New Mapping"
"Update mapping"
@RequestMapping(path = "/.../update", method = RequestMethod.GET)
public String updatePage(@RequestParam("...") String ..., Model model) {
String[] tokens = ....split("_");
Template template= class.method(...);
model.addAttribute("template", template);
return "save";
}
@RequestMapping(path = "/.../save")
public String newMappingPage(Model model) {
Template template = new Template();
template.setCostIndex("10");
model.addAttribute("template", template);
return "save";
}
@RequestMapping(path = "/.../save", method = RequestMethod.POST)
public String saveMapping(@ModelAttribute Template template) {
class.method(template);
return "redirect:/main-page";
}
save.hml
文件
<html>
<head>
<title>Engine</title>
<link rel="stylesheet" th:href="@{/css/file.css}"/>
</head>
<body>
<h1>New/Update mapping</h1>
<form action="save" method="post" th:object="${template}">
<fieldset>
<label for="...">....[min]</label>
<input type="number" id="..." th:field="*{...}" step="0.1" min="0"/>
<label for="...">...</label>
<input type="number" id="...." th:field="*{...}" required="true"/>
<label for="...">....</label>
<input type="text" id="..." th:field="*{...}" maxlength="1024" size="50"/>
<br/>
<br/>
<label for="response">Response</label>
<br/>
<textarea id="response" rows="20" cols="150" th:field="*{...}" required="true"/>
<br/>
<input id="submit" type="submit" class="primary" value="Submit"/>
</fieldset>
</form>
</body>
</html>
解决方案
像这样更新您的方法:
@PostMapping(path = "/.../{}/save")
public ModelAndView save(Model model) {
ModelAndView obj = new ModelAndView("save");
model = new Model();
if (model.getId() > 0) {
model = findById(model.getId());
obj .addAttribute("heading", "Update Mapping");
} else {
obj .addAttribute("heading", "New Mapping");
}
//Call method to store data from jsp file
return obj;
}
推荐阅读
- javascript - 用于获取数据和发布数据的 React 和 Firebase 连接
- stm32 - 无法在循环模式下使用 ADC 和 DMA 重新编程 STM32 (F401RE)
- vba - if 循环上的运行时错误 451 以设置最小比例
- java - 通过杰克逊将 LocalDateTime 字段转换为 JSON 字符串会导致逗号分隔的日期字段?
- json - 可编码对象到 JSON 转换的问题
- ios - NSLayoutConstraint 在操场上
- openssl - 为什么验证比签名快得多?
- javascript - TypeError:fn.bind 不是 Discord.js v12 上的函数
- laravel - 将变量发送到组件时增加页面渲染时间
- phpstorm - 为什么添加新书签后书签列表为空?