Reputation: 17
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name="addresses")
public class Address {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long add_id;
@Column
private String address;
@Column
private String postalcode;
@Column
private String city;
@Column
private String state;
@Column
private String country;
public Long getAdd_id() {
return add_id;
}
public void setAdd_id(Long add_id) {
this.add_id = add_id;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getPostalcode() {
return postalcode;
}
public void setPostalcode(String postalcode) {
this.postalcode = postalcode;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
}
HTML Page :
<form
method="post"
role="form"
th:action="@{/address/save}"
th:object="${address}"
>
<input type="hidden" th:field="*{add_id}" />
<div class="form-group mb-3">
<label class="form-label">Address</label>
<input
class="form-control"
id="address"
name="address"
placeholder="Enter Address"
th:field="*{address}"
type="text"
/>
<p th:errors = "*{address}" class="text-danger"
th:if="${#fields.hasErrors('address')}"></p>
</div>
<div class="form-group mb-3">
<label class="form-label">Postal Code</label>
<input
class="form-control"
id="postalcode"
name="postalcode"
placeholder="Enter Postal Code"
th:field="*{postalcode}"
type="text"
/>
<p th:errors = "*{postalcode}" class="text-danger"
th:if="${#fields.hasErrors('postalcode')}"></p>
</div>
<div class="form-group mb-3">
<label class="form-label">City</label>
<input
class="form-control"
id="city"
name="city"
placeholder="Enter City"
th:field="*{city}"
type="text"
/>
<p th:errors = "*{city}" class="text-danger"
th:if="${#fields.hasErrors('city')}"></p>
</div>
<div class="form-group mb-3">
<label class="form-label">State</label>
<input
class="form-control"
id="state"
name="state"
placeholder="Enter State"
th:field="*{state}"
type="text"
/>
<p th:errors = "*{state}" class="text-danger"
th:if="${#fields.hasErrors('state')}"></p>
</div>
<div class="form-group mb-3">
<label class="form-label">Country</label>
<input
class="form-control"
id="country"
name="country"
placeholder="Enter Country"
th:field="*{country}"
type="text"
/>
<p th:errors = "*{country}" class="text-danger"
th:if="${#fields.hasErrors('country')}"></p>
</div>
<div class="form-group" style="text-align:center">
<button class="btn btn-success" type="submit">Save</button>
</div>
</form>
Controller:
@Autowired
private AddressRepository addressRepository;
@GetMapping("/address/{id}")
public String addAddress(@PathVariable("id") Long add_id, Model model){
System.out.println("\nInside addressform :\n");
@SuppressWarnings("deprecation")
Address address = addressRepository.getById(add_id);
System.out.println(address.getAdd_id());
System.out.println("add_id : "+add_id);
model.addAttribute("address", address);
return "addressform";
}
// handler method to handle post request to save address
@PostMapping("/address/save")
public String saveAddress(@Valid @ModelAttribute("address") Address address) {
System.out.println("\nInside address save method.");
System.out.println(address.getAdd_id());
System.out.println(address.getAddress());
System.out.println(address.getPostalcode());
System.out.println(address.toString());
addressRepository.save(address);
return "redirect:/users";
}
ERROR :
There was an unexpected error (type=Bad Request, status=400).
Failed to convert value of type 'java.lang.String' to required type 'example.entity.Address'; Failed to convert from type [java.lang.String] to type [java.lang.Long] for value 'madhapur' org.springframework.beans.TypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'example.entity.Address'; Failed to convert from type [java.lang.String] to type [java.lang.Long] for value 'madhapur' at org.springframework.beans.TypeConverterSupport.convertIfNecessary(TypeConverterSupport.java:79) at org.springframework.beans.TypeConverterSupport.convertIfNecessary(TypeConverterSupport.java:53) at org.springframework.validation.DataBinder.convertIfNecessary(DataBinder.java:729) at org.springframework.web.servlet.mvc.method.annotation.ServletModelAttributeMethodProcessor.createAttributeFromRequestValue(ServletModelAttributeMethodProcessor.java:142) at org.springframework.web.servlet.mvc.method.annotation.ServletModelAttributeMethodProcessor.createAttribute(ServletModelAttributeMethodProcessor.java:78) at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.resolveArgument(ModelAttributeMethodProcessor.java:147) at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:122) at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:181) at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:148) at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:117) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:884) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:797) at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
Upvotes: 0
Views: 1285
Reputation: 71
Firstly I figured out couple of codes that can cause serious performance issues and is not advised.
Here's a few:
Take off your Generated Getters and Setters and Leave the annotation @Getter
and @Setter
to do the work.
Take off <input type="hidden" th:field="*{add_id}" />
as it will be auto-generated from your entity Address private Long add_id
Take off address.toString()
from your saveAddress
controller as this is not needed to parse data to your Entity, your entity receives String already which will be parsed from your form, there is no need to convert with toString
again.
One of your error states Failed to convert from type [java.lang.String] to type [java.lang.Long]
which simply means you're trying to convert Long to String which should be auto generated by your Entity
I hope this helps!!!
Upvotes: 1