gpt4 book ai didi

java - :form enctype ="multipart/form-data" not firing any action

转载 作者:行者123 更新时间:2023-12-01 12:07:44 25 4
gpt4 key购买 nike

我正在使用 JSF,并且遇到这样的问题:当表单具有 te 属性 enctype="multipart/form-data"时,当我删除该属性时,h:commandButton 不会从托管 bean 一侧触发任何操作美好的。但这不是我所做的,因为我使用文件上传。

这是我的表格:

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

关于java - :form enctype ="multipart/form-data" not firing any action,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27469914/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com