(Spring Boot)<form>提交后不会转到“操作”网址

时间:2019-08-03 08:02:57

标签: javascript html spring spring-boot freemarker

我做了一个<form>的post方法。运行项目时,单击提交按钮时,URL不会进入后期控制器中。为了找出问题所在,我将<form>标记的'action'属性值更改为现有页面的url。即使这样,它也不会转到该页面。所以我认为问题是提交事件不会发生。为什么会发生这种情况,我应该怎么做才能使其起作用?

HTML代码:

<div class="mdl-grid">
    <form class="mdl-card mdl-shadow-2dp mdl-cell--12-col" method="post" action="${action}" role="form">
        <#include "content/form/${view}.ftl">
    </form>
    <div class="mdl-cell--12-col mdl-grid btn-wrapper">
        <button class="mdl-button mdl-js-button mdl-button--raised mdl-cell--12-col" type="submit">
            Submit
        </button>
    </div>
</div>

控制器:

@RequestMapping(value = "/join", method = RequestMethod.GET)
    public ModelAndView join() {
        ModelAndView mv = new ModelAndView("form");
        mv.addObject("layout", "form");
        mv.addObject("view", "createMember");
        mv.addObject("viewmsg", "Sign In");
        mv.addObject("action", "/member/join");
        mv.addObject("modelName", "member");
        Member member = new Member();
        mv.addObject("member", member);
        return mv;
    }

    @RequestMapping(value = "/join", method = RequestMethod.POST)
    public ModelAndView create(@RequestParam("name") String name, @RequestParam("id") String id, @RequestParam("password") String password) {
        logger.debug("aaaa");
        Member member = new Member();
        member.setId(id);
        member.setName(name);
        member.setPassword(password);
        ModelAndView mv = new ModelAndView();

        try {
            memberService.create(member);
        }
        catch (Exception e) {
            mv.addObject("layout", "form");
            mv.addObject("view", "createMember");
            mv.addObject("viewmsg", "Sign In");
            mv.addObject("action", "/member/join");
            mv.addObject("member", member);
            mv.addObject("modelName", "member");
            mv.addObject("error", e.getMessage());

            return mv;
        }

        mv.setViewName("home");
        mv.addObject("view", "home");
        mv.addObject("member", member);
        return mv;
    }

1 个答案:

答案 0 :(得分:0)

验证您的操作值,以仔细检查目标端点。其次,将submit按钮移到form内。