【发布时间】:2013-01-11 15:12:39
【问题描述】:
我想将以下清单中的“title”参数动态传递给另一个 jsf Facelet,具体取决于 selectOneMenu 的选择。我的第一种方法如下所示:
<h:form id="form">
<p:selectOneMenu value="#{exerciseEditorBean.selectedExerciseType}" >
<f:selectItem itemLabel="Multiple Choice Exercise" itemValue="MultipleChoiceExercise" />
<f:selectItem itemLabel="Vocabulary Test" itemValue="VocabularyTest" />
</p:selectOneMenu>
<h:outputText value="Enter Title of your Exercise: " />
<h:inputText id="title" value="#{exerciseEditorBean.exerciseTitle}" />
<h:commandButton value="Next" action="#{exerciseEditorBean.openEditor()}" />
</h:form>
ExerciseEditorBean 是 ViewScoped。
openEditor() 函数然后由 selectedExerciseType 属性决定接下来要显示哪个 Facelet,并返回类似“multipleChoiceEditor.xhtml”的内容。 现在如何将 title 属性传递给这个 Facelet?
【问题讨论】:
-
JSF 是为基于 HTTP post 请求的导航而设计的。这意味着当你导航到一个页面时,参数也是以这种方式传递的。我非常不喜欢这个事实,因为它也不允许添加书签。我更喜欢基于带有参数化 URL 的 get 请求进行导航。 IMO URL 应该看起来像
/editor/multiplechoice而不是/editor.xhtml或/editor.xhtml?type=multiplechoice。如果你有兴趣,看看 PrettyFaces。 ;-) -
@siebz0r,您可以为使用
<h:button/>或<h:link/>生成的URL 添加书签,并在其中任何一个上方便地使用includeViewParams=true传递参数。它们都使用 commandXXX 组件生成 GET,而不是普通的 POST -
@kolossus 这是否是一个漂亮的解决方案还有待商榷 ;-)
-
在 JSF 中,唯一的 servlet 是
FacesServlet。每次您(错误)输入“servelet”时,您可能都想说“Facelet”。
标签: jsf primefaces navigation parameter-passing