如何从inputtextarea下载含有内容的xml文件? [英] How to download a xml file with content from inputtextarea?

查看:106
本文介绍了如何从inputtextarea下载含有内容的xml文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用面组件下载一个xml文件。这部分是工作,但我的页面上有一个inputtextarea,我想要在我下载的xml文件中写入的inputtextarea中的文本。开发人员可以帮我吗?谢谢。

I am trying to download a xml file using primefaces components. This part is working but I have on my page a inputtextarea, and I would like to have the text that I write in the inputtextarea written in the xml file that is downloaded. Could a developer help me ? Thank you.

我的观点:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
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>File Download</title>      
</h:head>
<h:body>
    <p:dialog modal="true" widgetVar="statusDialog" header="Status" draggable="false" closable="false" resizable="false">
        <p:graphicImage value="/images/loading11.gif" />          
    </p:dialog>

    <p:inputTextarea id ="mytheinput"  value="#{fileDownloadView.mytext}" cols="115" autoResize="true" rows="20"  />  

    <h:form>
        <p:commandButton value="Download" ajax="false" onclick="PrimeFaces.monitorDownload(start, stop);" icon="ui-icon-arrowthick-1-s">
            <p:fileDownload value="#{fileDownloadView.file}" />
        </p:commandButton>
    </h:form>

<script type="text/javascript">
function start() {
PF('statusDialog').show();
}

function stop() {
PF('statusDialog').hide();
}
</script>


</h:body>
</html>

我的豆:

@ManagedBean(name="fileDownloadView")
public class FileDownloadView {

private StreamedContent file;
private String mytext;

public FileDownloadView() {  
    InputStream stream = ((ServletContext)FacesContext.getCurrentInstance().getExternalContext().getContext()).getResourceAsStream(mytext);
    file = new DefaultStreamedContent(stream, "xml", "yourfile.xml");
}

public StreamedContent getFile() {
    return file;
}

public String getMytext() {
    return mytext;
}

}


推荐答案

少量评论


  1. 您的 p:inputTextarea 应该在 h:form 元素

  2. 你的bean的 mytext 属性必须有一个 getter (ok)AND setter (missing!)

  3. 您的InputStream代码来自PF示例,返回资源图片文件的内容。你只想从字符串创建一个流!问问自己如何将字符串转换为流在java?

  4. 由于文本的更改(即<$ code> InputStream c $ c> getFile 而不是构造函数)

  1. Your p:inputTextarea should be inside the h:form element
  2. Your bean's mytext property must have a getter (ok) AND a setter (missing!)
  3. Your InputStream code comes from a PF example that returns the content of a resource picture file. You just want to create a stream from a string! Ask yourself How do I turn a String into a Stream in java?
  4. The InputStream must be created on the fly because of the changing text (i.e. inside getFile instead of constructor)

有一点帮助 / p>

A little help

public StreamedContent getFile() {
    InputStream stream = new ByteArrayInputStream( mytext.getBytes() );
    StreamedContent file = new DefaultStreamedContent(stream, "xml", "yourfile.xml");
    return file;
}

public String getMytext() {
    return mytext;
}

public void setMytext(String mytext) {
    this.mytext = mytext;
}

这篇关于如何从inputtextarea下载含有内容的xml文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆