java - 在 Spring 中用另一个 Controller 重置 Controller

标签 java spring jsp spring-mvc

我是一名 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/

相关文章:

java - 我可以从动态生成的 XML 创建 JSP 吗?

java - Stringtoword vector 无法正常工作 weka

Java - 如何在 Eclipse 中查看断言消息

java - 无法懒惰地初始化角色集合,..无法初始化代理 - 无 session - JPA + SPRING

java - 使用 UserDetailsS​​ervice 时对数据库进行事务调用

java - 将值从 MenuItemTag 传递到 .jsp

java - 在 Eclipse Photon 中使用 Maven 构建 - 如何获取控制台颜色?

java - 如何获取当年1月1日开始至今的小时数?

java - 在 spring boot 应用程序中复制依赖项

java - 在 servlet 中使用资源包?