简体   繁体   English

将值从JSP传递到JSF页面支持Bean

[英]Pass Value From JSP To JSF Page Backing Bean

Is it possible to pass parameter from a JSP page to a JSF page's backing bean? 是否可以将参数从JSP页面传递到JSF页面的后备bean?

JSP page is popup window open when I invoke a button in JSF page and my selected value in JSP page, I should be able to pass to JSF's backing bean. 当我在JSF页面中调用按钮并在JSP页面中选择值时,JSP页面是打开的弹出窗口,我应该能够传递到JSF的后备bean。

PS When I add comment and I put @anyname when someone replies, @namyname part is getting truncated. PS:当我添加评论并在有人回复时输入@anyname时,@ namyname部分将被截断。

Update 1 更新1

To get the selected value from JSP to bean I did a crude approach. 为了从JSP到bean获得选定的值,我做了一个粗略的方法。

I added the the following in JSP 我在JSP中添加了以下内容

String str = request.getParameter("selectname");

and assigned string str to a hidden field 并将字符串str分配给隐藏字段

 <input type="hidden" name="hid" value="<%=str%>"  /> 

and in my bean I am getting the value like the following 在我的豆子中,我得到的价值如下

logger.info("jsp value "+FacesContext.getCurrentInstance().getExternalContext()
               .getRequestParameterMap().get("hid"));

This almost works except I always gets the value which I previously selects. 除了我总是得到以前选择的值之外,这几乎可以工作。 Eg First time when I selects 1 and value returned in bean is null, second time when I selects 2, value returned is 1. 例如,第一次选择1且bean中返回的值为null,第二次选择2时返回值为1。

How could I get the currently selected value in my bean? 我如何在bean中获得当前选择的值?

First, if your JSF view technology is JSP, then you can use the <h:> tags in the jsp and it becomes straightforward 0 just add a <h:commandButton action="#{yourBean.yourMethod}" /> 首先,如果您的JSF视图技术是JSP,则可以在jsp中使用<h:>标记,它变得很简单0只需添加<h:commandButton action="#{yourBean.yourMethod}" />

Otherwise, you still can perhaps, but I'd suggest that you make your popup also a JSF page. 否则,您也许仍然可以,但是我建议您将弹出窗口也设置为JSF页面。 JSF and JSP don't coexist well. JSF和JSP不能很好地共存。 If you really must retain the situation, then you can try to emulate a JSF POST request to the target jsf URL. 如果确实必须保留这种情况,则可以尝试将JSF POST请求模拟到目标jsf URL。

f:viewParam lets you associate bean properties with request parameters – f:viewParam使您可以将bean属性与请求参数相关联–

-This introduces several new capabilities -这引入了几个新功能

  • New tags that navigate via GET instead of POST and tags that navigate via GET instead of POST, and send parameters along with the address 通过GET而不是POST导航的新标签和通过GET而不是POST导航的标签,并将参数与地址一起发送
  • Sending data from non-JSF forms to JSF pages 将数据从非JSF表单发送到JSF页面
  • Make results pages results pages bookmarkable 使结果页结果页可添加书签
  • This is a new feature in JSF 2.0 这是JSF 2.0中的新功能
  • example: <f:viewParam name="fg" value="#{colorPreferences.foreground}" /> 示例: <f:viewParam name="fg" value="#{colorPreferences.foreground}" />
  • If the “fg” parameter is non-null, it is passed to setForeground before the page is rendered 如果“ fg”参数为非空值,则在呈现页面之前将其传递给setForeground

     <f:metadata> <f:viewParam name="param1" value="#{bean.prop1}"/> <f:viewParam name="param2" value="#{bean.prop2}"/> </f:metadata> <h:head>…</h:head> <h:body> Blah Blah blah #{bean prop1} , blah, #{bean.prop1} </h:body> 

    If the page is called with page.jsp?param1=foo&param2=bar , then “foo” and “bar” are passed to “setProp1” and “setProp2” before the page is rendered. 如果使用page.jsp?param1=foo&param2=bar调用page.jsp?param1=foo&param2=bar ,则在呈现页面之前, 会将“ foo”“ bar”传递给“ setProp1”“ setProp2” If any of the parameters are null (ie, no such request parameter exists), then the associated setter is not called at all, and the bean has its normal value for that property 如果任何参数为null(即不存在这样的请求参数),则根本不会调用关联的setter,并且Bean具有该属性的常规值。

You can find the answer from the JSF tutorial http://www.coreservlets.com/JSF-Tutorial/jsf2/ 您可以从JSF教程http://www.coreservlets.com/JSF-Tutorial/jsf2/中找到答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM