gpt4 book ai didi

mysql - Spring 与 hibernate Post 不工作

转载 作者:行者123 更新时间:2023-11-29 19:23:52 25 4
gpt4 key购买 nike

我花了两天没有这个代码,但我仍然找不到问题出在哪里。

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

我的代码如下,我做错了什么?

Controller

@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;

是我的 Controller 有问题还是 View 有问题?

最佳答案

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

尝试添加这个

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

关于mysql - Spring 与 hibernate Post 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42283145/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com