1 package facafesta
.controller
;
3 import java
.io
.IOException
;
5 import javax
.persistence
.EntityManager
;
6 import javax
.servlet
.ServletException
;
7 import javax
.servlet
.annotation
.WebServlet
;
8 import javax
.servlet
.http
.HttpServletRequest
;
9 import javax
.servlet
.http
.HttpServletResponse
;
11 import facafesta
.Servlet
;
12 import facafesta
.Util
;
13 import facafesta
.model
.Item
;
15 @WebServlet("/item/*")
16 public class VerItem
extends Servlet
{
18 private static final long serialVersionUID
= 1L;
21 protected void doGet(HttpServletRequest req
, HttpServletResponse resp
) throws ServletException
, IOException
{
23 String
[] parts
= req
.getRequestURI().substring(1).split("/");
24 if (parts
.length
!= 2 || !parts
[1].matches("\\d+")) {
27 EntityManager manager
= Util
.getEntityManager();
28 Long idItem
= new Long(parts
[1]);
29 Item item
= (Item
) manager
.createQuery("from Item where id = :id").setParameter("id", idItem
).getSingleResult();
33 req
.setAttribute("item", item
);
34 getServletContext().getRequestDispatcher("/WEB-INF/item-show.jsp").forward(req
, resp
);
40 private void naoEncontrado(HttpServletResponse resp
) throws IOException
{
41 addErro("Item não encontrado.");
42 resp
.sendRedirect("/itens");