Skip to content

Controller

Spring Boot finds your controller by component-scanning the application's base package. It will autowire any dependencies, such as a DAO, provided it can find a satisfactory bean.

  1. Inject your DAO in your controller using an @Autowired annotation, so you can access it from within your @RequestMapping methods.
@Controller
public class FilmController {
  @Autowired
  private FilmDAO filmDAO;
}
  1. When you retrieve a JPA entity from a DAO, the returned entity is unmanaged.

  2. Any changes you make to the entity will have no effect on the database, unless you merge it back into the persistence context.


@Controller
public class FilmController {
  @Autowired
  private FilmDAO filmDAO;

  @RequestMapping(path = "getFilm.do", method = RequestMethod.GET)
  public ModelAndView getFilm(@RequestParam("fid") int fid) {
    ModelAndView mv = new ModelAndView();

    Film film = filmDAO.findById(fid);     
    // film is unmanaged after it is outside of the transaction that exists in the DAO

    mv.addObject("film", film);
    mv.setViewName("WEB-INF/film/show.jsp");
    return mv;
  }
}

Drill

  1. Create a JSP file called index.jsp in your src/main/webapp/WEB-INF directory.

  2. Create a new directory inside of src/main/webapp/WEB-INF named film.

  3. Create a JSP file called show.jsp in that directory.

src/main/webapp/
├── WEB-INF
    ├── film
    │   └── show.jsp
    └── index.jsp
  1. In the com.example.bootmvc.controllers package:
  2. Add a class called FilmController to that package. Use the @Autowired annotation to inject your DAO.
  3. In FilmController, create a request handling method that returns the name of your index.jsp.
  4. Map this method to the request paths / and index.do with the method GET.

    @RequestMapping(path={"/","index.do"})
    public String index() {
      return "WEB-INF/index.jsp";
      // return "index"; // if using a ViewResolver.
    }
    

  5. Create a route (or RequestMapping) in FilmController mapped to getFilm.do.

  6. The method will take a single parameter fid as a @RequestParam.
  7. Use the DAO findById method to find a film by the provided id and add it to the model.
  8. Return the view WEB-INF/film/show.jsp.

  9. Modify your index.jsp file to invoke your controller's getFilm.do route

    <form action="getFilm.do" method="GET">
      Film ID: <input type="text" name="fid" />
      <input type="submit" value="Show Film" />
    </form>
    

  10. Modify your show.jsp file to display the film that will be attached to the model.

    <div>
      <h5>${film.title} (${film.releaseYear})</h5>
      <p>${film.description}</p>
    </div>
    


Prev -- Up -- Next