在Spring Boot项目中,我制作了一个窗体视图,并使用了该视图的两个控制器函数,它们的每个方法都是GET和POST。但是,如果我提交表单,则它不会进入POST方法功能。我应该怎么做才能使该功能正常工作?
这使用Spring Boot(Maven),freemarker和MySQL。
这是控制器的一部分:
@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");
Member member = new Member();
mv.addObject("member", member);
return mv;
}
@RequestMapping(value = "/join", method = RequestMethod.POST)
public ModelAndView create(@ModelAttribute("member") Member member, ModelAndView mv) {
System.out.println("bbbb");
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("error", e.getMessage());
return mv;
}
mv.setViewName("home");
mv.addObject("view", "home");
mv.addObject("member", member);
return mv;
}
这是form.ftl的一部分:
<form class="mdl-card mdl-shadow-2dp mdl-cell--12-col" method="post" action="${action}">
<#include "content/form/${view}.ftl">
</form>
这是createMember.ftl:
<div class="mdl-card__supporting-text">
<div class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label">
<input class="mdl-textfield__input" type="text" id="nameField" name="name">
<label class="mdl-textfield__label" for="nameField">Name</label>
</div>
<div class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label">
<input class="mdl-textfield__input" type="text" id="idField" name="id">
<label class="mdl-textfield__label" for="idField">ID (6 - 15 letters english/number)</label>
</div>
<div class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label">
<input class="mdl-textfield__input" type="password" id="passwordField" name="password">
<label class="mdl-textfield__label" for="passwordField">Password (6 - 15 letters english/number/symbols)</label>
</div>
<#if error??>
<p>${error}</p>
</#if>
</div>
我使create()函数在启动时打印“ bbbb”,但未打印“ bbbb”。因此,我想它甚至没有进入该功能。我应该怎么做才能使其正常工作?