0
votes

Im using JSF and i have this problem that the h:commandButton not firing any action from the side of the managed bean when the form has te attibute enctype="multipart/form-data", when i remove that attribute is work fine. But that is not what i do, because i use fileupload.

Here is my form:

    <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>

And here is my 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>

Any ideas? I tried to change the bean to @ViewScoped, to @RequestScoped and nothing.

Here is my managed 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);
  }
} 

Thanks.

2

2 Answers

2
votes

I finally get what the problem was, i missed define the Faces Servlet in the web.xml.

<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>
0
votes

You didn't show the packages you imported for JSF annotations. You probably are importing wrong ones. see this