我是一名 Java 开发实习生,这是我的第一个问题,所以请不要评判我!
我有一个可处理 jsp 文件的 Controller 类。第一个 jsp (userinput.jsp) 有 3 个文本字段(纬度、经度、半径)和 2 个按钮(提交、应用默认值)。第二个 jsp 只是一个充满数据的 HTML 表(取决于用户输入)和一个重置按钮,该按钮应返回到起始页 (userinput.jsp) 并删除所有现有数据。我该怎么做?
额外问题:如果我尝试在第二个状态(html 表)刷新页面,浏览器会生成一条警告,提示我将丢失所有数据并且不应刷新。我怎样才能摆脱这个?
@Controller
@EnableAutoConfiguration
class SpringBootController implements InitLogger {
@GetMapping(value="/geohash")
public String getUserInput(ModelMap model) {
model.put("command", new Tuple());
return "UserInput";
}
@PostMapping(value="/geohash", params="SubmitWithDefault")
public String defaultUserInput(ModelMap model) {
model.put("command", tupleFill (48.104564, 20.800041, 6) );
return "UserInput";
}
@PostMapping(value = "/geohash", params="Submit")
public String printHash(@ModelAttribute("user")Tuple tuple,ModelMap model) {
GetData.setLat1(tuple.getFirstCoordinate());
GetData.setLon1(tuple.getSecondCoordinate());
GetData.setRad1(tuple.getRadius());
LocationExecute.calculate();
model.addAttribute("geoItemList", LocationExecute.getTupleList());
model.addAttribute("listSize", LocationExecute.getTupleList().size());
return "Geohash";
}
@PostMapping(value = "/geohash", params="reset", method = RequestMethod.GET)
public ModelAndView method() {
return new ModelAndView("redirect:geohash");
}
}
userinput.jsp - 按钮
<input type="submit" name="Submit" value="Submit" style="height:25px; width:100px"/>
<input type="submit" name="SubmitWithDefault" value="Default Values" style="height:25px; width:100px">
geohash.jsp - (html 表)重置按钮
<input type="reset" name="reset" value="Reset" style="height:30px; width:100px">
最佳答案
您需要在MVC中实现PRG(Post-Redirect-Get)设计模式来解决这个问题。 请浏览以下内容以获取更多信息 http://www.c-sharpcorner.com/UploadFile/dacca2/implement-prg-pattern-in-mvc-architecture/
因此,不要返回 View 名称,而是将其重定向,这样您的问题就可以解决,如果您希望将一些数据发送到重定向方法,则使用 spring 的 flashAttributes
关于java - 在 Spring 中用另一个 Controller 重置 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39081651/