How to download a xml file with content from inputtextarea?
我正在尝试使用primefaces组件下载XML文件。这部分工作正常,但我的页面上有一个inputextarea,我希望将我在inputextarea中写入的文本写入下载的XML文件中。开发人员能帮我吗?谢谢您。
我的看法是:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | <!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> File Download </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(); } </h:body> </html> |
我的豆:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | @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 2 3 4 5 6 7 8 9 10 11 12 13 | 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; } |