问题描述
我仍然在学习JSF的道路上。我有一个IceFaces树与IceFaces commandLink尝试下载文件。到目前为止,这是我的xhtml和我的支持bean。当我点击commandLink它只打印两个消息,然后它什么都不做,它不显示任何警告任何错误...如何知道发生了什么?我缺少什么?干杯
XHTML
<?xml version ='1.0'encoding ='UTF-8'?>
<!DOCTYPE html PUBLIC - // W3C // DTD XHTML 1.0 Transitional // ENhttp://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:ice =http://www.icesoft.com/icefaces/component>
< h:head>
< title> Facelet标题< / title>
< / h:head>
< h:body>
< h:form>
< ice:tree id =tree
value =#{treeBean.model}
var =item
hideNavigation =false
hideRootNode =false
imageDir =./ images />
< ice:treeNode>
< f:facet name =icon>
< ice:panelGroup style =display:inline>
< h:graphicImage value =#{item.userObject.icon}/>
< / ice:panelGroup>
< / f:facet>
< f:facet name =content>
< ice:panelGroup style =display:inline>
< ice:commandLink action =#{treeBean.doDownload(item.userObject.fileAbsolutePath)}>
< ice:outputText value =#{item.userObject.text}/>
< / ice:commandLink>
< / ice:panelGroup>
< / f:facet>
< / ice:treeNode>
< / ice:tree>
< / h:form>
< / h:body>
< / html>
BEAN
@ManagedBean
@ViewScoped
public class TreeBean实现Serializable {
private final DefaultTreeModel model;
/ **创建一个新的TreeBean实例* /
public TreeBean(){
//创建其子节点扩展
DefaultMutableTreeNode rootTreeNode = new DefaultMutableTreeNode();
IceUserObject rootObject = new IceUserObject(rootTreeNode);
rootObject.setText(根节点);
rootObject.setExpanded(true);
rootObject.setBranchContractedIcon(./ images / tree_folder_close.gif);
rootObject.setBranchExpandedIcon(./ images / tree_folder_open.gif);
rootObject.setLeafIcon(./ images / tree_document.gif);
rootTreeNode.setUserObject(rootObject);
//模型由冰:tree组件通过getter方法访问
model = new DefaultTreeModel(rootTreeNode);
//为(int i = 0; i {
DefaultMutableTreeNode branchNode = new DefaultMutableTreeNode();
FileSourceUserObject branchObject = new FileSourceUserObject(branchNode);
branchObject.setText(SteveJobs.jpg);
branchObject.setFileAbsolutePath(/ Users / BRabbit / Downloads / SteveJobs.jpg);
branchObject.setBranchContractedIcon(./ images / tree_folder_close.gif);
branchObject.setBranchExpandedIcon(./ images / tree_folder_open.gif);
branchObject.setLeafIcon(./ images / tree_document.gif);
branchObject.setLeaf(true);
branchNode.setUserObject(branchObject);
rootTreeNode.add(branchNode);
}
}
public DefaultTreeModel getModel(){
return model;
}
public void doDownload(String fileAbsolutePath){
System.out.println(fileAbsolutePath);
文件文件=新建文件(fileAbsolutePath);
if(file.exists())
System.out.println(是); //它存在!
FacesContext facesContext = FacesContext.getCurrentInstance();
ExternalContext externalContext = facesContext.getExternalContext();
externalContext.setResponseHeader(Content-Type,externalContext.getMimeType(file.getName()));
externalContext.setResponseHeader(Content-Length,String.valueOf(file.length()));
externalContext.setResponseHeader(Content-Disposition,attachment; filename = \+ file.getName()+\);
InputStream input = null;
OutputStream output = null;
try {
input = new FileInputStream(file);
output = externalContext.getResponseOutputStream();
IOUtils.copy(input,output);
} catch(FileNotFoundException ex){
Logger.getLogger(TreeBean.class.getName())。log(Level.SEVERE,null,ex);
} catch(IOException ex){
Logger.getLogger(TreeBean.class.getName())。log(Level.SEVERE,null,ex);
} finally {
IOUtils.closeQuietly(output);
IOUtils.closeQuietly(input);
}
facesContext.responseComplete();
}
}
Bean的对象
public class FileSourceUserObject extends IceUserObject {
String fileAbsolutePath;
public FileSourceUserObject(DefaultMutableTreeNode wrapper){
super(wrapper);
}
public String getFileAbsolutePath(){
return fileAbsolutePath;
}
public void setFileAbsolutePath(String fileAbsolutePath){
this.fileAbsolutePath = fileAbsolutePath;
}
}
最后我做到了!
这是一些代码,允许用户看到一个树(文件)并下载它们。
XHTML
< ice:tree id =tree
value =#{treeBean.model}
var = item
hideNavigation =false
hideRootNode =false
imageDir =./ images />
< ice:treeNode>
< f:facet name =icon>
< ice:panelGroup style =display:inline>
< h:graphicImage value =#{item.userObject.icon}/>
< / ice:panelGroup>
< / f:facet>
< f:facet name =content>
< ice:panelGroup style =display:inline-block>
< ice:outputResource resource =#{item.userObject.resource}
fileName =#{item.userObject.text}
shared =false/>
< / ice:panelGroup>
< / f:facet>
< / ice:treeNode>
< / ice:tree>
BACKING BEAN
public class TreeBean实现Serializable {
private final DefaultTreeModel model; $ $ $ $ $ $ $ $ $ $ $
public TreeBean(){
DefaultMutableTreeNode rootTreeNode = new DefaultMutableTreeNode();
FileSourceUserObject rootObject = new FileSourceUserObject(rootTreeNode);
rootObject.setText(根节点);
rootObject.setExpanded(true);
rootObject.setBranchContractedIcon(./ images / tree_folder_close.gif);
rootObject.setBranchExpandedIcon(./ images / tree_folder_open.gif);
rootTreeNode.setUserObject(rootObject);
//模型由冰:tree组件通过getter方法访问
model = new DefaultTreeModel(rootTreeNode);
ExternalContext ec = FacesContext.getCurrentInstance()。getExternalContext();
//为(int i = 0; i {
DefaultMutableTreeNode branchNode = new DefaultMutableTreeNode();
FileSourceUserObject branchObject = new FileSourceUserObject(branchNode);
branchObject.setText(Test.jpg);
branchObject.setResource(新SRCResource(/< filePath> /Test.jpg));
branchObject.setBranchContractedIcon(./ images / tree_folder_close.gif);
branchObject.setBranchExpandedIcon(./ images / tree_folder_open.gif);
branchObject.setLeafIcon(./ images / tree_document.gif);
branchObject.setLeaf(true);
branchNode.setUserObject(branchObject);
rootTreeNode.add(branchNode);
}
}
public DefaultTreeModel getModel(){
return model;
}
}
SRCResource
public class SRCResource implements Resource {
private String fileAbsolutePath;
private final日期lastModified;
public SRCResource(String fileAbsolutePath){
this.fileAbsolutePath = fileAbsolutePath;
this.lastModified = new Date();
}
@Override
public String calculateDigest(){
returnNo localcularéjamás!!;
}
@Override
public InputStream open()throws IOException {
return(InputStream)(new FileInputStream(fileAbsolutePath));
}
@Override
public Date lastModified(){
return lastModified;
}
@Override
public void withOptions(Options optns)throws IOException {
}
public String getFileAbsolutePath(){
return fileAbsolutePath;
}
public void setFileAbsolutePath(String fileAbsolutePath){
this.fileAbsolutePath = fileAbsolutePath;
}
}
Web.xml
添加以下
< servlet>
< servlet-name>资源Servlet< / servlet-name>
< servlet-class> com.icesoft.faces.webapp.CompatResourceServlet< / servlet-class>
< load-on-startup> 1< / load-on-startup>
< / servlet>
< servlet-mapping>
< servlet-name>资源Servlet< / servlet-name>
< url-pattern> / xmlhttp / *< / url-pattern>
< / servlet-mapping>
< servlet-mapping>
< servlet-name> Faces Servlet< / servlet-name>
< url-pattern> / icefaces / *< / url-pattern>
< / servlet-mapping>
就这样!只需根据需要调整代码!
干杯!
有关
I'm still on the road of learning JSF. I have an IceFaces tree with an IceFaces commandLink to try to download a file. So far this is my xhtml and my backing bean. When I click the commandLink it just prints the two messages and then it does nothing and it does not show any warning any error at all... How to know what's happening? What am I missing?
Cheers
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:ice="http://www.icesoft.com/icefaces/component">
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
<h:form>
<ice:tree id="tree"
value="#{treeBean.model}"
var="item"
hideNavigation="false"
hideRootNode="false"
imageDir="./images/">
<ice:treeNode>
<f:facet name="icon">
<ice:panelGroup style="display: inline">
<h:graphicImage value="#{item.userObject.icon}"/>
</ice:panelGroup>
</f:facet>
<f:facet name="content">
<ice:panelGroup style="display: inline">
<ice:commandLink action="#{treeBean.doDownload(item.userObject.fileAbsolutePath)}">
<ice:outputText value="#{item.userObject.text}"/>
</ice:commandLink>
</ice:panelGroup>
</f:facet>
</ice:treeNode>
</ice:tree>
</h:form>
</h:body>
</html>
BEAN
@ManagedBean
@ViewScoped
public class TreeBean implements Serializable {
private final DefaultTreeModel model;
/** Creates a new instance of TreeBean */
public TreeBean() {
// create root node with its children expanded
DefaultMutableTreeNode rootTreeNode = new DefaultMutableTreeNode();
IceUserObject rootObject = new IceUserObject(rootTreeNode);
rootObject.setText("Root Node");
rootObject.setExpanded(true);
rootObject.setBranchContractedIcon("./images/tree_folder_close.gif");
rootObject.setBranchExpandedIcon("./images/tree_folder_open.gif");
rootObject.setLeafIcon("./images/tree_document.gif");
rootTreeNode.setUserObject(rootObject);
// model is accessed by by the ice:tree component via a getter method
model = new DefaultTreeModel(rootTreeNode);
// add some child nodes
for (int i = 0; i < 3; i++) {
DefaultMutableTreeNode branchNode = new DefaultMutableTreeNode();
FileSourceUserObject branchObject = new FileSourceUserObject(branchNode);
branchObject.setText("SteveJobs.jpg");
branchObject.setFileAbsolutePath("/Users/BRabbit/Downloads/SteveJobs.jpg");
branchObject.setBranchContractedIcon("./images/tree_folder_close.gif");
branchObject.setBranchExpandedIcon("./images/tree_folder_open.gif");
branchObject.setLeafIcon("./images/tree_document.gif");
branchObject.setLeaf(true);
branchNode.setUserObject(branchObject);
rootTreeNode.add(branchNode);
}
}
public DefaultTreeModel getModel() {
return model;
}
public void doDownload(String fileAbsolutePath) {
System.out.println(fileAbsolutePath);
File file = new File(fileAbsolutePath);
if(file.exists())
System.out.println("Yes"); //It exists !
FacesContext facesContext = FacesContext.getCurrentInstance();
ExternalContext externalContext = facesContext.getExternalContext();
externalContext.setResponseHeader("Content-Type", externalContext.getMimeType(file.getName()));
externalContext.setResponseHeader("Content-Length", String.valueOf(file.length()));
externalContext.setResponseHeader("Content-Disposition", "attachment;filename=\"" + file.getName() + "\"");
InputStream input = null;
OutputStream output = null;
try {
input = new FileInputStream(file);
output = externalContext.getResponseOutputStream();
IOUtils.copy(input, output);
} catch (FileNotFoundException ex) {
Logger.getLogger(TreeBean.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(TreeBean.class.getName()).log(Level.SEVERE, null, ex);
} finally {
IOUtils.closeQuietly(output);
IOUtils.closeQuietly(input);
}
facesContext.responseComplete();
}
}
Bean's object
public class FileSourceUserObject extends IceUserObject{
String fileAbsolutePath;
public FileSourceUserObject(DefaultMutableTreeNode wrapper) {
super(wrapper);
}
public String getFileAbsolutePath(){
return fileAbsolutePath;
}
public void setFileAbsolutePath(String fileAbsolutePath){
this.fileAbsolutePath = fileAbsolutePath;
}
}
Finally I did it!Here's some code that allows the user to see a tree (of Files) and download them.
XHTML
<ice:tree id="tree"
value="#{treeBean.model}"
var="item"
hideNavigation="false"
hideRootNode="false"
imageDir="./images/">
<ice:treeNode>
<f:facet name="icon">
<ice:panelGroup style="display: inline">
<h:graphicImage value="#{item.userObject.icon}"/>
</ice:panelGroup>
</f:facet>
<f:facet name="content">
<ice:panelGroup style="display: inline-block">
<ice:outputResource resource="#{item.userObject.resource}"
fileName="#{item.userObject.text}"
shared="false"/>
</ice:panelGroup>
</f:facet>
</ice:treeNode>
</ice:tree>
BACKING BEAN
@ManagedBean
@ViewScoped
public class TreeBean implements Serializable {
private final DefaultTreeModel model;
public TreeBean() {
DefaultMutableTreeNode rootTreeNode = new DefaultMutableTreeNode();
FileSourceUserObject rootObject = new FileSourceUserObject(rootTreeNode);
rootObject.setText("Root Node");
rootObject.setExpanded(true);
rootObject.setBranchContractedIcon("./images/tree_folder_close.gif");
rootObject.setBranchExpandedIcon("./images/tree_folder_open.gif");
rootTreeNode.setUserObject(rootObject);
// model is accessed by by the ice:tree component via a getter method
model = new DefaultTreeModel(rootTreeNode);
ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
// add some child nodes
for (int i = 0; i < 3; i++) {
DefaultMutableTreeNode branchNode = new DefaultMutableTreeNode();
FileSourceUserObject branchObject = new FileSourceUserObject(branchNode);
branchObject.setText("Test.jpg");
branchObject.setResource(new SRCResource("/<filePath>/Test.jpg"));
branchObject.setBranchContractedIcon("./images/tree_folder_close.gif");
branchObject.setBranchExpandedIcon("./images/tree_folder_open.gif");
branchObject.setLeafIcon("./images/tree_document.gif");
branchObject.setLeaf(true);
branchNode.setUserObject(branchObject);
rootTreeNode.add(branchNode);
}
}
public DefaultTreeModel getModel() {
return model;
}
}
SRCResource
public class SRCResource implements Resource {
private String fileAbsolutePath;
private final Date lastModified;
public SRCResource(String fileAbsolutePath) {
this.fileAbsolutePath = fileAbsolutePath;
this.lastModified = new Date();
}
@Override
public String calculateDigest() {
return "No lo calcularé jamás !!";
}
@Override
public InputStream open() throws IOException {
return (InputStream)(new FileInputStream(fileAbsolutePath));
}
@Override
public Date lastModified() {
return lastModified;
}
@Override
public void withOptions(Options optns) throws IOException {
}
public String getFileAbsolutePath() {
return fileAbsolutePath;
}
public void setFileAbsolutePath(String fileAbsolutePath) {
this.fileAbsolutePath = fileAbsolutePath;
}
}
Web.xml
Add the following
<servlet>
<servlet-name>Resource Servlet</servlet-name>
<servlet-class>com.icesoft.faces.webapp.CompatResourceServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Resource Servlet</servlet-name>
<url-pattern>/xmlhttp/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/icefaces/*</url-pattern>
</servlet-mapping>
And that's it ! Simply adapt the code to your needs !Cheers !
More info on http://wiki.icefaces.org/display/ICE/Adding+ICEfaces+to+Your+Application
这篇关于从IceFaces树下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!