Java 如何根据操作显示不同的标题?
我有两个方法重定向到相同的Java 如何根据操作显示不同的标题?,java,html,Java,Html,我有两个方法重定向到相同的.html文件。 第一种方法负责保存,第二种方法负责更新。 它有相同的视图,所以我只想将用户移动到相同的视图。 我有。在.html中,我希望有标题用于添加新映射的“新映射”,以及用于更新映射时的“更新映射”。 这些是完成所有工作的方法(重定向到.html文件) save.hml文件 <html> <head> <title>Engine</title> <link rel="styleshe
.html
文件。
第一种方法负责保存,第二种方法负责更新。
它有相同的视图,所以我只想将用户移动到相同的视图。
我有。
在.html
中,我希望有标题用于添加新映射的“新映射”
,以及用于更新映射时的“更新映射”
。
这些是完成所有工作的方法(重定向到.html文件)
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>
引擎
新建/更新映射
..[分钟]
...
....
回应
当您从JSP页面将数据存储到DB中时,不需要为add和update创建两个单独的方法。仅使用一个/save API
@PostMapping(path = "/.../{}/save")
public String newMappingPage(Model model) {
Template template = new Template();
template.setCostIndex("10");
model = new Model();
if (model.getId() > 0) {
model = findById(model.getId());
model.addAttribute("heading", "Update Mapping");
} else {
model.addAttribute("heading", "New Mapping");
}
//Call method to store data from jsp file
return "save";
}
在JSP中:
<html>
<head>
<title>Engine</title>
<link rel="stylesheet" th:href="@{/css/file.css}"/>
</head>
<body>
<h1>${heading}</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>
引擎
${heading}
..[分钟]
...
....
回应
如下更新您的方法:
@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;
}
URL是否包含更新或保存?或者,您只想在服务器上执行此操作?请不要通过破坏您的帖子为其他人做更多的工作。通过在Stack Exchange(SE)网络上发布,您已经在下授予了SE分发内容的不可撤销权利(无论您将来的选择如何)。根据SE政策,分发非故意破坏版本。因此,任何此类破坏性编辑都将恢复。有关删除此网站内容的详细信息,请参阅。
@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;
}