我正在使用JSF 2.0,tomcat 7.0,mojarra 2.0.3和primefaces。 问题是我没有成功实现javax.servlet.Filter类重定向到登录页面如果用户试图在没有登录的情况下进入页面,但我也使用FileUpload将文件上传到服务器,我认为这是什么我和doFilter方法触发冲突,服务器抛出以下异常:
javax.servlet.ServletException
javax.faces.webapp.FacesServlet.service (FacesServlet.java: 321)
org.primefaces.webapp.filter.FileUploadFilter.doFilter (FileUploadFilter.java: 98)
org.apache.tomcat.websocket.server.WsFilter.doFilter (WsFilter.java: 52)
我按如下方式配置了web.xml文件:
<!--this may be the cause of the error--!>
<filter>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<servlet-name>Faces Servlet</servlet-name>
</filter-mapping>
<filter>
<filter-name>Login</filter-name>
<filter-class>controller.BeanFiltersession</filter-class>
</filter>
<filter-mapping>
<filter-name>Login</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
之后删除实现Filter的类并删除web.xml文件中的行,但错误仍然存在 任何想法如何解决它?
问题不是FileUpload问题介于primefaces之间,我认为问题在于我指出一个过滤器使用HttpSession和HttpServletRequest来验证会话,登录后发给我这个异常,很明显删除了实现Filter的类,探测FileUpload并且工作没有问题,但我认为我在该类中所做的更改是在具有Tomcat 7.0的api.jar servlet-api中记录的,我的代码如下:package validators;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
public class SessionValidator {
public static HttpSession getSesion(){
return (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
}
public static HttpServletRequest getRequest(){
return (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
}
public static String getUser(){
HttpSession sesion= (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
return sesion.getAttribute("usuario").toString();
}
public static String getNivel(){
HttpSession sesion=getSesion();
if(sesion !=null){
return sesion.getAttribute("nivel").toString();
}else{
return null;
}
}
}
我登录代码时调用的SessionValidator类是:
package controller;
import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.servlet.http.HttpSession;
import org.primefaces.context.RequestContext;
import validators.SessionValidator;
import model.csConexion;
import model.csSesion;
@ManagedBean
@SessionScoped
public class BeanSesion implements Serializable{
private static final long serialVersionUID = 1L;
private csSesion sesion;
private csConexion con;
HttpSession session;
private String usuario;
private String nivel;
public BeanSesion(){
sesion=new csSesion();
con=new csConexion();
}
public String getUsuario(){
return SessionValidator.getUser();
}
public void setUsuario(String usuario){
this.usuario=usuario;
}
public String getNivel(){
if(SessionValidator.getSesion().getAttribute("usuario")!=null){
return SessionValidator.getNivel();
}else{
return "";
}
}
public void setNivel(String nivel){
this.nivel=nivel;
}
public csSesion getSesion(){
return sesion;
}
public void setSesion(csSesion sesion){
this.sesion=sesion;
}
public String iniciarsesion(ActionEvent e){
RequestContext context= RequestContext.getCurrentInstance();
FacesContext fcontext= FacesContext.getCurrentInstance();
FacesMessage message=null;
boolean ok=false;
ResultSet rs;
try {
if(con.conectar()==true){
String sql="select*from usuario where usuario=? and contrasenia=?";
PreparedStatement comando= con.getConexion().prepareStatement(sql);
comando.setString(1,sesion.getUsuario());
comando.setString(2,sesion.getContrasenia());
rs=comando.executeQuery();
if(rs.next()){
nivel= rs.getString("nivel");
usuario= rs.getString("usuario");
ok=true;
session= SessionValidator.getSesion();
session.setAttribute("usuario", usuario);
session.setAttribute("nivel", nivel);
message= new FacesMessage(FacesMessage.SEVERITY_INFO,"¡Bienvenido!",sesion.getUsuario());
try {
if(nivel.equals("master")){
fcontext.getExternalContext().redirect("admin/admin.xhtml");
}else if(nivel.equals(null)){
fcontext.getExternalContext().redirect("index.xhtml");
}
} catch (Exception e2) {
e2.printStackTrace();
message= new FacesMessage(FacesMessage.SEVERITY_WARN,"Error","Error");
}
}else{
ok=false;
message= new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error","Usuario o contraseña inválidos");
}
FacesContext.getCurrentInstance().addMessage(null,message);
context.addCallbackParam("Ok", ok);
}
} catch (Exception ex) {
ex.printStackTrace();
ok=false;
}
finally{
System.out.println(nivel);
con.desconectar();
}
return nivel;
}
}
这是发送Tomcat的例外:
excepción
javax.servlet.ServletException: null source
javax.faces.webapp.FacesServlet.service(FacesServlet.java:321)
org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:98)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
causa raíz
java.lang.IllegalArgumentException: null source
java.util.EventObject.<init>(Unknown Source)
javax.faces.event.SystemEvent.<init>(SystemEvent.java:67)
javax.faces.event.ComponentSystemEvent.<init>(ComponentSystemEvent.java:69)
javax.faces.event.PostRestoreStateEvent.<init>(PostRestoreStateEvent.java:69)
com.sun.faces.lifecycle.RestoreViewPhase.deliverPostRestoreStateEvent(RestoreViewPhase.java:256)
com.sun.faces.lifecycle.RestoreViewPhase.execute(RestoreViewPhase.java:245)
com.sun.faces.lifecycle.Phase.doPhase(Phase.java:97)
com.sun.faces.lifecycle.RestoreViewPhase.doPhase(RestoreViewPhase.java:107)
com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:114)
javax.faces.webapp.FacesServlet.service(FacesServlet.java:308)
org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:98)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)