1

我有以下页面使用 JSF 将 .pdf 文件上传到服务器上指定的文件夹。但是,commandButton 操作似乎根本没有触发,因为我的 println() 和消息更新都没有显示。如果有人能解释这个问题,将不胜感激。

XHTML 页面:

    <?xml version="1.0" encoding="UTF-8"?>
<!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:t="http://myfaces.apache.org/tomahawk"
    xmlns:f="http://java.sun.com/jsf/core" xml:lang="en" lang="en">
    <head>
        <meta http-equiv="keywords" content="enter,your,keywords,here" />
        <meta http-equiv="description"
            content="A short description of this page." />
        <meta http-equiv="content-type" content="text/html; charset=UTF-8" />

        <link rel="stylesheet" type="text/css" href="Styles/style.css" />
        <script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery/jquery-1.7.2.min.js"></script>
    </head>
    <body>


        <h:form enctype="multipart/form-data">
            <h:outputText value="First Name: " />
            <h:inputText id="fname" size="40" value="#{userBean.firstName}" required="true"/> <br/>
            <h:outputText value="Last Name: " />
            <h:inputText id="lname" size="40" value="#{userBean.lastName}" required="true"/> <br/>
            <h:outputText value="Position Sought: "/>
            <h:inputText id="position" size="40" value="#{userBean.position}" required="true"/> <br/>
            <h:outputText value="File to upload: " />
            <t:inputFileUpload value="#{userBean.resume}" /> <br/>
            <h:commandButton value="submit" action="#{userBean.submit}" />
            <h:messages />
        </h:form>

    </body>
</html>

面孔:

<?xml version="1.0" encoding="UTF-8"?>
<faces-config version="2.0" xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xi="http://www.w3.org/2001/XInclude"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd">
 <managed-bean>
  <managed-bean-name>userProcessor</managed-bean-name>
  <managed-bean-class>com.logic.UserProcessor</managed-bean-class>
  <managed-bean-scope>session</managed-bean-scope>
  <managed-property>
   <property-name>ID</property-name>
   <property-class>java.lang.String</property-class>
   <value/>
  </managed-property>
  <managed-property>
   <property-name>firstName</property-name>
   <property-class>java.lang.String</property-class>
   <value/>
  </managed-property>
  <managed-property>
   <property-name>lastName</property-name>
   <property-class>java.lang.String</property-class>
   <value/>
  </managed-property>
  <managed-property>
   <property-name>password</property-name>
   <property-class>java.lang.String</property-class>
   <value/>
  </managed-property>
  <managed-property>
   <property-name>resume</property-name>
   <property-class>java.lang.String</property-class>
   <value/>
  </managed-property>
  <managed-property>
   <property-name>username</property-name>
   <property-class>java.lang.String</property-class>
   <value/>
  </managed-property>
 </managed-bean>
 <managed-bean>
  <managed-bean-name>userBean</managed-bean-name>
  <managed-bean-class>com.logic.UserBean</managed-bean-class>
  <managed-bean-scope>request</managed-bean-scope>
  <managed-property>
   <property-name>id</property-name>
   <property-class>java.lang.String</property-class>
   <value/>
  </managed-property>
  <managed-property>
   <property-name>username</property-name>
   <property-class>java.lang.String</property-class>
   <value/>
  </managed-property>
  <managed-property>
   <property-name>password</property-name>
   <property-class>java.lang.String</property-class>
   <value/>
  </managed-property>
  <managed-property>
   <property-name>firstName</property-name>
   <property-class>java.lang.String</property-class>
   <value/>
  </managed-property>
  <managed-property>
   <property-name>lastName</property-name>
   <property-class>java.lang.String</property-class>
   <value/>
  </managed-property>
  <managed-property>
   <property-name>position</property-name>
   <property-class>java.lang.String</property-class>
   <value/>
  </managed-property>
  <managed-property>
   <property-name>resume</property-name>
   <property-class>org.apache.myfaces.custom.fileupload.UploadedFile</property-class>
   <value/>
  </managed-property>
 </managed-bean>
 <navigation-rule>
  <from-view-id>/addUser.xhtml</from-view-id>
  <navigation-case>
   <from-outcome>failure</from-outcome>
   <to-view-id>/addUser.xhtml</to-view-id>
  </navigation-case>
  <navigation-case>
   <from-outcome>success</from-outcome>
   <to-view-id>/Home_Menu.xhtml</to-view-id>
  </navigation-case>
 </navigation-rule>
 <navigation-rule>
  <from-view-id>/Home_Menu.xhtml</from-view-id>
 </navigation-rule>
 <navigation-rule>
  <from-view-id>/login.xhtml</from-view-id>
  <navigation-case>
   <from-outcome>failure</from-outcome>
   <to-view-id>/login.xhtml</to-view-id>
  </navigation-case>
  <navigation-case>
   <from-outcome>success</from-outcome>
   <to-view-id>/Home_Menu.xhtml</to-view-id>
  </navigation-case>
 </navigation-rule>
 <navigation-rule>
  <from-view-id>/upload_resume.xhtml</from-view-id>
  <navigation-case>
   <from-action>#{userBean.submit}</from-action>
   <from-outcome>success</from-outcome>
   <to-view-id>/Home_Menu.xhtml</to-view-id>
  </navigation-case>
  <navigation-case>
   <from-action>#{userBean.submit}</from-action>
   <from-outcome>failure</from-outcome>
   <to-view-id>/upload_resume.xhtml</to-view-id>
  </navigation-case>
 </navigation-rule>
</faces-config>

和 bean.submit()

  public String submit() throws IOException {
        String fileName = FilenameUtils.getName(resume.getName());
        String contentType = resume.getContentType();
        InputStream is = resume.getInputStream();
        byte[] bytes = resume.getBytes();

        Date date = new Date();
        String ext = FilenameUtils.getExtension(fileName);
        System.out.println("File upload started");
        System.out.println("File name: " + fileName);
        System.out.println("Timestamp: " + contentType);
        System.out.println("Content type: " + date.toString());
        System.out.println("File extension: " + ext);

        // Now you can save bytes in DB (and also content type?)

        if(ext != "pdf"){
            FacesContext.getCurrentInstance().addMessage(null, 
                    new FacesMessage(String.format("File upload failed, please make sure that your file has the correct extension (.pdf)")));
            System.out.println("File upload failed: incorrect file extension.");
            return "failure";
        } else {
            writeInfoToDB();
            is.read(bytes);

            String folder = getFirstName().charAt(0) + getLastName();                           //will not work if folder does not exist
            File file = new File("C:\\Users\\MAR-PC01\\Desktop\\apache-tomcat-7.0\\uploads\\" 
                                 + folder + "\\" + fileName);
            file.createNewFile();
            FileOutputStream fos = new FileOutputStream(file);
            fos.write(bytes);

            FacesContext.getCurrentInstance().addMessage(null, 
                new FacesMessage(String.format("File '%s' of type '%s' successfully uploaded!", fileName, contentType)));

            return "success";
        }
    }
4

1 回答 1

5

此表单使用JSF 默认支持的multipart/form-data编码提交。只有根据其文档正确注册Tomahawk's 才会起作用。这个过滤器负责解析请求并将各个部分转换为正常的请求参数,以便 JSF 可以以通常的方式透明地应用请求参数(例如:提交的值和调用的操作)。ExtensionsFilterweb.xmlmultipart/form-dataform-data

如果ExtensionsFilter由于 webapp 中的其他一些干扰因素而没有注册和/或无法正常工作,那么表单提交将永远不会进入 JSF 回发生命周期。

也可以看看:

于 2012-07-30T18:29:39.377 回答