简体   繁体   English

如何从inputtextarea下载包含内容的xml文件?

[英]How to download a xml file with content from inputtextarea?

I am trying to download a xml file using primefaces components. 我正在尝试使用primefaces组件下载xml文件。 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. 这部分正在工作,但是我的页面上有一个inputtextarea,我希望将在inputtextarea中编写的文本写入下载的xml文件中。 Could a developer help me ? 开发人员可以帮助我吗? Thank you. 谢谢。

my view : 我的观点 :

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

My bean : 我的豆:

@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;
}

}

Few remarks 几句话

  1. Your p:inputTextarea should be inside the h:form element 您的p:inputTextarea应该位于h:form元素内
  2. Your bean's mytext property must have a getter (ok) AND a setter (missing!) 您的bean的mytext属性必须具有一个getter (确定)和一个setter (丢失!)
  3. Your InputStream code comes from a PF example that returns the content of a resource picture file. 您的InputStream代码来自PF示例,该示例返回资源图片文件的内容。 You just want to create a stream from a string! 您只想从字符串创建流! Ask yourself How do I turn a String into a Stream in java? 问问自己如何在Java中将字符串转换为流?
  4. The InputStream must be created on the fly because of the changing text (ie inside getFile instead of constructor) 由于文本的更改,必须动态创建InputStream (即在getFile而不是构造函数中)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM