2014-01-06 69 views
0

我工作的一个春天项目表导航混乱春天

从控制器AddForm.java的请求转发到Form.jsp &的onsubmit从那里回来到同一控制器。

代码下面

Form.jsp

<%@ include file="/WEB-INF/view/include.jsp" %> 
<%@ include file="/WEB-INF/view/header.jsp" %> 
<c:choose> 
    <c:when test="${action eq 'addowner'}"><c:set var="method" value="post"/></c:when> 
    <c:otherwise><c:set var="method" value="put"/></c:otherwise> 
</c:choose> 

<h2><c:if test="${action eq 'addowner'}">New </c:if>Owner:</h2> 
<form:form modelAttribute="owner" method="${method}"> 
    <table> 
    <tr> 
     <th> 
     First Name: 
     <br/> 
     <form:input path="firstName" size="30" maxlength="80"/> 
     </th> 
    </tr> 
    <tr> 
     <th> 
     Last Name: 
     <br/> 
     <form:input path="lastName" size="30" maxlength="80"/> 
     </th> 
    </tr> 
    <tr> 
     <th> 
     Address: 
     <br/> 
     <form:input path="address" size="30" maxlength="80"/> 
     </th> 
    </tr> 
    <tr> 
     <th> 
     City: 
     <br/> 
     <form:input path="city" size="30" maxlength="80"/> 
     </th> 
    </tr> 
    <tr> 
     <th> 
     Telephone: 
     <br/> 
     <form:input path="telephone" size="20" maxlength="20"/> 
     </th> 
    </tr> 
    <tr> 
     <td> 
     <c:choose> 
      <c:when test="${action eq 'addowner'}"> 
      <p class="submit"><input type="submit" value="Add Owner"/></p> 
      </c:when> 
      <c:otherwise> 
      <p class="submit"><input type="submit" value="Update Owner"/></p> 
      </c:otherwise> 
     </c:choose> 
     </td> 
    </tr> 
    </table> 
</form:form> 

<%@ include file="/WEB-INF/view/footer.jsp" %> 

AddFormController代码

@Controller 
@RequestMapping("/owners/new") 
@SessionAttributes(types = Owner.class) 
public class AddOwnerForm { 

    private final Clinic clinic; 


    @Autowired 
    public AddOwnerForm(Clinic clinic) { 
     this.clinic = clinic; 
    } 

    @InitBinder 
    public void setAllowedFields(WebDataBinder dataBinder) { 
     dataBinder.setDisallowedFields("id"); 
    } 

    @RequestMapping(method = RequestMethod.GET) 
    public String setupForm(Model model) { 
     Owner owner = new Owner(); 
     model.addAttribute(owner); 
     model.addAttribute("action", "addowner"); 
     return "owners/form"; 
    } 

    @RequestMapping(method = RequestMethod.POST) 
    public String processSubmit(@ModelAttribute Owner owner, SessionStatus status) { 

      System.out.println(owner.toString()); 
      System.out.println("Inside AddOwner processSubmit method"); 
      this.clinic.storeOwner(owner); 
      status.setComplete(); 
      return "redirect:/forms/owners/" + owner.getId(); 

    } 

} 

问题是在控制器中的setupForm方法转发的形式流动到JSP但在JSP标记不通过动作,但提交流程返回到 processSubmit方法。谁能告诉我为什么?

+0

请同时添加你的'@ Controller'类声明。 –

回答

1

如果您提交的form没有action属性,则浏览器通常会向当前URL发出请求。例如,如果您之前做了一个GET请求

www.yourhost.com/your-app/owners/new 

然后提交<form>将请求发送到相同的URL。


我不确定这是规范还是公约。