【问题标题】:Thymeleaf th:href invoking both a post and getThymeleaf th:href 调用 post 和 get
【发布时间】:2014-12-07 05:24:19
【问题描述】:

this guide 之后,我配置了一个控制器来处理一个非常简单的表单的显示和验证。我正在使用 Thymeleaf th:href 链接到表单。

<a th:href="@{/event/create}">Create Event</a>

问题是 showForm() 和 checkEventForm() 在单击链接时一个接一个地被调用。结果是从表单快速重定向回 / 因为 checkEventForm() 也被处理。

GET 和 POST 是否都需要处理?

链接:

<div id="sidebar">
    <ol>
      <li><a th:href="@{/}">Join Event</a></li>
      <li><a th:href="@{/event/create}">Create Event</a></li>
    </ol>   
</div>  

MvcConfig :

@Configuration
public class MvcConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/home").setViewName("home");
        registry.addViewController("/").setViewName("home");
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry
            .addResourceHandler("/resources/**")
            .addResourceLocations("/resources/")
            .setCachePeriod(31556926);
    }

}

创建事件控制器:

@Controller
public class CreateEventController {

    @RequestMapping(value="/event/create", method=RequestMethod.GET)
    public String showForm(CreateEvent event) {
        return "createEvent";
    }

    @RequestMapping(value="/event/create", method=RequestMethod.POST)
    public String checkEventForm(@Valid CreateEvent event, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            return "createEvent";
        }
        return "redirect:/";
    }

}

【问题讨论】:

  • th:href="@{/event/create}" 的链接应该只调用showForm 方法而不是其他方法。您确定没有其他相关代码吗?
  • @geoand,感谢您的回复!我添加了更多信息..
  • 你能分享这个项目吗?我看不到任何明显的东西可以指出为什么两者都被调用
  • @geoand,经过一番挖掘,我的程序根本没有用!它实际上是一个 chrome 扩展(lastpass),它有一个自动保存的 localhost 密码,它正在尝试自动登录并提交表单!谈论让自己发疯......谢谢!

标签: spring spring-mvc spring-boot thymeleaf


【解决方案1】:

经过一番挖掘,结果证明我的程序根本没有!它实际上是一个 chrome 扩展(lastpass),它有一个自动保存的 localhost 密码,它正在尝试自动登录并提交表单!谈论让自己发疯!

【讨论】:

    猜你喜欢
    • 2023-03-31
    • 2016-02-18
    • 2018-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多