JSF commandButton动作未调用

时间:2012-07-30 17:57:20

标签: jsf tomahawk

我有以下页面使用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";
        }
    }

1 个答案:

答案 0 :(得分:5)

此表单使用multipart/form-data编码提交,默认情况下支持 。只有在ExtensionsFilter根据its documentation正确注册了战斧的web.xml时,它才有效。此过滤器负责解析multipart/form-data个请求并将各个form-data部分转换为正常的请求参数,以便JSF可以通常的方式透明地应用请求参数(所以:提交的值和调用的操作)

如果由于您的webapp中的其他一些令人不安的因素而未注册{/ 1}和/或无法正常工作,那么表单提交将永远不会进入JSF回发生命周期。

另见: