【问题标题】:Spring with hibernate Post not working带有休眠帖子的春天不起作用
【发布时间】:2017-02-16 19:39:47
【问题描述】:

我已经花了 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;

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

【问题讨论】:

  • 您可以在表单域中输入任何文本吗?我试过你的代码,我什至不能在文本输入区域内点击
  • @Ivan 你有什么例外吗?请求完成
  • @cralfaro 不,我没有得到任何异常
  • @IvanPaitoo 你在表单中添加了 action 属性吗?
  • @cralfaro 是的,我做到了

标签: mysql spring hibernate spring-mvc


【解决方案1】:

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

尝试添加这个

<form:form method="POST" modelAttribute="customerForm" action="/newcustomer" class="form-horizontal well">

【讨论】:

  • 我在添加操作时收到 HTTP 状态 404 - /newcustomer
  • @IvanPaitoo 您能否检查网络选项并检查调用 POST 操作的 URL 是否正确?
猜你喜欢
  • 2011-09-23
  • 2013-01-29
  • 2014-10-09
  • 2010-10-06
  • 2011-07-13
  • 1970-01-01
  • 1970-01-01
  • 2013-04-01
  • 1970-01-01
相关资源
最近更新 更多