【问题标题】:Form navigation confusion in SpringSpring中的表单导航混乱
【发布时间】:2025-11-30 19:05:01
【问题描述】:

我正在做一个 Spring 项目

从控制器 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 中的表单标签中没有给出任何操作,但在提交时流程返回到 处理提交方法。谁能告诉我为什么?

【问题讨论】:

  • 请同时添加您的@Controller 类声明。

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


【解决方案1】:

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

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

然后提交 &lt;form&gt; 会将请求发送到相同的 URL。


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

【讨论】: