I have spent 2 days no this code, and i can not still find where the problem is.
I want to submit a form, into a database, when i submit my form it will not show any error yet it will not submit.
My code is below what am i doing wrong?
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";
}
My View
<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>
My Model :
@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;
Is my Controller the Problem or the View?
In your form you didnt define the action attribute
Try to add this
<form:form method="POST" modelAttribute="customerForm" action="/newcustomer" class="form-horizontal well">
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.