繁体   English   中英

h:form enctype =“ multipart / form-data”不执行任何操作

[英]h:form enctype=“multipart/form-data” not firing any action

我正在使用JSF,但我有一个问题:当我删除该属性时,h:commandButton在表单具有atti enctype =“ multipart / form-data”时不从托管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);
  }
} 

谢谢。

我终于明白问题出在哪里了,我错过了在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>

您没有显示为JSF注释导入的软件包。 您可能正在导入错误的。 看到这个

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM