由于过滤器,交易中止?

时间:2013-06-26 16:42:08

标签: jpa jsf-2 filter transactions

我遇到Filter问题并插入一个新的Object。我有一个过滤器:

的web.xml

<filter>
  <filter-name>authFilter</filter-name>
  <filter-class>filter.AuthFilter</filter-class>
</filter>
<filter-mapping>
  <filter-name>authFilter</filter-name>
  <url-pattern>/admin/*</url-pattern>
</filter-mapping>

AuthFilter.java

public class AuthFilter implements Filter  {

@Override
public void init(FilterConfig filterConfig) throws ServletException {
}

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
  HttpServletRequest req = (HttpServletRequest) request;
  HttpServletResponse res = (HttpServletResponse) response;
  LoginBean sesion = (LoginBean) req.getSession().getAttribute("loginBean");

  if (sesion != null && sesion.isLoggedIn()) {
    if (sesion.hasRole("admin"))
      chain.doFilter(request, response);
    else
      res.sendRedirect(req.getContextPath());
  } else {
    res.sendRedirect(req.getContextPath() + "/user/login.xhtml");
  }
}

@Override
public void destroy() {
}

我觉得这很好用。当我尝试访问admin.xhtml时,如果我没有登录,请将我重定向到login.xhtml。 在admin.xhtml中,我有一个表单和我的Bean:

public class AdminBean {

  FacesContext context;
  @EJB
  private ProductoBeanLocal productoBean;
  private Producto producto;

  public String addProduct() {
    context = FacesContext.getCurrentInstance();
    if (producto != null) {
      producto.setFechaEntrada(new Date());
      Producto flag = productoBean.insert(producto);
      if (flag != null)
        context.addMessage("growl", new FacesMessage("Success", 
            "El Producto " + flag.getNombre() + " se ha insertado correctamente"));
      else
        context.addMessage("growl", new FacesMessage("Success", 
            "Se produjo un error al insertar el Producto"));
    }
    return "admin";
  }

调试时,我到达:

  

context.addMessage(“growl”,新的FacesMessage(“成功”,                   “El Producto”+ flag.getNombre()+“se ha insertado correctamente”));

跳转到:

  

chain.doFilter(request,response);

我的过滤器的

,离开过滤器。我的申请抛出:

  

javax.ejb.EJBException:事务已中止

我虽然是Bean注释,@ RequestScoped,@ ViewScoped,@ SessionScoped,但我尝试了所有,我没有得到任何东西。

任何人都可以帮助我吗?向我询问更多信息。

提前致谢!

问候。

0 个答案:

没有答案