Pass request parameters to jsf method in xhtml file? - jsf-2

i use el-impl-2.2.jar with jsf 2 that allows to call a method from managed bean in xhtml file and pass it params.
if i test this :
#{myBean.findById(1)}
all works fine. but now i want to retrieve params from request
i have tried this
#{myBean.findById(${param.id})}
it's not working.
any solution?

This expression #{myBean.findById(param.id)} should work. You shouldn't search in your view. You should do it in controller. Create ManagedBean and do it there.
Read mkyong article http://www.mkyong.com/jsf2/4-ways-to-pass-parameter-from-jsf-page-to-backing-bean/.

Related

How to pass bean from action class to other jsp page?

It is very simple to do in servlet using request.setAttribute(). But how to do this in Struts2?
How to use that bean in other jsp page?
Please give example code.
As #AndreaLigios mentions in the comments, you'll want to generate getters and setters in your action classes.
In your JSP files, you'll be able to get them by using the <s:property> tag.

using <set> tag in grails

I am new to grails. I came across a set tag which we can use in gsp pages itself to set the values which is similar to setting model from the controller.
<g:set var="home" value="something" />
so that when we write ${home} it outputs "something".
Is there any way to set the value in sessions in gsp pages itself and not from controller using set tag?
Yes you can do it in gsp pages as well. You just have to include an extra attribute scope to indicate which scopes(session, flash, page and request) you are setting the value to.
<g:set var="home" value="something" scope="session" />
If you do not include the scope option then it defaults to page.
To display the the value you just have to write ${session.home} or ${request.home} or simply ${home} for request scope. Hope this helps.
For more : https://grails.github.io/grails-doc/3.0.x/ref/Tags/set.html
Well! above answer suffices the need. Just wanted to add one more thing that gsp pages comprises of jsp internally and hence all the 9 implict objects are available on gsp pages as well.
request HttpServletRequest object
response HttpServletResponse object
out PrintWriter object used to send output to the client.
session HttpSession object
application ServletContext object associated with application context.
config ServletConfig object associated with the page.
pageContext server-specific features JspWriters.
page synonym for this
Exception handling exceptions and error page redirects.An instance of javax.servlet.jsp.JspException
You could at any point of time access these in your gsp pages.
More you can read from this.
Hope it helps!

CDI bean List in datatable is null on submit from JSF

Please note: This question is about CDI scopes as we are using CDI scopes in the app and not JSF scopes.
1) Controller Bean (TestController.java) which is in RequestScoped (enterprise context) is called index_cut.xhtml, when we come for first time on this page.
2) On button “Load”, we load the following method to populate the sapFinancialPeriodList which works fine and displays the data
3) After changing the content on the page and submitting, the sapFinancialPeriodList appears as NULL in the following method –
Any suggestions?
Your bean is request scoped and you're loading the data model on action only instead of on (post)construction. When the HTTP response after the action which loaded the data is finished, then the bean is garbaged. The subsequent request would get a brand new instance of the bean with all properties set to default. However, as the same data model isn't been preserved during (post)construct, it remains empty.
In JSF2 you'd solve this with using #ViewScoped. This way the bean will live as long as you're interacting with the same view by postbacks (which return null or void).
In CDI you'd need to solve this using #ConversationScoped, which in turn requires some additional #Inject Conversation boilerplate, complete with begin() and end() calls at the right moments. For a concrete example, see also What scope to use in JSF 2.0 for Wizard pattern?.
An alternative is to pass the parameters responsible for creating the data model to the subsequent request via <f:param> in the command link/button as follows
<h:commandButton value="save" ...>
<f:param name="period" value="#{bean.period}" />
</h:commandButton>
and then recreate exactly the same data model in (post)constructor of the request scoped bean as follows
String period = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("period");
List<SapFinancialPeriod> sapFinancialPeriodList = someservice.list(period);
(the above is by the way nicer to solve with #ManagedProperty if you were using standard JSF; as far as I know CDI doesn't have an annotation which enables you to set a HTTP request parameter as a bean property)
See also:
How to choose the right bean scope?
Unrelated to the concrete problem, the upcoming JSF 2.2 solves this functional requirement in a nicer way using the new "Faces Flow" feature with the new #FlowScoped annotation and the new xmlns:j="http://java.sun.com/jsf/flow" tags.

How do I set Managed Bean field to the value of a ui:param?

I have a JSF page that is included in other JSF pages (basically a page header, with common information). This common page has its own controller and is reliant that the page which includes this common page pass it some common data.
Specifically, I am currently trying to include this common page on other pages using:
<ui:include src="commonPage.xhtml">
<ui:param name="commonData" value="#{thisPagesController.commonData}"/>
</ui:include>
Which should pass "commonData" to the commonPage.xhtml page and ideally set the "commonData" property on the CommonPageController class:
#ManagedProperty("#{commonData}")
CommonData commonData;
However, this is not working... the managed property is not getting set.
What is the proper way to do this?
See comment from BalusC. There is no standard for this in the JSF API spec. Use a composite or custom component instead.

Pass request parameters through FacesContext

I moved from JSF 1.2 to JSF 2.0 and it seems I missed something during the switch. I have following scenario:
There is a button on one page with actionListener set to one managed bean's method which adds an object to request by calling FacesContext.getCurrentInstance().getExternalContext().getRequestMap().put("foo", fooObject);
Navigation is properly handled to other page where other managed bean is initialized.
The constructor of other managed bean tries to retrieve passed object from request by calling FacesContext.getCurrentInstance().getExternalContext().getRequestMap().get("foo"); and to initialize itself with received values.
Both managed beans are request scoped. I notice that constructor can't retrieve proper value from request because request map doesn't contain "foo" key.
What am I doing wrong? Is there a better way to do this?
Thanks in advance.
In step 2, if there is a redirect the initial request scope is lost as the redirect would result in another request.

Resources