我遇到了过滤器问题并插入了一个新对象。我有一个过滤器:
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("咆哮", new FacesMessage("Success", "El Producto" + flag.getNombre() + "se ha insertado correctamente"));
跳到:
链.doFilter(请求,响应);
我的过滤器,然后离开过滤器。我的应用程序抛出:
javax.ejb.EJBException:事务中止
我虽然是 Bean 注释,@RequestScoped,@ViewScoped,@SessionScoped 但我尝试了所有,但我没有得到任何东西。
有谁能够帮我?向我询问更多信息。
提前致谢!
问候。