您好我正在尝试使用 Netbeans 中的 Primeface Basic Upload 工具(http://www.primefaces.org/showcase/ui/fileUploadSimple.jsf)。我的目标是在我的 Oracle 数据库中上传图像。目前加载了按钮等界面。但是,当我按下提交按钮时,它似乎没有到达我的代码。不幸的是,我没有收到任何错误消息,它似乎只到达我的 XHTML 页面。如何访问托管 bean 中的代码?
这是我的 XHTML 页面
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui">
<body>
<h:form enctype="multipart/form-data">
<p:messages showDetail="true" />
<p:fileUpload value="#{FileUploadController.file}" mode="simple" />
<p:commandButton value="Submit" ajax="false" actionListener="#{FileUploadController.handleFileUpload}" />
<h:graphicImage id="image" alt="uploaded image" url="/image"/>
</h:form>
</body>
</html>
这是托管 bean(注意:它没有到达我的日志)
public class FileUploadController
{
private UploadedFile file;
private static final transient Log log = Log.getLog(null);
public void handleFileUpload() throws IOException
{
log.info("Checking if code is reaching breakpoint");
try
{
String amId = file.getFileName();
byte[] bytes = file.getContents();
ArticleManager aMan = new ArticleManager();
aMan.updateImageForArticle(amId, bytes);
FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Upload success", file.getFileName() + " is uploaded.");
FacesContext.getCurrentInstance().addMessage(null, msg);
}
catch(Exception e)
{
FacesMessage errorMsg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Upload error", e.getMessage());
FacesContext.getCurrentInstance().addMessage(null, errorMsg);
}
}
public UploadedFile getFile()
{
return file;
}
public void setFile(UploadedFile file)
{
this.file = file;
}
}
ArticleManager
部分(连接到 Oracle 数据库并插入右列)。
public void updateImageForArticle(String amId, byte[] image) throws DatabaseObjectException
{
StringBuffer sb = new StringBuffer();
// Insert into my columns in database (img_data is a BLOB file type)
sb.append("insert into image(img_id, img_data) values (?, ?)");
ILCDatabaseConnection conn = null;
try
{
//connect to the database (pretty sure this works)
conn = pool.getConnection();
// create the statement object
PreparedStatement stmt = conn.prepareStatement(sb.toString());
stmt.setString(1, amId);
stmt.setBytes(2, image);
stmt.executeUpdate();
conn.commit();
}
catch (Exception ex)
{
ex.printStackTrace();
}
finally
{
if (conn != null)
conn.close();
}
}
我的web.xml
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" 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-app_3_0.xsd">
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<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>*.xhtml</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<filter>
<filter-name>Pretty Filter</filter-name>
<filter-class>com.ocpsoft.pretty.PrettyFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>Pretty Filter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>FORWARD</dispatcher>
<dispatcher>REQUEST</dispatcher>
<dispatcher>ERROR</dispatcher>
</filter-mapping>
<welcome-file-list>
<welcome-file>loadFileBrowseDialog.xhtml</welcome-file>
</welcome-file-list>
<filter>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
<init-param>
<param-name>thresholdSize</param-name>
<param-value>51200</param-value>
</init-param>
<init-param>
<param-name>uploadDirectory</param-name>
<param-value>c:/tmp</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<servlet-name>Faces Servlet</servlet-name>
</filter-mapping>
</web-app>
希望我没有犯任何严重的错误,不过这是我第一次使用 primefaces。