我有一个Java-Web-Application,它读取Tomcat服务器的本地日志文件,并将其写入表中。应该有一个下载按钮,所以我可以下载每个日志。
首先,我选择一个应该读取的日志文件。这有效。现在DownloadController
应该知道我要下载哪个文件。
但我总是得到错误,该类无法实例化。问题出在哪里?
这是我的代码:
的index.xhtml:
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title>LogReader</title>
<style type="text/css">
.info {
background-color: #F2F5A9 !important;
background-image: none !important;
color: #000000 !important;
}
.warn {
background-color: #FFCC00 !important;
background-image: none !important;
color: #000000 !important;
}
.error {
background-color: #FF6600 !important;
background-image: none !important;
color: #000000 !important;
}
.fatal {
background-color: #FF0000 !important;
background-image: none !important;
color: #000000 !important;
}
</style>
<p:dialog modal="true" widgetVar="statusDialog" header="Load Log-File"
draggable="false" closable="false">
<p:graphicImage value="/design/ajaxloadingbar.gif" />
</p:dialog>
</h:head>
<h:body>
<h:form id="logReader">
<p:accordionPanel dynamic="true" cache="true" widgetVar="accordion" activeIndex="-1" multiple="false">
<p:tab title="Select File">
<h:panelGrid>
<p:dataList id="folder" var="file" value="#{fileUploadController.tableBean.listFolder}" itemType="disc">
<p:commandLink id="ajax" update=":logReader:logs" actionListener="#{fileUploadController.readFile(file)}" style="margin-right:20px;" onstart="PF('statusDialog').show();" onsuccess="PF('statusDialog').hide();" onclick="accordion.select(-1);">
<h:outputText value="#{file}" />
</p:commandLink>
</p:dataList>
</h:panelGrid>
</p:tab>
<p:tab title="Download File">
<h:panelGrid>
<p:commandButton id="downloadLink" value="Download" ajax="false" onclick="PrimeFaces.monitorDownload(start, stop)"
icon="ui-icon-arrowthichk-s">
<p:fileDownload value="#{fileDownloadController.tableBean.fileDownload}" />
</p:commandButton>
</h:panelGrid>
</p:tab>
</p:accordionPanel>
<br />
<p:outputPanel id="logs">
<p:dataTable id="dataTable" var="log" value="#{fileUploadController.tableBean.logsSmall}" widgetVar="dataTable" rowStyleClass="#{log.level eq 'ERROR' ? 'error' : log.level eq 'INFO' ? 'info' : log.level eq 'WARN' ? 'warn' : log.level eq 'FATAL' ? 'fatal' : null}"
emptyMessage="No Log found with given criteria" filteredValue="#{tableBean.filteredLogs}"
rowKey="#{log.datetime}" paginator="true" rows="20" paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}" rowsPerPageTemplate="5,10,15,20,50,100" selection="#{tableBean.selectedLog}" selectionMode="single">
<f:facet name="header">
<p:outputPanel>
<h:outputText value="Search all fields:" />
<p:inputText id="globalFilter" onkeyup="dataTable.filter();" style="width:150px" />
</p:outputPanel>
</f:facet>
<p:column id="datetimeColumn" filterBy="datetime" sortBy="datetime"
headerText="DateTime" footerText=""
filterMatchMode="contains">
<h:outputText value="#{log.datetime}" />
</p:column>
<p:column id="levelColumn" filterBy="level"
headerText="LogLevel" footerText=""
filterOptions="#{tableBean.levelOptions}"
filterMatchMode="exact" sortBy="level">
<h:outputText value="#{log.level}" />
</p:column>
<p:column id="categoryColumn" filterBy="category" sortBy="category"
headerText="Category" footerText=""
filterMatchMode="contains">
<h:outputText value="#{log.category}" />
</p:column>
<p:column id="messageColumn" filterBy="message" sortBy="message"
headerText="Message" footerText="" filterMatchMode="contains">
<h:outputText value="#{log.message}" />
</p:column>
</p:dataTable>
</p:outputPanel>
</h:form>
</h:body>
</html>
TableBean:
package com.rausch.logreader;
import java.io.Serializable;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.model.SelectItem;
import org.apache.log4j.Logger;
import org.primefaces.model.StreamedContent;
@ViewScoped
@ManagedBean(name = "tableBean")
public class TableBean implements Serializable {
transient Logger logging = Logger.getLogger("com.rausch.logreader.TableBean");
private final static String[] level;
private SelectItem[] levelOptions;
private List<Log> filteredLogs;
public List<Log> logsSmall;
private List<String> listFolder;
private Log selectedLog;
private Log[] selectedLogs;
private StreamedContent fileDownload;
private String logFile;
public String getLogFile() {
return logFile;
}
public void setLogFile(String logFile) {
this.logFile = logFile;
}
static {
level = new String[5];
level[0] = "DEBUG";
level[1] = "INFO";
level[2] = "WARN";
level[3] = "ERROR";
level[4] = "FATAL";
}
public TableBean() {
levelOptions = createLevelOptions(level);
}
public StreamedContent getFileDownload() {
return fileDownload;
}
public void setFileDownload(StreamedContent fileDownload) {
this.fileDownload = fileDownload;
}
public Log getSelectedLog() {
return selectedLog;
}
public void setSelectedLog(Log selectedLog) {
this.selectedLog = selectedLog;
}
public List<Log> getFilteredLogs() {
return filteredLogs;
}
public void setFilteredLogs(List<Log> filteredCars) {
this.filteredLogs = filteredCars;
}
public void setLogsSmall(List<Log> logsSmall) {
this.logsSmall = logsSmall;
}
public List<Log> getLogsSmall(){
return logsSmall;
}
public void setListFolder(List<String> listFolder) {
this.listFolder = listFolder;
}
public List<String> getListFolder(){
return listFolder;
}
private SelectItem[] createLevelOptions(String[] data) {
SelectItem[] options = new SelectItem[data.length + 1];
options[0] = new SelectItem("", "Select");
for(int i = 0; i < data.length; i++) {
options[i + 1] = new SelectItem(data[i], data[i]);
}
return options;
}
public SelectItem[] getLevelOptions() {
return levelOptions;
}
}
FileUploadController:
package com.rausch.logreader;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import org.apache.log4j.Logger;
@ManagedBean(name = "fileUploadController")
@SessionScoped
public class FileUploadController {
//implement log4j
Logger logging = Logger.getLogger("com.rausch.logreader.FileUploadController");
TableBean tableBean = new TableBean();
public TableBean getTableBean() {
return tableBean;
}
public void setTableBean(TableBean tableBean) {
this.tableBean = tableBean;
}
private String directory = System.getProperty("catalina.base") + "/logs";
private File f = new File(directory);
public FileUploadController(){
getFolderContent();
}
public final void getFolderContent(){
List<String> listFolder = new ArrayList<String>();
File[] files = f.listFiles();
if (files != null) {
for (int i = 0; i < files.length; i++) {
if (!(files[i].isDirectory())) {
listFolder.add(files[i].getName());
}
}
tableBean.setListFolder(listFolder);
}
}
public void readFile(String file){
try
{
String filePath = System.getProperty("catalina.base") + "/logs/" + file;
List<Log> logsSmall = new ArrayList<Log>();
String sCurrentLine;
BufferedReader br = new BufferedReader(new FileReader(filePath));
//String output = "";
String datetime = "";
String level = "";
String category = "";
String message = "";
while ((sCurrentLine = br.readLine()) != null) {
//Diverse uninteresting Parsing
//Add to the table-list
logsSmall.add(new Log(datetime, level, category, message));
//System.out.println(output);
}
}
tableBean.setLogsSmall(logsSmall);
tableBean.setLogFile(file);
} catch (IOException e) {
logging.error(e.getMessage());
}
}
}
FileDownloadController:
package com.rausch.logreader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;
@ManagedBean(name = "fileDownloadController")
@SessionScoped
public class FileDownloadController {
TableBean tableBean = new TableBean();
public TableBean getTableBean() {
return tableBean;
}
public void setTableBean(TableBean tableBean) {
this.tableBean = tableBean;
}
private StreamedContent fileDownload;
String logFile = tableBean.getLogFile();
public String getLogFile() {
return logFile;
}
public void setLogFile(String logFile) {
this.logFile = logFile;
}
public void setDownload(DefaultStreamedContent download) {
this.fileDownload = download;
tableBean.setFileDownload(fileDownload);
}
public FileDownloadController() throws FileNotFoundException {
File file = new File(logFile);
InputStream input = new FileInputStream(file);
ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
setDownload(new DefaultStreamedContent(input, externalContext.getMimeType(file.getName()), file.getName()));
}
public StreamedContent getFile() {
return fileDownload;
}
public void setFile(String file){
this.logFile = file;
}
}
例外是:
com.sun.faces.mgbean.ManagedBeanCreationException: Klasse com.rausch.logreader.FileDownloadController kann nicht instanziiert werden.
at com.sun.faces.mgbean.BeanBuilder.newBeanInstance(BeanBuilder.java:191)
at com.sun.faces.mgbean.BeanBuilder.build(BeanBuilder.java:100)
at com.sun.faces.mgbean.BeanManager.createAndPush(BeanManager.java:409)
at com.sun.faces.mgbean.BeanManager.create(BeanManager.java:269)
at com.sun.faces.el.ManagedBeanELResolver.resolveBean(ManagedBeanELResolver.java:244)
at com.sun.faces.el.ManagedBeanELResolver.getValue(ManagedBeanELResolver.java:116)
at com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176)
at com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203)
at org.apache.el.parser.AstIdentifier.getValue(AstIdentifier.java:72)
at org.apache.el.parser.AstValue.getValue(AstValue.java:161)
at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:185)
at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:109)
at org.primefaces.component.filedownload.FileDownloadActionListener.processAction(FileDownloadActionListener.java:53)
at javax.faces.event.ActionEvent.processListener(ActionEvent.java:88)
at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:818)
at javax.faces.component.UICommand.broadcast(UICommand.java:300)
at javax.faces.component.UIData.broadcast(UIData.java:1108)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:646)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:724)
Caused by: java.lang.NullPointerException
at java.io.File.<init>(File.java:277)
at com.rausch.logreader.FileDownloadController.<init>(FileDownloadController.java:50)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at java.lang.Class.newInstance(Class.java:374)
at com.sun.faces.mgbean.BeanBuilder.newBeanInstance(BeanBuilder.java:186)
... 41 more
答案 0 :(得分:1)
我认为问题出在FileDownloadController
:
TableBean tableBean = new TableBean();
// [...]
String logFile = tableBean.getLogFile();
// [...]
public FileDownloadController() throws FileNotFoundException {
File file = new File(logFile);
您创建了一个新的TableBean
,并且从不在其上调用setLogFile()
。因此,tableBean.getLogFile()
会返回null
,当您创建file
时,logFile
将为null
,并且NullPointerException
中会有File
{1}}构造函数。
答案 1 :(得分:0)
尝试切割FileDownloadController
构造函数中的代码,并将其粘贴到另一个带有@PostContruct
注释的方法中。
@PostContruct
public void initBean()
{
File file = new File(logFile);
InputStream input = new FileInputStream(file);
ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
setDownload(new DefaultStreamedContent(input, externalContext.getMimeType(file.getName()), file.getName()));
}