Spring with hibernate Post not working

时间:2017-02-16 19:39:48

标签: mysql spring hibernate spring-mvc

我花了2天没有这个代码,我仍然无法找到问题所在。

我想将表单提交到数据库中,当我提交表单时,它不会显示任何错误但它不会提交。

我的代码低于我做错了什么?

控制器

@RequestMapping(value = { "/newcustomer" }, method = RequestMethod.GET)
    public String newcustomer(ModelMap model) throws Exception {
        Customers customers = new Customers();
        model.addAttribute("customerForm", customers);
        model.addAttribute("editt", false);
        model.addAttribute("newcustomer", getPrincipal());
        return "newcustomer";
    }

    @RequestMapping(value = { "/newcustomer" }, method = RequestMethod.POST)
    //public String saveCustomers(@Valid Customers customers, BindingResult result, ModelMap model) 
    public String saveCustomers(@ModelAttribute("customerForm") Customers customers, BindingResult result, ModelMap model){

        if (result.hasErrors()) {
            return "newcustomer";
        }

        if (!customersService.isCustomersPhoneUnique(customers.getIdCustomers(), customers.getCustomerPhone())) {
            FieldError CustomerPhoneError = new FieldError("customers", "CustomerPhone", messageSource
                    .getMessage("non.unique.CustomerPhone", new String[] {customers.getCustomerPhone()  }, Locale.getDefault()));
            result.addError(CustomerPhoneError);
            return "newcustomer";
    }

我的观点

<form:form method="POST" modelAttribute="customerForm"
                                    class="form-horizontal well">
                                    <fieldset id="newcustomer">
                                        <form:input type="hidden" path="idCustomers" id="id" />

                                        <div class="control-group">
                                            <label class="control-label" for="customerName">Customer
                                                Name</label>
                                            <div class="controls">
                                                <form:input type="text" path="CustomerName"
                                                    id="customerName" class="form-control span5" />
                                                <div class="has-error">
                                                    <form:errors path="CustomerName" class="help-inline" />
                                                </div>
                                            </div>
                                        </div>

                                        <div class="control-group">
                                            <label class="control-label" for="company">Company</label>
                                            <div class="controls">
                                                <form:input type="text" path="Company" id="company"
                                                    class="form-control span5" />
                                                <div class="has-error">
                                                    <form:errors path="Company" class="help-inline" />
                                                </div>
                                            </div>
                                        </div>

                                        <div class="control-group">
                                            <label class="control-label" for="phone">Cell Phone</label>
                                            <div class="controls">
                                                <form:input type="text" path="CustomerPhone" id="phone"
                                                    class="form-control span5" />
                                                <div class="has-error">
                                                    <form:errors path="CustomerPhone" class="help-inline" />
                                                </div>
                                            </div>
                                        </div>

                                        <div class="control-group">
                                            <label class="control-label" for="email">Email
                                                Address:</label>
                                            <div class="controls">
                                                <form:input type="text" path="CustomerEmail" id="email"
                                                    class="form-control span5" />
                                                <div class="has-error">
                                                    <form:errors path="CustomerEmail" class="help-inline" />
                                                </div>
                                            </div>
                                        </div>

                                        <div class="control-group ">
                                            <label class="control-label" for="name">Customer
                                                UserName:</label>
                                            <div class="controls">
                                                <form:input id="name" path="CustomerLoginName" type="text"
                                                    class="form-control span3" />
                                                <div class="has-error">
                                                    <form:errors path="CustomerLoginName" class="help-inline" />
                                                </div>
                                            </div>
                                        </div>

                                        <div class="control-group ">
                                            <label class="control-label" for="asset">Asset/Device
                                                Make/Model:</label>
                                            <div class="controls">
                                                <form:input id="asset" path="DeviceModel" type="text"
                                                    class="form-control span3" />
                                                <div class="has-error">
                                                    <form:errors path="DeviceModel" class="help-inline" />
                                                </div>
                                            </div>
                                        </div>

                                        <div class="input-append date" id="datepicker"
                                            data-date="12-02-2012" data-date-format="dd-mm-yyyy">
                                            <div class="control-group">
                                                <label class="control-label" for="date"> Scheduled
                                                    Date/Time:</label>
                                                <div class="controls">
                                                    <div class="input-append">
                                                        <form:input id="date" path="PickupDate" type="text"
                                                            class="form-control span6" />
                                                        <span class="add-on margin-fix"><i class="icon-th"></i></span>
                                                        <div class="has-error">
                                                            <form:errors path="PickupDate" class="help-inline" />
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>

                                        <div class="control-group">
                                            <label class="control-label" for="note">Notes</label>
                                            <div class="controls">
                                                <form:textarea id="note" path="Note" rows="5" />

                                            </div>
                                            <div class="has-error">
                                                <form:errors path="Note" class="help-inline" />
                                            </div>
                                        </div>

                                        <div class="control-group">
                                            <label class="control-label" for="dop">Description of
                                                Problem Work to be Performed:</label>
                                            <div class="controls">
                                                <form:textarea id="dop" path="DescriptionofProblem" rows="7" />

                                            </div>
                                            <div class="has-error">
                                                <form:errors path="DescriptionofProblem" class="help-inline" />
                                            </div>
                                        </div>

<form:input type="hidden" path="DateCreated" id="idd" />
                                        <div class="control-group">
                                            <div class="controls">
                                                <h1>Other Asset/Device Info</h1>
                                            </div>
                                        </div>
                                        <div class="form-actions">
                                            <c:choose>
                                                <c:when test="${editt}">
                                                    <input type="submit" value="Update"
                                                        class="btn btn-primary " /> or <a
                                                        href="<c:url value='/customlist' />">Cancel</a>
                                                </c:when>
                                                <c:otherwise>
                                                    <input type="submit" value="Add" class="btn btn-primary" /> or <a
                                                        href="<c:url value='/customlist' />">Cancel</a>
                                                </c:otherwise>

                                            </c:choose>
                                        </div>
                                    </fieldset>
                                </form:form>

我的模特:

@Entity
@Table(name = "Customers")
public class Customers implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = -1136348339164417585L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer idCustomers;

    @Size(min = 3, max = 30)
    @Column(name = "CUSTOMERNAME", nullable = false)
    private String CustomerName;

    @NotEmpty
    @Column(name = "COMPANY", nullable = false)
    private String Company;

    @NotEmpty
    @Column(name = "CUSTOMERPHONE", nullable = false)
    private String CustomerPhone;

    @NotEmpty
    @Column(name = "DEVICEMODEL", nullable = false)
    private String DeviceModel;

    @NotEmpty
    @Email
    @Column(name = "CUSTOMEREMAIL", nullable = false)
    private String CustomerEmail;

    @DateTimeFormat(pattern = "dd/MM/yyyy HH:mm")
    @Future
    @NotNull
    @Column(name = "PICKUPDATE", nullable = false)
    private Date PickupDate;

    @NotEmpty
    @Column(name = "CUSTOMERLOGINNAME", nullable = false)
    private String CustomerLoginName;

    @NotEmpty
    @Column(name = "NOTE", length = 255)
    private String Note;

    @Column(name = "DESCRIPTIONOFPROBLEM", length = 255)
    private String DescriptionofProblem;

    @Temporal(TemporalType.TIMESTAMP)
    @Generated(GenerationTime.INSERT)   
    @Column(name = "DATECREATED", insertable=false)
    private Date DateCreated;

我的控制器是问题还是视图?

1 个答案:

答案 0 :(得分:0)

在您的表单中,您没有定义 操作 属性

尝试添加此

FutureT