首页 > 解决方案 > 如何显示不同的标题取决于动作?

问题描述

我有两种方法可以重定向到同一个.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>

标签: javahtml

解决方案


像这样更新您的方法:

@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;
}

推荐阅读