如何从xhtml中的commandButton重定向到Servlet [英] how to redirect to a Servlet from a commandButton in xhtml
问题描述
我已经麻烦了几天了.希望这会更清洁,以前做过此事的人可以帮助我!
I've been having this trouble for a couple of days. hopefully this is cleaner and someone who's done this before can help me out!
我正在Tomcat 7.0服务器上运行的Eclipse中使用JSF 2.0构建WebApp.并做了一个我想去的servlet,但是我不知道如何分配导航规则来带我去那里:
I'm building a webApp with JSF 2.0 in Eclipse running on a tomcat 7.0 server. and have made a servlet I want to go to, but I don't know how to thandle the navigation rules to take me there:
在我的项目中,路径为src/com/servlets/PdfServlet,因此,当我在服务器上运行项目时,可以通过localhost:8080/miloWeb/PdfServlet来访问该servlet. servlet实际上会为我创建一个Pdf文件并显示它.
In my project the path is src/com/servlets/PdfServlet so when I run the project on the server this servlet can be accessed through the url localhost:8080/miloWeb/PdfServlet. the servlet actualy creates a Pdf file for me and displays it.
无论如何,我的xhtml都有:
anyway my xhtml has this:
<h:commandButton id="reportButton" action="reportPdf" styleClass="button" value="get Report" ></h:commandButton>
我的faces-config导航规则如下:
my faces-config navigation rules look like this:
<navigation-rule>
<navigation-case>
<from-outcome>reportPdf</from-outcome>
<to-view-id>/miloWeb/PdfServlet</to-view-id>
<redirect />
</navigation-case>
</navigation-rule>
但是我需要带我去miloWeb/faces/miloWeb/PdfServlet,并且我需要带我去miloWeb/PdfServlet.
but it takes me to miloWeb/faces/miloWeb/PdfServlet, and I need it to take me to miloWeb/PdfServlet.
关于如何完成此操作,其他路线或执行操作的任何想法?
Any ideas on how to accomplish this, or another route or doing this ?
推荐答案
首先,导航规则仅适用于JSF.他们无法帮助您导航到servlet.
First of all, the navigation rule is for JSF only. They can't help you to navigate to a servlet.
faces-config中的导航规则在JSF2.0中是多余的.它们由JSF隐式处理,基于操作方法返回的值(结果).可以将"?faces-redirect=true"
附加到结果的末尾以进行重定向.
The navigation rules in faces-config are redundant in JSF2.0. They are implicitly handled by JSF, based on the value (outcome) returned by an action method. "?faces-redirect=true"
can be attached to the end of the outcome for redirection.
我不知道您的功能要求是什么.您正在尝试将JSF和servelts混合使用,这不是一个好主意.
I don't know what your functional requirement is. You are trying to mix JSF and servelts, which is not a very good idea.
<h:commandButton id="reportButton" action="reportPdf"
action
属性的值必须是解析为某些JSF托管bean中的方法的方法表达式.例如action="#{controllerBean.axnMethod()}"
.
The value of the action
attribute must be a method expression that resolves to a method in some JSF managed bean. e.g. action="#{controllerBean.axnMethod()}"
.
调用axnMethod()
时,一种选择是从那里重定向到servlet.如下:
When the axnMethod()
is called, one option is to redirect from there to the servlet. The is something as follows:
FacesContext.getCurrentInstance().getExternalContext().redirect("url");
或者,要求只是调用servlet,然后在视图上,只需放置一个带有调用servlet的url的链接.
Or, requirement is just to invoke the servlet then on the view, just put a link with the url that invokes the servlet.
这篇关于如何从xhtml中的commandButton重定向到Servlet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!