NullPointerException at setCharacterEncoding(UploadRequestWr



我正在尝试使用特立尼达fileupload (tr:inputFile)组件上传文件到服务器,但我得到一个

null point exception at setCharacterEncoding(UploadRequestWrapper.java:83)

按下命令按钮。

我的XHTML文件包含:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:tr="http://myfaces.apache.org/trinidad" >
<ui:composition template="/WEB-INF/templates/BasicTemplate.xhtml">
    <ui:define name="content">
        <tr:form usesUpload="true">
            <tr:inputFile label="Upload:" valueChangeListener="#{myBackingBean.fileUploaded}"/>
            <tr:commandButton text="Begin"/>
        </tr:form>
    </ui:define>
    </ui:define>
</ui:composition>
</html>

the Backing Bean:

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ValueChangeEvent;
import org.apache.myfaces.trinidad.model.UploadedFile;
public class MyBackingBean{
    ...
    public void fileUploaded(ValueChangeEvent event){
        UploadedFile file = (UploadedFile) event.getNewValue();
        if (file != null){
            FacesContext context = FacesContext.getCurrentInstance();
            FacesMessage message = new FacesMessage(
                "Successfully uploaded file " + file.getFilename() +
                " (" + file.getLength() + " bytes)");
                context.addMessage(event.getComponent().getClientId(context), message);
                // Here's where we could call file.getInputStream()
        }
    }
}

WEB-INF/web.xml文件:

<filter>
    <filter-name>trinidad</filter-name>
    <filter-class>org.apache.myfaces.trinidad.webapp.TrinidadFilter</filter-class
</filter>
<servlet>
    <servlet-name>faces</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
</servlet>
<filter-mapping>
    <filter-name>trinidad</filter-name>
    <servlet-name>faces</servlet-name>
</filter-mapping>

我使用:

  1. 服务器:Tomcat 7.0.12
  2. MyFaces 2.2.0
  3. 特立尼达2.0.1
  4. JSTL 1.2
  5. Eclipse Java EE IDE for Web Developers/Version: Kepler Service Release 1

全输出:

Schwerwiegend: Servlet.service() for servlet [Faces Servlet] in context with path [/com.cargarantie.ws.claimshandler] threw exception [null] with root cause java.lang.NullPointerException
at org.apache.myfaces.trinidadinternal.config.upload.UploadRequestWrapper.setCharacterEncoding(UploadRequestWrapper.java:83)
at org.apache.myfaces.context.servlet.ServletExternalContextImpl.setRequestCharacterEncoding(ServletExternalContextImpl.java:581)
at javax.faces.context.ExternalContextWrapper.setRequestCharacterEncoding(ExternalContextWrapper.java:416)
at javax.faces.context.ExternalContextWrapper.setRequestCharacterEncoding(ExternalContextWrapper.java:416)
at javax.faces.application.ViewHandler.initView(ViewHandler.java:339)
at org.apache.myfaces.application.ViewHandlerImpl.initView(ViewHandlerImpl.java:191)
at javax.faces.application.ViewHandlerWrapper.initView(ViewHandlerWrapper.java:49)
at org.apache.myfaces.lifecycle.RestoreViewExecutor.doPrePhaseActions(RestoreViewExecutor.java:83)
at org.apache.myfaces.lifecycle.LifecycleImpl.executePhase(LifecycleImpl.java:182)
at org.apache.myfaces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:143)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:196)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:395)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:250)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

任何想法?

我解决了这个问题!

我不知道这种方法是否是解决问题的最佳方法,但它确实有效。

  1. 创建自己的SetCharacterEncodingFilter类并将其添加到构建路径中。来源:

    import java.io.IOException;
    import javax.servlet.Filter;
    import javax.servlet.FilterChain;
    import javax.servlet.FilterConfig;
    import javax.servlet.ServletException;
    import javax.servlet.ServletRequest;
    import javax.servlet.ServletResponse;
    public class SetCharacterEncodingFilter implements Filter {
        protected String encoding = null;
        protected FilterConfig filterConfig = null;
        protected boolean ignore = true;
        @Override
        public void destroy() {
            this.encoding = null;
            this.filterConfig = null;
        }
        @Override
        public void doFilter(ServletRequest request, ServletResponse response,
                     FilterChain chain) throws IOException, ServletException {
            // Conditionally select and set the character encoding to be used
            if (ignore || (request.getCharacterEncoding() == null)) {
                String characterEncoding = selectEncoding(request);
                if (characterEncoding != null)
                    request.setCharacterEncoding(characterEncoding);
            }
    
            // Pass control on to the next filter
            chain.doFilter(request, response);
            Utilities.ConsoleMsg("Done.", this, "doFilter", 5);
        }
        @Override
        public void init(FilterConfig fConfig) throws ServletException {
            this.filterConfig = fConfig;
            this.encoding = fConfig.getInitParameter("encoding");
            String value = fConfig.getInitParameter("ignore");
            if (value == null)
                this.ignore = true;
            else if (value.equalsIgnoreCase("true"))
                this.ignore = true;
            else if (value.equalsIgnoreCase("yes"))
                this.ignore = true;
            else
                this.ignore = false;
        }
        protected String selectEncoding(ServletRequest request) {
            return (this.encoding);
         }
    
    }
    
  2. 在web.xml中注册过滤器并设置初始化参数。

    <filter>
        <filter-name>UTFEncodingFilter</filter-name>
        <filter-class>path.to.your.package.SetCharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>ignore</param-name>
            <param-value>false</param-value>
        </init-param>
    </filter>
    
  3. server.xml中的所有连接器添加URIEncoding="UTF-8"属性。例子:

    <Connector port="8080"
               protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443"
               URIEncoding="UTF-8" />
    

来源:

  • https://issues.apache.org/jira/browse/trinidad - 1744
  • http://docs.oracle.com/cd/E16162_01/core.1112/e22506/chapter_adf_trinid_messages.htm
  • tomcat的字符编码问题

相关内容

  • 没有找到相关文章