Which component will fetch model data, supply that data to views, as well as handle the routing of incoming requests?
Controller
Controller -----> Service -----> DAO-Data Access Object(Repository) -----> SessionFactory
Starting Your Database Server
With a terminal window open in the directory of your project that contains the H2 JAR file, the command to start your server is as follows:
java -cp h2*.jar org.h2.tools.Server
After this, a browser tab should open, and in that tab you'll need to enter the database connection URL, username and password. These should match what you have in your app.properties file.
@OneToMany(mappedBy = "category") private List<Gif> gifs = new ArrayList<>();mappedBy后面有一个变量fetch默认value为fetchtype.lazy
可以改成fetchtype.eager -> Every time one or more categories are fetched from the database, another query will be performed to fetch all GIF entities from the database that are associated with that category.
但是如果直接设成eager,那么即使在category index page,没有的GIFs也会被fetch
So we fetch all the GIFs for a category, only when a single category is being fetched.
@Overridepublic Category findById(Long id) { Session session = sessionFactory.openSession(); Category category = session.get(Category.class, id); Hibernate.initialize(category.getGifs()); session.close(); return category;}We were seeing that error because we tried to access a mapped collection outside of a hibernate session, which just isn't possible. 所以加入一句Hibernate.initialize(category.getGifs());
// Mark/unmark an existing GIF as a favorite@RequestMapping(value = "/gifs/{gifId}/favorite", method = RequestMethod.POST) public String toggleFavorite(@PathVariable Long gifId, HttpServletRequest request) { // TODO: With GIF whose id is gifId, toggle the favorite field Gif gif = gifService.findById(gifId); gifService.toggleFavourate(gif); // TODO: Redirect to GIF's detail view return String.format("redirect:%s", request.getHeader("referer"));}
- If you throw this in as a parameter to a controller method,
- 3:13it exposes all sorts of stuff related to the HTTP request including
- 3:17all of its headers that you recall from the HTTP basics course.
- 3:23One of the headers it exposes is what's called the referer header here.
- 3:29Check the teachers notes for the misspelling on this.
- 3:31This is actually the correct use.
- 3:34Now this referer header allows you to see what URL the user or request or
- 3:39was on when the HTTP request was made.
- 3:42In essence, it allows us to see where this request came from.
- 3:45So that in our case, I can send the browser right back there for the redirect.
- 3:50And I did this because a user might mark a GIF as a favorite from the detail or
- 3:54from the index view and this takes the browser back to wherever it came from.
- 3:59Now word of warning about this and I do want to be clear about this warning.
- 4:04Using a client specified value can be dangerous.
- 4:09If the user's browser is compromised,
- 4:12malicious software could set the referer to a malicious site.
- 4:17And suddenly our application sends the user into a danger zone
- 4:21simply when they mark or unmark a GIF as a favorite.
- 4:25So be careful.
No comments:
Post a Comment