在Spring中形成导航混乱

时间:2014-01-06 16:21:50

标签: java spring jsp java-ee spring-mvc

我正在开发一个Spring项目

从Controller 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方法。谁能告诉我为什么?

1 个答案:

答案 0 :(得分:1)

如果您提交的form没有action属性,则浏览器通常会向当前网址发出请求。例如,如果您之前已向

发出GET请求
www.yourhost.com/your-app/owners/new

然后提交<form>会将请求发送到同一网址。


我不确定这是在规范中还是只是惯例。