JasperException: java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'user' available as request attribute - spring

I have two Data Model : User and Car, User can have many cars , so this is the User Class :
#Entity
#Table(name="APP_USER")
public class User implements Serializable{
#Id #GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;
......
#OneToMany(mappedBy="user",cascade=CascadeType.ALL)
private Set<Car> cars = new HashSet<Car>();
Car.java :
#Entity
public class Car implements Serializable {
#Id
#GeneratedValue(strategy=GenerationType.IDENTITY)
private int id ;
.....
#ManyToOne(optional=false)
#JoinColumn(name="user_fk")
private User user;
In the carController , I have the method to add a new Car for one user :
#Controller
#RequestMapping("/cars")
public class CarController {
#Autowired
CarService carService;
#Autowired
UserService userService;
#RequestMapping(value={"/list"},method=RequestMethod.GET)
public String listCars(ModelMap model){
List<Car> cars = carService.allCars();
model.addAttribute("cars", cars);
return "car"; }
#ModelAttribute("utilisateurs")
public List<User> allUsers(){
List<User> utilisateurs = userService.findAllUsers();
return utilisateurs;
}
#RequestMapping(value={"/newCar"},method=RequestMethod.GET)
public String newCar(ModelMap model){
Car car = new Car();
model.addAttribute("car",car);
return "carRegistration";
}
#RequestMapping(value={"newCar"},method=RequestMethod.POST)
public String newCar(#Valid Car car, BindingResult result,ModelMap model){
if (result.hasErrors()) {
return "registration";
}
carService.save(car);
model.addAttribute("car",car);
return "redirect:/cars/list";
}
Finally , in the view , the form is :
<form:form method="POST" modelAttribute="car"
class="form-horizontal">
<form:input type="hidden" path="id" id="id" />
<div class="control-group">
<label class="control-label">Libelle</label>
<div class="controls">
<form:input type="text" path="libelle" id="libelle" style="height:4%" />
</div>
</div>
<div class="control-group">
<label class="control-label">Registration</label>
<div class="controls">
<form:input type="text" path="registration" id="email" />
</div>
</div>
<div class="control-group">
<label class="control-label">Utilisateur</label>
<div class="controls">
<form:select path="user" items="${utilisateurs}" itemValue="id" itemLabel="lastName" style="margin-left: 4%;"></form:select>
</div>
</div>
<div class="form-actions">
<input type="submit" value="Validate" class="btn btn-success" />
</div>
</form:form>
I can get the view , but when I click on button submit , I get this error :
Neither BindingResult nor plain target object for bean name 'user' available as request attribute.
I think there is a problem with the select item of users !!

Have you provided any Spring converter or formatter for User ?
applicationContext.xml:
<bean id="conversionService"
class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<list>
<bean class="my.project.StringToUser"/>
<bean class="my.project.StringToRole"/>
</list>
</property>
</bean>
<mvc:annotation-driven conversion-service="conversionService" />
It will convert User id to User object.
my.project.StringToUser:
import org.springframework.core.convert.converter.Converter;
public class StringToUser implements Converter<String, JUser> {
#Autowired
private UserService userService;
#Override
public JUser convert(String source) {
long id = Long.valueOf(source);
return userService.get(id);
}
}
the error :
WARNING: Failed to bind request element: org.springframework.beans.TypeMismatchException:
Failed to convert value of type [com.websystique.springmvc.model.User] to required type [com.websystique.springmvc.model.User];
nested exception is org.springframework.core.convert.ConversionFailedException:
Failed to convert from type [com.websystique.springmvc.model.User] to
type [#org.springframework.web.bind.annotation.ModelAttribute #javax.validation.Valid com.websystique.springmvc.model.User]
for value 'User [id=null, ssoId=alaa, password=alaa1991, firstName=, lastName=, email=, userProfiles=null, accounts=null, userDocuments=[], cars=[], documents=[]]';
nested exception is java.lang.ClassCastException:
com.websystique.springmvc.model.User cannot be cast to java.lang.String

Related

Spring tags form do not show validation errors in jsp

I am writing a page with user registration. Faced a display problem in the form of validation errors
My Controller:
#Controller
public class UIController {
private final Logger log = LoggerFactory.getLogger(getClass());
#Autowired
private UserService service;
#Autowired
private SecurityService securityService;
#Autowired
private UserValidator validator;
#PostMapping(value = "/login")
public String signIn(#ModelAttribute("userForm") UserTo userForm, BindingResult bindingResult, Model model) {
validator.validate(userForm, bindingResult);
User user = service.findByLogin(UserUtil.createNewFromTo(userForm).getLogin());
if (!userForm.getPassword().equals(user.getPassword())) {
log.info("invalid password {}", user);
return "redirect:/login";
}
log.info("signIn {}", user);
securityService.autologin(user.getLogin(), user.getPassword());
return "redirect:/welcome";
}
}
My Validator:
#Component
public class UserValidator implements Validator {
#Autowired
private UserRepositoryImpl userRepository;
#Override
public boolean supports(Class<?> aClass) {
return UserTo.class.equals(aClass);
}
#Override
public void validate(Object o, Errors errors) {
UserTo user = (UserTo) o;
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "login", "NotEmpty");
if (user.getLogin().length() < 6 || user.getLogin().length() > 32) {
errors.rejectValue("login", "Size.userForm.login");
}
if (userRepository.findByLogin(user.getLogin()) != null) {
errors.rejectValue("login", "Duplicate.userForm.login");
}
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "password", "NotEmpty");
if (user.getPassword().length() < 8 || user.getPassword().length() > 32) {
errors.rejectValue("password", "Size.userForm.password");
}
}
}
My jsp form :
<div id="wrapper">
<!--SLIDE-IN ICONS-->
<div class="user-icon"></div>
<div class="pass-icon"></div>
<!--END SLIDE-IN ICONS-->
<!--LOGIN FORM-->
<form:form name="login-form" modelAttribute="userForm" class="login-form" method="post" id="form">
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
<!--CONTENT-->
<div class="content">
<!--USERNAME-->
<spring:bind path="login">
<div class="form-group ${status.error ? 'has-error' : ''}">
<form:input name="login" id="login" type="text" path="login" class="input username"
placeholder="Login"></form:input>
<form:errors path="login"></form:errors>
</div>
</spring:bind>
<!--END USERNAME-->
</div>
<!--END CONTENT-->
<!--FOOTER-->
<div class="footer">
<button class="button" type="submit">Login</button>
<button class="register" type="button" name="submit" onclick=" register_url('${contextPath}/registration')">
Register
</button>
</div>
<!--END FOOTER-->
</form:form>
</div>
When debugging in chrome when sending a POST request, a 302 HTTP error appears.
Accordingly, if I set a breakpoint in the controller, then the debug is not processed.
Tell me what could be the problem?
The "form" tag in your jsp does not have "action" attribute.
You should not redirect on errors, because the redirect will erase the binding result. Instead do :
if (!userForm.getPassword().equals(user.getPassword())) {
log.info("invalid password {}", user);
return "login";
}

hibernate validation not working while one-to-one mapping in between two entities

During form submission, if there is any validation error then form shows the errors messages under the fields. But the actual problem is in another place. I have two entities User and UserDetails. These entities are mapped with each other by bidirectional one-to-one mapping. Validation is working only with the User entity fields but not with the UserDetails entity.
Spring - 5.0.2`
Hibernate - 5.2.10
User.java
#Entity
#Table(name="users")
public class User {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
#NotEmpty(message="{common.error.msg}")
private String first_name;
#NotEmpty(message="{common.error.msg}")
private String last_name;
#NotEmpty(message="{common.error.msg}")
private String status;
#OneToOne(cascade = CascadeType.ALL, mappedBy = "user", fetch = FetchType.LAZY)
private UserDetails userDetails;
//Getter and setter methods
}
UserDetails.java
#Entity
#Table(name="user_details")
public class UserDetails {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private int user_id;
#NotEmpty(message="{common.error.msg}")
private String address;
#NotEmpty(message="{common.error.msg}")
private String mobile;
#OneToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "id")
private User user;
//Getter and setter methods
}
Get and Post methods in the controller class
#GetMapping(value="/create")
public String loadUserForm(Model model) {
model.addAttribute("command", new User());
return "backend/oms/user/form"; //JSP
}
#PostMapping(value="/create")
public String Save(#Valid #ModelAttribute("command") User user, BindingResult br, Model model, HttpSession session, RedirectAttributes ra) {
if(br.hasErrors()) {
return "backend/oms/user/form"; //JSP
} else {
try {
int id = userService.save(user);
if(id > 0) {
ra.addFlashAttribute("flash_msg", "ok|User added!");
return "redirect:/oms/user/edit/"+id;
} else {
return "backend/oms/user/create"; //JSP
}
} catch (ConstraintViolationException ex) {
model.addAttribute("err", "Something wrong! Please try again.");
return "backend/oms/user/form"; //JSP
}
}
}
messages.properties
common.error.msg=This field is required!
form.jsp
<form:form action="/oms/user/create" method="post" modelAttribute="command">
<label>First Name</label>
<form:input path="first_name" class="form-control" placeholder="First Name" value="" />
<form:errors cssClass="error" path="first_name" />
<label>Last Name</label>
<form:input path="last_name" class="form-control" placeholder="Last Name" value="" />
<form:errors cssClass="error" path="last_name" />
<label>Mobile</label>
<form:input path="userDetails.mobile" class="form-control" placeholder="Mobile" value="" />
<form:errors cssClass="error" path="userDetails.mobile" />
<label>Address</label>
<form:textarea path="UserDetails.address" class="form-control" placeholder="Address" value="" />
<form:errors cssClass="error" path="userDetails.address" />
<label>Status</label>
<form:select class="form-control" path="status">
<option value="">Choose...</option>
<option value="E">Enable</option>
<option value="D">Disable</option>
</form:select>
<form:errors path="status" cssClass="error"/>
<input type="submit" class="btn btn-primary" value="Save" />
</form>
Please see the screenshot
As you can see the image only fields from the User entity is validating but fields those are from the UserDetails are not validating. Please help.
It is solved now. #Valid annotation solved my problem. I need to put #Valid annotation before the entity variable declaration like below --
#OneToMany(mappedBy="majorHead", cascade = CascadeType.ALL)
#Valid
private List<MinorHead> minorHead = new ArrayList<MinorHead>();

Using thymeleaf to post form data to a Controller that uses #ModelAttribute (complex objects)

There is the Element class:
public class Element {
private Long id;
private Name name;
// Getters and Setters ...
}
And the Name class:
public class Name {
private String en;
private String fr;
private String de;
// Getters and Setters ...
}
There is a getElementsController:
#GetMapping("/elements/create")
public String getElementsCreate() {
return "private/new-element";
}
There is a NewElementController controller:
#PostMapping("/elements/create")
public String postElementsCreate(#ModelAttribute Element element) {
System.out.println(element)
return null;
}
There is a form that posts data to the NewElementController:
<form method="post" th:object="${element}" th:action="#{/elements/create}">
<input type="text" value="1" name="id" placeholder="Id"/>
// How should I make the input fields for:
element.name.en ?
element.name.fr ?
element.name.de ?
<button type="submit">Save element</button>
</form>
Setting the Id works, but I can not access the name field (it is an object)
I have tried with th:field="*{name}" and with th:field="*{name.en}", but it does not work in that way.
Try following:
<form method="post" th:object="${element}" th:action="#{/elements/create}">
<input type="text" name="id" th:value="*{id}" placeholder="Id"/>
<input type="text" name="name.en" th:value="*{name.en}" placeholder="Name (EN)"/>
<input type="text" name="name.fr" th:value="*{name.fr}" placeholder="Name (FR)"/>
<input type="text" name="name.de" th:value="*{name.de}" placeholder="Name (DE)"/>
<button type="submit">Save element</button>
</form>
Yor controller method for GET should be like this:
#GetMapping("/elements/create")
public String getElementsCreate(Model model) {
Element element = new Element();
Name name = new Name();
element.setName(name);
model.addAttribute("element", element);
return "private/new-element.html";
}

Spring Boot multiple controllers with same mapping

My problem is very similar with this one: Spring MVC Multiple Controllers with same #RequestMapping
I'm building simple Human Resources web application with Spring Boot. I have a list of jobs and individual url for each job:
localhost:8080/jobs/1
This page contains job posting details and a form which unauthenticated users -applicants, in this case- can use to apply this job. Authenticated users -HR Manager-, can see only posting details, not the form. I have trouble with validating form inputs.
What I tried first:
#Controller
public class ApplicationController {
private final AppService appService;
#Autowired
public ApplicationController(AppService appService) {
this.appService = appService;
}
#RequestMapping(value = "/jobs/{id}", method = RequestMethod.POST)
public String handleApplyForm(#PathVariable Long id, #Valid #ModelAttribute("form") ApplyForm form, BindingResult bindingResult) {
if (bindingResult.hasErrors()) {
return "job_detail"; //HTML page which contains job details and the application form
}
appService.apply(form, id);
return "redirect:/jobs";
}
#RequestMapping(value = "/applications/{id}", method = RequestMethod.GET)
public ModelAndView getApplicationPage(#PathVariable Long id) {
if (null == appService.getAppById(id)) {
throw new NoSuchElementException(String.format("Application=%s not found", id));
} else {
return new ModelAndView("application_detail", "app", appService.getAppById(id));
}
}
}
As you guess this didn't work because I couldn't get the models. So I put handleApplyForm() to JobController and changed a little bit:
#Controller
public class JobController {
private final JobService jobService;
private final AppService appService;
#Autowired
public JobController(JobService jobService, AppService appService) {
this.jobService = jobService;
this.appService = appService;
}
#RequestMapping(value = "/jobs/{id}", method = RequestMethod.POST)
public ModelAndView handleApplyForm(#PathVariable Long id, #Valid #ModelAttribute("form") ApplyForm form, BindingResult bindingResult) {
if (bindingResult.hasErrors()) {
return getJobPage(id);
}
appService.apply(form, id);
return new ModelAndView("redirect:/jobs");
}
#RequestMapping(value = "/jobs/{id}", method = RequestMethod.GET)
public ModelAndView getJobPage(#PathVariable Long id) {
Map<String, Object> model = new HashMap<String, Object>();
if (null == jobService.getJobById(id)) {
throw new NoSuchElementException(String.format("Job=%s not found", id));
} else {
model.put("job", jobService.getJobById(id));
model.put("form", new ApplyForm());
}
return new ModelAndView("job_detail", model);
}
}
With this way, validations works but I still can't get the same effect here as it refreshes the page so that all valid inputs disappear and error messages don't appear.
By the way, job_detail.html is like this:
<h1>Job Details</h1>
<p th:inline="text"><strong>Title:</strong> [[${job.title}]]</p>
<p th:inline="text"><strong>Description:</strong> [[${job.description}]]</p>
<p th:inline="text"><strong>Number of people to hire:</strong> [[${job.numPeopleToHire}]]</p>
<p th:inline="text"><strong>Last application date:</strong> [[${job.lastDate}]]</p>
<div sec:authorize="isAuthenticated()">
<form th:action="#{/jobs/} + ${job.id}" method="post">
<input type="submit" value="Delete this posting" name="delete" />
</form>
</div>
<div sec:authorize="isAnonymous()">
<h1>Application Form</h1>
<form action="#" th:action="#{/jobs/} + ${job.id}" method="post">
<div>
<label>First name</label>
<input type="text" name="firstName" th:value="${form.firstName}" />
<td th:if="${#fields.hasErrors('form.firstName')}" th:errors="${form.firstName}"></td>
</div>
<!-- and other input fields -->
<input type="submit" value="Submit" name="apply" /> <input type="reset" value="Reset" />
</form>
</div>
Check thymeleaf documentation here
Values for th:field attributes must be selection expressions (*{...}),
Also ApplyForm is exposed then you can catch it in the form.
Then your form should looks like this:
<form action="#" th:action="#{/jobs/} + ${job.id}" th:object="${applyForm}" method="post">
<div>
<label>First name</label>
<input type="text" name="firstName" th:value="*{firstName}" />
<td th:if="${#fields.hasErrors('firstName')}" th:errors="*{firstName}"></td>
</div>
<!-- and other input fields -->
<input type="submit" value="Submit" name="apply" /> <input type="reset" value="Reset" />
</form>

Spring , HIbernate need assistance

This is my index.jsp
<body>
<div class="container">
<div class="row">
<h1 class="text-center">Rupasinghe Trust Invesments</h1>
<div
class="col-lg-4 col-md-4 col-sm-8 col-xs-12 col-lg-offset-4 col-md-offset-4 col-sm-offset-2">
<div class="myForm">
<form:form class="form_signin" method="POST" commandName="user" action="login">
<%-- <form:input path="branch" type="text" class="form-control" name="branch"
placeholder="Branch Code" required="autofocus" /><br />
--%>
<form:input path="username"
type="text" class="form-control" name="username"
placeholder="Username" required="autofocus" /><br />
<form:input path="password"
type="password" class="form-control" name="password"
placeholder="Password" required="autofocus" /><br />
<input type="submit" value="Login" class="btn"/>
</form:form>
</div>
</div>
</div>
</div>
</body>
This is my LoginController.java
#Controller
public class LoginController {
#RequestMapping(value="/login" , method=RequestMethod.POST)
public String login(#ModelAttribute("user") User user , BindingResult result){
return "mainFrameAdminPanlel";
}
}
This is the bean User.java
#Entity
public class User {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
private int userId;
private String username;
private String password;
#ManyToOne
#JoinColumn(name="branchId")
private BranchEntity branch;
#OneToMany
private Set<UserAccess> userAccess;
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public BranchEntity getBranch() {
return branch;
}
public void setBranch(BranchEntity branch) {
this.branch = branch;
}
public Set<UserAccess> getUserAccess() {
return userAccess;
}
public void setUserAccess(Set<UserAccess> userAccess) {
this.userAccess = userAccess;
}
public static long getSerialversionuid() {
return serialVersionUID;
}
}
I am getting this error
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'user' available as request attribute
I am new to spring and still couldn't get what is wrong with this. Please help ! Thank you in advance
When you load the first page itself, I mean the page containing the form, you need to pass an instance of User. This exception is thrown since you have not passed instance of User that you are trying to use in the form. Kindly check.

Resources