Coder Social home page Coder Social logo

Comments (5)

ankitkeer avatar ankitkeer commented on June 24, 2024

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.

from springmvcstepbystep.

sjaswinder82 avatar sjaswinder82 commented on June 24, 2024

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. }

from springmvcstepbystep.

in28minutes avatar in28minutes commented on June 24, 2024

Awesome. Do you want to submit a pull request?

from springmvcstepbystep.

sonhandsome06 avatar sonhandsome06 commented on June 24, 2024

from springmvcstepbystep.

sonhandsome06 avatar sonhandsome06 commented on June 24, 2024

from springmvcstepbystep.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.