简体   繁体   中英

Spring-MVC @ModelAttribute and @Autowired

I'm trying to initialize an Object with the @ModelAttribute Annontation on a Method. When calling the URL "/p/PPP/scope" strange things happen. The ProjectService seems not to be instantiated when calling the @ModelAttribute method, but it is there when calling the show() method. Has anybody an idea whats wrong in this?

Here are the log statements:

12:32:19 [DEBUG] ScopeController - getProject() - loading project for 'PPP'
12:32:19 [DEBUG] ScopeController - getProject() - projectService initialized? null
12:32:21 [DEBUG] ScopeController - show() - projectService initialized? ...project.ProjectService@20f2442e

and the source:

@Controller
@RequestMapping("/p/{abbr}/scope")
@SessionAttributes("project")
public class ScopeController {

    public static final String SHOW_PROJECT_PAGE = "/projects/scope/show";

    private static final Logger log = LoggerFactory.getLogger(ScopeController.class);

    @Autowired
    private ProjectService projectService;

    @ModelAttribute("project")
    private Project getProject(@PathVariable(value = "abbr") String abbr) {
        log.debug("getProject() - loading project for '{}'", abbr);
        log.debug("getProject() - projectService initialized? {}", projectService);
        // should call this method:
        // return projectService.find(abbr);
        return new Project();
    }

    @RequestMapping(method = RequestMethod.GET)
    @Transactional
    public String show() throws BindException {
        log.debug("show() - projectService initialized? {}", projectService);
        return SHOW_PROJECT_PAGE;
    }
}

All methods with ModelAttibute annotation must be be public.

So when method getProject is public - it will work properly:

 @ModelAttribute("project")
 public Project getProject( ...

Perhaps try a few things.

  1. Change the signature of:
    • private Project getProject to
    • public @ResponseBody Project
  2. Remove @Transactional from controller and move them to whatever service methods need them. (arguably better design practice - doubt it is causing you the problem you describe)
  3. Move @ModelAttribute("Project") annotation to the Project class
    • ie
      @ModelAttribute("Project") public Project get Project(){ return new Project(); }

So it would look something like:

@Controller
@RequestMapping("/p/{abbr}/scope")
@SessionAttributes("project")
public class ScopeController {

public static final String SHOW_PROJECT_PAGE = "/projects/scope/show";

private static final Logger log = LoggerFactory.getLogger(ScopeController.class);

@Autowired
private ProjectService projectService;

@RequestMapping(value = "/<yourUri for getProject>", method = RequestMethod.GET)
public @ResponseBody Project get(@PathVariable(value = "abbr") String abbr) {
    return getProject(abbr);
}

private Project getProject(String abbr) {
    log.debug("getProject() - loading project for '{}'", abbr);
    log.debug("getProject() - projectService initialized? {}", projectService);
    // should call this method:
    // return projectService.find(abbr);
    return new Project();
}

@RequestMapping(method = RequestMethod.GET)
@Transactional
public String show() throws BindException {
    log.debug("show() - projectService initialized? {}", projectService);
    return SHOW_PROJECT_PAGE;
}

}

//In your Project class 
@ModelAttribute("project")
public class Project {
//your class stuff
}

For one thing, I would put the @Transactional annotation in the Repository/data access layer as that is the norm of good layered Spring MVC-annotation based applications. In addition, your @PathVariable annotation is used to retrieve values passed in the URI after the controller's base URI. So having this annotation in a private helper method that does not intercept a URI pattern makes little sense.

So after playing around I found the solution. The problem was the name in the @ModelAttribute. After removing the "project" the method works as expectet. Because of the confusion regarding the "getProject()"-method, I did a little refactoring to make the intention of the method more clear. Here is the complete class with additional comments:

@Controller
@RequestMapping("/p/{abbr}/scope")
public class ScopeController {

    private static final String SHOW_PROJECT_PAGE = "/projects/scope/show";

    private static final Logger log = LoggerFactory.getLogger(ScopeController.class);

    @Autowired
    private ProjectService projectService;

    // method is called before show() and update()
    @ModelAttribute
    private void initProject(@PathVariable(value = "abbr") String abbr, Model model) {
        log.debug("loading project for '{}'", abbr);
        // load the project JPA entity from the database, will be merged with the  
        // updated form values in the POST request. By doing this, I can asure
        // that the primary key (the ID) and the related objects are present as 
        // needed for the em.saveOrUpdate() in the projectService.save() method.
        model.addAttribute("project", projectService.find(abbr));
    }

    @RequestMapping(method = RequestMethod.GET)
    public String show() throws BindException {
        // shows the project scope form with the project 
        // added in 'initProject()'
        return SHOW_PROJECT_PAGE;
    }

    @RequestMapping(method = RequestMethod.POST)
    public String update(
            // the project with the updated form values and the JPA ID and JPA 
            // relations as loaded in the initProject()
            @Valid @ModelAttribute Project project, BindingResult result, 
            RedirectAttributes redirectAttrs)
            throws MethodArgumentNotValidException {

        redirectAttrs.addFlashAttribute(project);

        try {
            if (!result.hasErrors()) {
                projectService.save(project);
            }
        }
        catch (Exception e) {
            log.error(e.toString());
            throw new MethodArgumentNotValidException(null, result);
        }

        log.debug("project '{}' updated", project.getAbbreviation());
        return SHOW_PROJECT_PAGE;
    }
}

Thanks all for your answers.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM