通过“获取”参数不起作用,参数在链接中不可见 [英] Passing "get" parameters doesn't work, parameter not visible in the link
问题描述
我是JSF的初学者,我想在我的未来网站上编写一个小搜索栏。 我做了两页: index .xhtml
和 search.xhtml
,我尝试从 index.xhtml
to search.xhtml
,所以我做了这个小小的形式:
<! - index.xhtml - >
< h:form id =Form_search>
< h:inputText class =search_bar_textbinding =#{se}>< / h:inputText>
< h:button class =search_bar_buttonoutcome =search>
< f:param name =searchvalue =#{se.value}/>
< / h:按钮>
< / h:表格>
总而言之,我想将inputText的内容发送到search.xhtml
但是有一个问题:当我点击提交按钮时,没有参数被传递,所以没有 /search.xhtml?search=foobar
我只有 /search.xhtml
。
我也试过这个,但这不起作用
<! - index.xhtml - >
< h:form id =Form_search>
< h:inputText class =search_bar_textbinding =#{se}>< / h:inputText>
< / h:按钮>
< / h:表格>
有人可以向我解释这个问题的原因,以及我可以如何解决它?
< f:param value>
和< ; h:按钮结果>
在渲染HTML输出的过程中被评估,而不是像你期望的那样在提交表单期间评估。请注意,这里实际上没有提交表单的方式。如果您有能力阅读 HTML代码,则应该在JSF生成的代码中看到它HTML输出,您可以通过右键单击,在webbrowser中查看源代码。
将其修复为真正的GET表单。您不需要< h:form>
,< h:inputText>
,也不需要< h:button>
在这里。你不想要一个POST表单。你似乎不想将输入绑定到一个bean属性。
< form id =form_searchaction =search.xhtml> ;
< input name =searchclass =search_bar_text/>
< input type =submitclass =search_bar_button/>
< / form>
是的,您可以在JSF中使用纯HTML。
如果真的, 出于某种原因需要使用JSF组件,那么您也可以使用此POST-redirect-GET-with-view-params技巧。 p>
首先将它添加到 index.xhtml
和 search.xhtml
:
< f:元数据>
< f:viewParam name =searchvalue =#{bean.search}/>
< / f:元数据>
然后使用以下表单:
< h:form id =form_search>
< h:inputText value =#{bean.search}styleClass =search_bar_text/>
< / h:表格>
如果您打算对它使用JSF验证,这可能会有意义。但即使如此,这并不妨碍最终用户使用无效参数手动打开URL。您最好在 search.xhtml
中为< f:viewParam>
本身添加验证。
另见:
- 什么可以< f:元数据> ;,< f:viewParam>和< f:viewAction>用于?(滚动至答案底部)
- 如何在页面加载时在后台bean中处理GET查询字符串URL参数?
I'm a beginner to JSF and I want to code a little searchbar on my future website.
I made two pages : index.xhtml
and search.xhtml
, and I try to pass get parameters from index.xhtml
to search.xhtml
, so I made this little formular :
<!-- index.xhtml -->
<h:form id="Form_search">
<h:inputText class="search_bar_text" binding="#{se}"></h:inputText>
<h:button class="search_bar_button" outcome="search">
<f:param name="search" value="#{se.value}" />
</h:button>
</h:form>
To summarize, I want to send the content of an inputText to search.xhtml
But there's a problem : when I click on the submit button, no parameters are passed, so instead of having /search.xhtml?search=foobar
I only have /search.xhtml
.
I also tried this, but this doesn't work either :
<!-- index.xhtml -->
<h:form id="Form_search">
<h:inputText class="search_bar_text" binding="#{se}"></h:inputText>
<h:button class="search_bar_button" outcome="search.xhtml?search=#{se.value}">
</h:button>
</h:form>
Can someone explain to me the reason of this problem and how I can fix it?
The <f:param value>
and <h:button outcome>
are evaluated during rendering the HTML output, not during "submitting" of the form as you seem to expect. Do note that there's actually no means of a form submit here. If you're capable of reading HTML code, you should see it in the JSF-generated HTML output which you can see via rightclick, View Source in webbrowser.
Fix it to be a true GET form. You don't need a <h:form>
, <h:inputText>
, nor <h:button>
here at all. You don't want a POST form. You don't seem to want to bind the input to a bean property. You don't want a plain navigation button.
<form id="form_search" action="search.xhtml">
<input name="search" class="search_bar_text" />
<input type="submit" class="search_bar_button" />
</form>
Yes, you can just use plain HTML in JSF.
If you really, really need to use JSF components for this purpose for some reason, then you could also use this POST-redirect-GET-with-view-params trick.
First add this to both index.xhtml
and search.xhtml
:
<f:metadata>
<f:viewParam name="search" value="#{bean.search}" />
</f:metadata>
Then use this form:
<h:form id="form_search">
<h:inputText value="#{bean.search}" styleClass="search_bar_text" />
<h:commandButton styleClass="search_bar_button" action="search?faces-redirect=true&includeViewParams=true" />
</h:form>
This would perhaps make sense if you intend to use JSF validation on it. But even then, this doesn't prevent endusers from manually opening the URL with invalid params. You'd then better add validation to <f:viewParam>
itself on search.xhtml
.
See also:
- What can <f:metadata>, <f:viewParam> and <f:viewAction> be used for? (scroll to bottom of answer)
- How do I process GET query string URL parameters in backing bean on page load?
这篇关于通过“获取”参数不起作用,参数在链接中不可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!