JSF selectOneMenu的onChange事件以更新一个对象。

huangapple go评论71阅读模式
英文:

JSF selectOneMenu onChange to update an object

问题

我使用了答案中的代码从<https://stackoverflow.com/questions/16207479/how-to-include-file-from-external-local-disk-file-system-folder-in-jsf>获得了以下内容:

JSF

...
&lt;ui:define name = &quot;content&quot;&gt;
    &lt;h:form&gt; 
        &lt;span class=&quot;dataSpan&quot; style=&quot;border-width:0px&quot;&gt;
            &lt;object id=&quot;thePdf&quot; data=&quot;#{request.contextPath}/my.pdf&quot; type=&quot;application/pdf&quot; width=&quot;1150&quot; height=&quot;620&quot;&gt;
                &lt;a href=&quot;#{request.contextPath}/my.pdf&quot;&gt;Download file.pdf&lt;/a&gt;
            &lt;/object&gt;
        &lt;/span&gt;
    &lt;/h:form&gt;
            
    &lt;h:form class=&quot;standardFont&quot;&gt;  
        &lt;span class=&quot;notesSpan&quot; style=&quot;border-width:0px&quot;&gt;
            &lt;p:panel header=&quot;Data Entry&quot;&gt;
                        
                &lt;h:panelGrid columns=&quot;1&quot; border=&quot;0&quot; styleClass=&quot;form-grid&quot; columnClasses=&quot;form-column-label,form-column-input&quot;&gt;

                &lt;h:outputLabel /&gt;
                &lt;h:outputLabel id=&quot;fileName&quot; styleClass=&quot;centerBoldRed&quot; value=&quot;#{pdfServlet.fileName}&quot; &gt;
                &lt;/h:outputLabel&gt;
                &lt;h:outputLabel /&gt;
                &lt;h:outputLabel for=&quot;fileNameList&quot;&gt;Files:&lt;/h:outputLabel&gt;
                &lt;h:selectOneMenu id=&quot;fileNameList&quot; value=&quot;#{dataEntryBean.fileNameList}&quot; styleClass=&quot;boldRed&quot;&gt;
                    &lt;f:selectItems value=&quot;#{dataEntryBean.fileNameList}&quot; var=&quot;file&quot; itemValue=&quot;#{file}&quot; itemLabel=&quot;#{file}&quot; /&gt;
                &lt;/h:selectOneMenu&gt;                          
                &lt;h:message class=&quot;error&quot; for=&quot;fileNameList&quot; id=&quot;fileNameListError&quot; /&gt;

                &lt;/h:panelGrid&gt;
...

Java - PdfServlet

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        
        String s = &quot;my.pdf&quot;;
        File file = new File(&quot;//Temp/input/my/pdfs/IncomingPdf/&quot; + s);
        response.setHeader(&quot;Content-Type&quot;, getServletContext().getMimeType(file.getName()));
        response.setHeader(&quot;Content-Length&quot;, String.valueOf(file.length()));
        response.setHeader(&quot;Content-Disposition&quot;, &quot;inline; filename=\&quot;&quot; + file.getName()+ &quot;\&quot;&quot;);
        Files.copy(file.toPath(), response.getOutputStream());
    }

DataEntryBean

...
    public List&lt;String&gt; getFileNameList() {
        return fileNameList;
    }

    public final void setFileNameList() {
                
        File folder = new File(&quot;//Temp/input/my/pdfs/IncomingPdf/&quot;);
        FilenameFilter pdfFileFilter = (File dir, String name) -&gt; {
            return name.endsWith(&quot;.pdf&quot;);
        };

        File[] files = folder.listFiles(pdfFileFilter);
        try {
            for(File f : files) {
                fileNameList.add(f.getName());
            }
        } catch (ArrayIndexOutOfBoundsException ex) {
            fileNameList.add(&quot;No PDF file was found.&quot;);
        }
    }       
...

这个方案很好地运作。PDF文件在查看器中打开,selectOneMenu显示了目录中的所有文件名。

因此,我的问题是:
如何从selectOneMenu中更改/选择一个名称,并使该文件在object中打开?

我认为我必须使用selectOneMenu中的itemValue作为PdfServlet的参数,并将其用于s中的硬编码值,但我不确定如何做到这一点。任何建议将不胜感激。TIA。

英文:

I used the code from the answer in <https://stackoverflow.com/questions/16207479/how-to-include-file-from-external-local-disk-file-system-folder-in-jsf> to come up with:

JSF

...
&lt;ui:define name = &quot;content&quot;&gt;
    &lt;h:form&gt; 
        &lt;span class=&quot;dataSpan&quot; style=&quot;border-width:0px&quot;&gt;
            &lt;object id=&quot;thePdf&quot; data=&quot;#{request.contextPath}/my.pdf&quot; type=&quot;application/pdf&quot; width=&quot;1150&quot; height=&quot;620&quot;&gt;
                &lt;a href=&quot;#{request.contextPath}/my.pdf&quot;&gt;Download file.pdf&lt;/a&gt;
            &lt;/object&gt;
        &lt;/span&gt;
    &lt;/h:form&gt;
            
    &lt;h:form class=&quot;standardFont&quot;&gt;  
        &lt;span class=&quot;notesSpan&quot; style=&quot;border-width:0px&quot;&gt;
            &lt;p:panel header=&quot;Data Entry&quot;&gt;
                        
                &lt;h:panelGrid columns=&quot;1&quot; border=&quot;0&quot; styleClass=&quot;form-grid&quot; columnClasses=&quot;form-column-label,form-column-input&quot;&gt;

                &lt;h:outputLabel /&gt;
                &lt;h:outputLabel id=&quot;fileName&quot; styleClass=&quot;centerBoldRed&quot; value=&quot;#{pdfServlet.fileName}&quot; &gt;
                &lt;/h:outputLabel&gt;
                &lt;h:outputLabel /&gt;
                &lt;h:outputLabel for=&quot;fileNameList&quot;&gt;Files:&lt;/h:outputLabel&gt;
                &lt;h:selectOneMenu id=&quot;fileNameList&quot; value=&quot;#{dataEntryBean.fileNameList}&quot; styleClass=&quot;boldRed&quot;&gt;
                    &lt;f:selectItems value=&quot;#{dataEntryBean.fileNameList}&quot; var=&quot;file&quot; itemValue=&quot;#{file}&quot; itemLabel=&quot;#{file}&quot; /&gt;
                &lt;/h:selectOneMenu&gt;                          
                &lt;h:message class=&quot;error&quot; for=&quot;fileNameList&quot; id=&quot;fileNameListError&quot; /&gt;

                &lt;/h:panelGrid&gt;
...

Java - PdfServlet

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        
        String s = &quot;my.pdf&quot;;
        File file = new File(&quot;//Temp/input/my/pdfs/IncomingPdf/&quot; + s);
        response.setHeader(&quot;Content-Type&quot;, getServletContext().getMimeType(file.getName()));
        response.setHeader(&quot;Content-Length&quot;, String.valueOf(file.length()));
        response.setHeader(&quot;Content-Disposition&quot;, &quot;inline; filename=\&quot;&quot; + file.getName()+ &quot;\&quot;&quot;);
        Files.copy(file.toPath(), response.getOutputStream());
    }

DataEntryBean

...
    public List&lt;String&gt; getFileNameList() {
        return fileNameList;
    }

    public final void setFileNameList() {
                
        File folder = new File(&quot;//Temp/input/my/pdfs/IncomingPdf/&quot;);
        FilenameFilter pdfFileFilter = (File dir, String name) -&gt; {
            return name.endsWith(&quot;.pdf&quot;);
        };

        File[] files = folder.listFiles(pdfFileFilter);
        try {
            for(File f : files) {
                fileNameList.add(f.getName());
            }
        } catch (ArrayIndexOutOfBoundsException ex) {
            fileNameList.add(&quot;No PDF file was found.&quot;);
        }
    }       
...

And this works great. The PDF file is opened in the viewer and the selectOneMenu displays all of the file names from the directory.

So my question is this:
How can I change/select a name from the selectOneMenu and have that file get opened in the object?

I think I have to use the itemValue from the selectOneMenu as a parameter to PdfServlet and use that instead of the hardcoded value in s but I'm not sure how to do that. Any suggestions would be appreciated. TIA.

答案1

得分: 0

这是我所做的事情...不确定是否是正确的方法,但它实现了我想要的效果...

在JSF中添加(位于顶部):

<f:metadata>
    <f:viewAction action="#{dataEntryBean.onLoad()}" />
    <f:viewAction action="#{pdfServlet.setFileName(dataEntryBean.fileName)}" />
</f:metadata>

并在fileNameListError之前添加以下内容:

<h:commandButton value="提交" action="#pdfServlet.setFileName(dataEntryBean.fileName)}"/>

然后在PdfServlet中,将以下内容更改为:

String s = "my.pdf";

改为:

private static String s;

添加到DataEntryBean中:

public void onLoad() {
    setFileNameList();
    this.fileName = this.fileNameList.get(0);
}

正如我所说,我不确定这是否是百分之百正确的方法,但它确实起作用。如果有任何更新或更正使其变得更好,将不胜感激。谢谢。

英文:

So here's what I did...not sure if it's the correct way, but it does what I was looking for...

Added to JSF (at top):

&lt;f:metadata&gt;
    &lt;f:viewAction action=&quot;#{dataEntryBean.onLoad()}&quot; /&gt;
    &lt;f:viewAction action=&quot;#{pdfServlet.setFileName(dataEntryBean.fileName)}&quot; /&gt;
&lt;/f:metadata&gt;		

And added this before fileNameListError:

&lt;h:commandButton value=&quot;Submit&quot; action=&quot;#pdfServlet.setFileName(dataEntryBean.fileName)}&quot;/&gt;

Then in PdfServlet I changed

String s = &quot;my.pdf&quot;;

to

private static String s;

Added to DataEntryBean:

    public void onLoad() {
        setFileNameList();
        this.fileName = this.fileNameList.get(0);
    }

As I said, I'm not sure if this is the 100% correct way to do this but it does work. Any updates or corrections that make this better would be appreciated. Thanks.

huangapple
  • 本文由 发表于 2020年9月18日 02:43:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/63944390.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定