如何从xhtml中的commandButton重定向到Servlet

Myy*_*Myy 3 java jsf servlets

我已经有几天麻烦了.希望这更干净,之前完成此任务的人可以帮助我!

我正在使用运行在tomcat 7.0服务器上的Eclipse中的JSF 2.0构建webApp.并且已经创建了一个我想去的servlet,但我不知道如何解决导航规则带我去那里:

在我的项目中,路径是src/com/servlets/PdfServlet,所以当我在服务器上运行项目时,可以通过url localhost:8080/miloWeb/PdfServlet访问这个servlet.servlet实际上为我创建了一个Pdf文件并显示它.

无论如何我的xhtml有这个:

<h:commandButton id="reportButton"  action="reportPdf" styleClass="button" value="get Report" ></h:commandButton>
Run Code Online (Sandbox Code Playgroud)

我的faces-config导航规则如下所示:

<navigation-rule>
    <navigation-case>
        <from-outcome>reportPdf</from-outcome>      
        <to-view-id>/miloWeb/PdfServlet</to-view-id>
        <redirect />
    </navigation-case>
</navigation-rule>
Run Code Online (Sandbox Code Playgroud)

但它需要我到miloWeb/faces/miloWeb/PdfServlet,我需要它带我去miloWeb/PdfServlet.

关于如何完成这个或其他路线或这样做的任何想法?

Bhe*_*ung 6

首先,导航规则仅适用于JSF.他们无法帮助您导航到servlet.

faces-config中的导航规则在JSF2.0中是多余的.它们由JSF隐式处理,基于action方法返回的值(结果)."?faces-redirect=true"可以附加到结果的末尾以进行重定向.

我不知道你的功能要求是什么.您正在尝试混合使用JSF和servelts,这不是一个好主意.

<h:commandButton id="reportButton"  action="reportPdf"
Run Code Online (Sandbox Code Playgroud)

action属性的值必须是解析为某个JSF托管bean中的方法的方法表达式.例如action="#{controllerBean.axnMethod()}".

axnMethod()被调用时,一个选择是从那里重定向到servlet.具体如下:

FacesContext.getCurrentInstance().getExternalContext().redirect("url");
Run Code Online (Sandbox Code Playgroud)

或者,要求只是在视图上调用servlet,只需将一个链接与调用servlet的url放在一起.