Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Having trouble at validation #14

Closed
sonu0402 opened this issue Jul 7, 2017 · 5 comments
Closed

Having trouble at validation #14

sonu0402 opened this issue Jul 7, 2017 · 5 comments

Comments

@sonu0402
Copy link

sonu0402 commented Jul 7, 2017

Hello sir,
I am having trouble at step 27 while validating an add-a-new-todo request. When i enter more than 6 characters, the code executes normally. Although when i enter less than 6 chars, it returns;

java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'todo' available as request attribute.

Rest of the code flows normally. Here is the code of add controller.

@RequestMapping(value = "/add-todos", method = RequestMethod.GET)
public String showAddTodoPage(ModelMap model) {
model.addAttribute("todo", new ToDoBean());
return "add-todo";
}
@RequestMapping(value = "/add-todos", method = RequestMethod.POST)
public String addTodo(ModelMap model, @Valid ToDoBean todoBean,
BindingResult result) {

	if (result.hasErrors()) {
		return "add-todo";
	}
	String user = (String) model.get("name");
	todoService.addTodo(user, todoBean.getDesc(), new Date(), false);
	model.clear();// to prevent request parameter "name" to be passed
	return "redirect:/list-todos";
}

I have uploaded the code here. Thank you sir.

in28Minutes-first-webapp.zip

@sonu0402 sonu0402 closed this as completed Jul 7, 2017
@sonu0402 sonu0402 reopened this Jul 7, 2017
@ankitkeer
Copy link

ankitkeer commented Jan 14, 2018

You need to

  1. make sure commandName is same as Bean class name.
  2. when hasError is true set the ModelMap again as it is valid only for one request.

@sjaswinder82
Copy link

sjaswinder82 commented Jan 26, 2019

Hello sir,
I am having trouble at step 27 while validating an add-a-new-todo request. When i enter more than 6 characters, the code executes normally. Although when i enter less than 6 chars, it returns;

java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'todo' available as request attribute.

Rest of the code flows normally. Here is the code of add controller.

@RequestMapping(value = "/add-todos", method = RequestMethod.GET)
public String showAddTodoPage(ModelMap model) {
model.addAttribute("todo", new ToDoBean());
return "add-todo";
}
@RequestMapping(value = "/add-todos", method = RequestMethod.POST)
public String addTodo(ModelMap model, @Valid ToDoBean todoBean,
BindingResult result) {

	if (result.hasErrors()) {
		return "add-todo";
	}
	String user = (String) model.get("name");
	todoService.addTodo(user, todoBean.getDesc(), new Date(), false);
	model.clear();// to prevent request parameter "name" to be passed
	return "redirect:/list-todos";
}

I have uploaded the code here. Thank you sir.

in28Minutes-first-webapp.zip

Update method signature to:

public String addTodo(@Valid @ModelAttribute("todoBean") ToDoBean todoBean, BindingResult result) { //code goes here. }

@in28minutes
Copy link
Owner

Awesome. Do you want to submit a pull request?

@sonhandsome06
Copy link

sonhandsome06 commented Jan 27, 2019 via email

@sonhandsome06
Copy link

sonhandsome06 commented Jan 28, 2019 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants