h:表单enctype =" multipart / form-data"没有解雇任何行动

时间:2014-12-14 13:42:06

标签: java forms jsf primefaces jboss

我使用JSF,我有这个问题,当表单有te attibute enctype =“multipart / form-data”时h:commandButton没有从托管bean一侧触发任何操作,当我删除该属性是工作精细。但这不是我做的,因为我使用fileupload。

这是我的表格:

    <h:form enctype="multipart/form-data" class="form-horizontal" id="form-catastrofe">
        <p:growl id="messages" showDetail="true" />
        <div class="form-group">
            <div class="col-sm-12">
                <p:fileUpload fileUploadListener="#{catastrofeBean.handleFileUploadImagen}" mode="advanced" dragDropSupport="false"
                        multiple="false" allowTypes="/(\.|\/)(gif|jpe?g|png)$/"
                        label="Elegir Icono"
                        cancelLabel="Cancelar"
                        update="messages" />
                    </div>
            </div>
        <div class="form-group">
            <div class="col-sm-12"> 
                <p:inputText id="inputname" value="#{catastrofeBean.catastrofe.nombre}" required="true" label="Name" styleClass="form-control">
                  <f:validateLength minimum="2" />
                </p:inputText>
                <p:watermark for="inputname" value="Nombre de Evento" id="watermarkName" />
            </div>
        </div>
        <div class="form-group">
           <div class="col-sm-12">  
              <p:inputText id="inputdescripcion" value="#{catastrofeBean.catastrofe.descripcion}" required="true" label="Name" styleClass="form-control"/>
              <p:watermark for="inputdescripcion" value="Descripcion" id="watermarkDesc" />
           </div>
        </div>
        <div class="form-group">
           <div class="col-sm-12">                      
              <p:inputText id="inputdireccion" value="#{catastrofeBean.catastrofe.direccion}" required="true" label="Name" styleClass="form-control"/>
              <p:watermark for="inputdireccion" value="Direccion" id="watermarkDireccion" />           
           </div>
        </div>
        <div class="form-group">
           <div class="col-sm-12">
             <p:fileUpload fileUploadListener="#{catastrofes.handleFileUpload}" mode="advanced" dragDropSupport="false"
             multiple="true" allowTypes="/(\.|\/)(pdf)$/"
             label="Elegir Plan"
             cancelLabel="Cancelar"
             update="messages" />
           </div>
        </div>    
        <h:commandButton value="Ingresar Catástrofe" action="#{catastrofeBean.update}" class="pull-right btn btn-success"></h:commandButton>
   </h:form>

这是我的web.xml:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <web-app xmlns="http://java.sun.com/xml/ns/javaee"              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="3.0" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name>catastrophes-system-web</display-name>
<context-param>
  <param-name>javax.faces.DATETIMECONVERTER_DEFAULT_TIMEZONE_IS_SYSTEM_TIMEZONE</param-name>
  <param-value>true</param-value>
</context-param>
<context-param>
  <param-name>primefaces.THEME</param-name>
  <param-value>bootstrap</param-value>
</context-param>
<context-param>
  <param-name>primefaces.UPLOADER</param-name>
  <param-value>auto</param-value>
</context-param>
<session-config>
  <session-timeout>30</session-timeout>
</session-config>
<servlet>
  <servlet-name>javax.ws.rs.core.Application</servlet-name>
  <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
  <servlet-name>javax.ws.rs.core.Application</servlet-name>
  <url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<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>javax.ws.rs.core.Application</servlet-name>
</filter-mapping>
<error-page>
  <error-code>404</error-code>
  <location>/faces/error.xhtml</location>
</error-page>
<error-page>
  <error-code>500</error-code>
  <location>/faces/error.xhtml</location>
</error-page>
</web-app>

有什么想法吗?我试图将bean更改为@ViewScoped,更改为@RequestScoped而没有。

这是我的托管bean:

@Named
@Stateful
@ConversationScoped
public class CatastrofeBean implements Serializable
{

  private static final long serialVersionUID = 1L;

  /*
   * Support creating and retrieving Catastrofe entities
   */

  private Long id;

  public Long getId()
  {
    return this.id;
  }

  public void setId(Long id)
  {
    this.id = id;
  }

  private Catastrofe catastrofe;

  public Catastrofe getCatastrofe()
  {
   return this.catastrofe;
  }

  @Inject
  private Conversation conversation;

  @Inject
  private CatastrofeDao catastofeDao;


  public String create()
  {
    this.conversation.begin();
    return "create?faces-redirect=true";
  }


  /*
   * Support updating and deleting Catastrofe entities
   */

  public String update()
  {
    this.conversation.end();

    try
    {
       if (this.id == null)
       {
        this.catastofeDao.create(this.catastrofe);
        return "search?faces-redirect=true";
       }
       else
       {
        this.catastofeDao.update(this.catastrofe);
        return "view?faces-redirect=true&id=" + this.catastrofe.getId();
       }
    }
    catch (Exception e)
    {
     FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(e.getMessage()));
     return null;
    }
  }

  public void handleFileUploadImagen(FileUploadEvent event) {       
    FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Succesful", event.getFile().getFileName() + " is uploaded.");
    FacesContext.getCurrentInstance().addMessage(null, message);
  }

  public void handleFileUpload(FileUploadEvent event) {
    FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Succesful", event.getFile().getFileName() + " is uploaded.");
    FacesContext.getCurrentInstance().addMessage(null, message);
  }
} 

感谢。

2 个答案:

答案 0 :(得分:1)

我终于明白了问题所在,我错过了在web.xml中定义Faces Servlet。

<servlet>
  <servlet-name>Faces Servlet</servlet-name>
  <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
  <servlet-name>Faces Servlet</servlet-name>
  <url-pattern>/faces/*</url-pattern>
</servlet-mapping>

答案 1 :(得分:0)

您没有显示为JSF注释导入的软件包。你可能导入错误的。见this