Spring MVC:表单未提交

时间:2011-07-24 17:54:50

标签: model-view-controller spring form-submit

在我的Spring MVC应用程序中,表单控制器已正确连接并按预期显示表单。

但是“提交”按钮不起作用。此外,FormController的onSubmit()方法永远不会被调用,因为我的调试打印语句没有显示。

相反,当我单击“提交”时,表单会重新加载并且某些字段会发生变化(非常奇怪的行为)。

我像往常一样在Spring MVC中实现了我的FormController:

public class RegisterController extends SimpleFormController {

    public RegisterController()
    {
        setCommandClass(Customer.class);
        setCommandName("customer");
    }

    @Override
    protected ModelAndView onSubmit(Object command) throws Exception {
        // never gets run!
        Customer customer = (Customer) command;
        System.out.println("TEST. Obtained Customer object: " + customer.toString());
    }
}

Dispatcher Servlet的我的bean连接是:

<bean name="/register.htm" class="techbooks.web.RegisterController"
    p:formView="register" p:successView="home"/>

“register”是指register.htm(.jsp)和home.htm(.jsp)的“home”,两者都存在。

JSP:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>

<html>

<head>
<meta http-equiv="Content-Language" content="en-us" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>TechBooks.com</title>
<link rel="stylesheet" type="text/css" href="css/main.css" />
</head>

<body>

<center>

<table style="width: 850px" class="style2">
<form:form method="POST" commandName="customer">
    <tr>
    <td><h1>Register New Customer</h1></td>
    </tr>

    <tr>
        <td>
            <table>
                <tr>
                    <td align="left">Customer ID:</td>
                    <td align="left"><form:input path="customerId"/></td>
                </tr>
                <tr>
                    <td align="left">Customer Password:</td>
                    <td align="left"><form:password path="customerPassword"/></td>
                </tr>
                <tr>
                    <td align="left">Customer First Name:</td>
                    <td align="left"><form:input path="customerFirstName"/></td>
                </tr>
                <tr>
                    <td align="left">Customer Last Name:</td>
                    <td align="left"><form:input path="customerLastName"/></td>
                </tr>
                <tr>
                    <td align="left">Customer Address:</td>
                    <td align="left"><form:input path="customerAddress"/></td>
                </tr>
                <tr>
                    <td align="left">Customer City:</td>
                    <td align="left"><form:input path="customerCity"/></td>
                </tr>
                <tr>
                    <td align="left">Customer State:</td>
                    <td align="left"><form:input path="customerState"/></td>
                </tr>
                <tr>
                    <td align="left">Customer Zip Code:</td>
                    <td align="left"><form:input path="customerZipCode"/></td>
                </tr>
                <tr>
                    <td align="left">Customer Email Address:</td>
                    <td align="left"><form:input path="customerEmailAddress"/></td>
                </tr>
                <tr>
                    <td align="left">Date Added:</td>
                    <td align="left"><form:input path="customerDateAdded" /></td>
                </tr>
                <tr>
                    <td align="left">Is Admin?</td>
                    <td align="left"><form:checkbox path="customerIsAdmin"/></td>
                </tr>
                <tr>
                    <td align="left"><input type="submit" value="Register"/><br/><input type="button" value="Cancel"/>
                    </td>
                    <td align="left">&nbsp;</td>
                </tr>
            </table>

        </td>
    </tr>

</form:form>
</table>


</center>

</body>

</html>

客户bean类:

package techbooks.model;

import java.io.Serializable;
import java.util.Date;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

public class Customer implements Serializable {

    @Id
    private String customerId;
    private String customerPassword;
    private String customerFirstName;
    private String customerLastName;
    private String customerAddress;
    private String customerCity;
    private String customerState;
    private String customerZipCode;
    private String customerEmailAddress;
    private Date customerDateAdded;
    private Boolean customerIsAdmin;

    /**
     * @return the customerId
     */
    public String getCustomerId() {
        return customerId;
    }

    /**
     * @param customerId the customerId to set
     */
    public void setCustomerId(String customerId) {
        this.customerId = customerId;
    }

    /**
     * @return the customerPassword
     */
    public String getCustomerPassword() {
        return customerPassword;
    }

    /**
     * @param customerPassword the customerPassword to set
     */
    public void setCustomerPassword(String customerPassword) {
        this.customerPassword = customerPassword;
    }

    /**
     * @return the customerFirstName
     */
    public String getCustomerFirstName() {
        return customerFirstName;
    }

    /**
     * @param customerFirstName the customerFirstName to set
     */
    public void setCustomerFirstName(String customerFirstName) {
        this.customerFirstName = customerFirstName;
    }

    /**
     * @return the customerLastName
     */
    public String getCustomerLastName() {
        return customerLastName;
    }

    /**
     * @param customerLastName the customerLastName to set
     */
    public void setCustomerLastName(String customerLastName) {
        this.customerLastName = customerLastName;
    }

    /**
     * @return the customerAddress
     */
    public String getCustomerAddress() {
        return customerAddress;
    }

    /**
     * @param customerAddress the customerAddress to set
     */
    public void setCustomerAddress(String customerAddress) {
        this.customerAddress = customerAddress;
    }

    /**
     * @return the customerCity
     */
    public String getCustomerCity() {
        return customerCity;
    }

    /**
     * @param customerCity the customerCity to set
     */
    public void setCustomerCity(String customerCity) {
        this.customerCity = customerCity;
    }

    /**
     * @return the customerState
     */
    public String getCustomerState() {
        return customerState;
    }

    /**
     * @param customerState the customerState to set
     */
    public void setCustomerState(String customerState) {
        this.customerState = customerState;
    }

    /**
     * @return the customerZipCode
     */
    public String getCustomerZipCode() {
        return customerZipCode;
    }

    /**
     * @param customerZipCode the customerZipCode to set
     */
    public void setCustomerZipCode(String customerZipCode) {
        this.customerZipCode = customerZipCode;
    }

    /**
     * @return the customerEmailAddress
     */
    public String getCustomerEmailAddress() {
        return customerEmailAddress;
    }

    /**
     * @param customerEmailAddress the customerEmailAddress to set
     */
    public void setCustomerEmailAddress(String customerEmailAddress) {
        this.customerEmailAddress = customerEmailAddress;
    }

    /**
     * @return the customerDateAdded
     */
    public Date getCustomerDateAdded() {
        return customerDateAdded;
    }

    /**
     * @param customerDateAdded the customerDateAdded to set
     */
    public void setCustomerDateAdded(Date customerDateAdded) {
        this.customerDateAdded = customerDateAdded;
    }

    /**
     * @return the customerIsAdmin
     */
    public Boolean getCustomerIsAdmin() {
        return customerIsAdmin;
    }

    /**
     * @param customerIsAdmin the customerIsAdmin to set
     */
    public void setCustomerIsAdmin(Boolean customerIsAdmin) {
        this.customerIsAdmin = customerIsAdmin;
    }
}

正如我所说,表单永远不会被提交,并且Controller的onSubmit()永远不会执行。感谢您的任何想法

2 个答案:

答案 0 :(得分:1)

我认为可能存在一些绑定错误,所以我建议覆盖

protected ModelAndView processFormSubmission(HttpServletRequest request,
                                             HttpServletResponse response,
                                             Object command,
                                             BindException errors)
                                      throws Exception

并检查errors.hasErrors()是否返回false,以确保提交成功。

请参阅processFormSubmission

答案 1 :(得分:0)

除了上面接受的答案,我想指出一些事情 -

如果你已经被覆盖

  

protected Map referenceData(HttpServletRequest请求,对象命令,错误错误)

通常在GET请求上调用的控制器中的

方法即使在提交表单(POST)时也会被调用。这是因为如果有任何Binding错误形式将重新加载,再次调用referecenData()。当然你可以覆盖processFormSubmission()方法,但如果你只是想要覆盖onSubmit()方法我建议覆盖

  

protected ModelAndView onSubmit(HttpServletRequest请求,HttpServletResponse响应,Object命令,BindException错误)

方法并查看BindException错误。