I would like to know if there is a way to set a default value to a inputText, or inputHidden or any other tag that can be recovered in the backing bean when the page shows.
I would like something like this (this code doesn't work):
<h:inputText id="companyName" value="#{loginController.companyName}" defaultValue="123456">
And in the backing bean:
private String companyName;
#PostConstruct
public void init() {
System.out.println("CompanyName=" + companyName);
}
So that it shows "CompanyName=123456" in the console.
I need to define the default value in the page itself, I don't want to put the default value in the backing bean.
The real problem is that I need to find a way to pass a value to the backing bean defined in the page. I have many pages and I want to define a 'mode' in each page to be shown differently, and this 'mode' needs to be read by the backing bean when the page displays (before any submit)
I've somewhat solved the problem by showing what I want to show in certain mode, using an h:panelGroup and an EL expression to set the value to the bean:
<h:panelGroup rendered="#{loginController.companyName('123456')}" >
...
</h:panelGroup>
Thanks everybody for your comments
Related
I would like to set a value of bean from another bean once a page is displayed. The target dataTable is included because it is also used in another context.
Like I said before, there are two beans involved:
bean1 'gets' a parameter, will load an object and display some properties.
bean2 is the bean that is responsible for the filtering / search in the dataTable.
relevant parts:
<f:metadata>
<f:viewParam name="objectId" value="#{bean1.objectId}" />
</f:metadata>
<!-- resolving works -->
#{bean1.object.name}
included search:
<p:dataTable>...<p:inputText value="#{bean2.value}">...</p:dataTable>
How to assign (a substring of) bean1.object.name to the value of the input text once at page request but keep the existing value attribute of the field? I don't want to mess up the included page but would prefer to solve it "outside" in my including jsf/xhtml file.
The best way to do this is using javascript.
Define a input hidden tag to hold the value of #{bean1.object.name}.
Now in javascript get the value of this field using document.getElementById("").value
Take the substring and assign to input text field using javascript.
I have a page where I want to include a part of the page (footer in this instance) dependant on values given from a view parameter.
I have my ViewScoped backing bean initializing on preRenderView
<f:metadata>
<f:viewParam name="racecode" value="#{displayResults.racecode}" />
<f:event type="preRenderView" listener="#{displayResults.init}" />
</f:metadata>
This queries the database to get the name of the footer to be included. This then, is used in this fashion :
<h:panelGroup id="customFooter" display="block">
<ui:include src="#{displayResults.customFooter}" />
</h:panelGroup>
This always gives me a missing page. But if I enter the page name manually it works. Same if I replace the ui:include with an h:outputText.
I understand that it has something to do with the phases of JSF and that at the time the ui:include is done, the value is not set yet. (reading up and better understanding the phases is something on my TODO list).
The question remains. How can I get something of the sort done. Have a bean use the viewParam, query the database and use that value in a ui:include?
#wemu has already explained the cause. The <ui:include src> is evaluated before init() method is called. His proposed <f:phaseListener> solution is however clumsy.
Just use #ManagedProperty/#PostConstruct on a #RequestScoped bean.
#ManagedProperty("#{param.racecode}")
private String racecode;
#PostConstruct
public void init() {
// ...
}
PreRenderView listeners are called within the RenderResponsePhase, before components are rendered BUT AFTER the TagHandlers are called. This means that TagHandlers will NOT see data initialized within a PreRenderView event.
If you are using a <ui:include value="#{myBean.myViewId}" /> to dynamically switch an include you can't use a PreRenderView event listener to set the myViewId property of myBean.
If you need to do that use a <f:phaseListener>.
This question already has answers here:
Setting the value for jsf InputSecret during page load from the managed bean
(2 answers)
Closed 6 years ago.
becomes empty automatically even if i keep value in the back bean for it.
I have already set the value for password in inputsecret variable in the back bean in jsf2 than also the input secret is being shown empty.
This is indeed the default behaviour of <h:inputSecret> due to security reasons. You can get the value to redisplay anyway by setting the redisplay attribute to true.
<h:inputSecret value="#{bean.password}" redisplay="true" />
See also its view declaration language documentation (emphasis mine)
Encode Behavior
Render the clientId of the component as the value of the "name" attribute. Render the current value of the component as the value of the "value" attribute, if and only if the "redisplay" component attribute is the string "true". If the "styleClass" attribute is specified, render its value as the value of the "class" attribute.
This is part of xhtml page:
<h:form>
<h:inputSecret value="#{login.password}" />
</h:form>
This is backing bean:
#Component //Spring component
public class Login{
private String password;
public Login(){
password="12341fsf"; //any value you want to set
}
}
I tested this code it works fine
Is it possible to pass parameter from a JSP page to a JSF page's backing 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.
P.S. When I add comment and I put #anyname when someone replies, #namyname part is getting truncated.
Update 1
To get the selected value from JSP to bean I did a crude approach.
I added the the following in JSP
String str = request.getParameter("selectname");
and assigned string str to a hidden field
<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.
E.g. First time when I selects 1 and value returned in bean is null, second time when I selects 2, value returned is 1.
How could I get the currently selected value in my 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}" />
Otherwise, you still can perhaps, but I'd suggest that you make your popup also a JSF page. JSF and JSP don't coexist well. If you really must retain the situation, then you can try to emulate a JSF POST request to the target jsf URL.
f:viewParam lets you associate bean
properties with request parameters
–
-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
Sending data from non-JSF forms to JSF pages
Make results pages results pages bookmarkable
This is a new feature in JSF 2.0
example:
<f:viewParam name="fg" value="#{colorPreferences.foreground}" />
If the “fg” parameter is non-null, it is passed to
setForeground before the page is rendered
<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¶m2=bar, then “foo” and “bar” are passed to “setProp1” and “setProp2” before the page is rendered. If any of the parameters are null (i.e., no such request parameter exists), then the associated setter is not called at all, and the bean has its normal value for that property
You can find the answer from the JSF tutorial http://www.coreservlets.com/JSF-Tutorial/jsf2/
I need to determine the ID of a form field from within an action handler. The field is a part of a included facelets component and so the form will vary.
included.xhtml
<ui:component>
<h:inputText id="contained_field"/>
<h:commandButton actionListener="#{backingBean.update}" value="Submit"/>
</ui:component>
example_containing.xhtml
<h:form id="containing_form">
<ui:include src="/included.xhtml"/>
</h:form>
How may I determine the ID of the form in the update method at runtime? Or better yet, the ID of the input field directly.
Bind the button to your backing bean, then use getParent() until you find the nearest form.
Programmatically I would use jsight's method. You can know the id of your elements (unless you let JSF create them, I don't know the means for numbering in the ids) by looking at it. h:form is a naming container so as long as you don't have it wrapped in another naming container it will be containingForm:containedfield The ':' is the naming separator by default is JSF and the ids are created like this, roughly anyway, (parentNamingContainerId:)*componentId
Since update method is of type actionListener, you can access your UI component as follows
public void update(javax.faces.event.ActionEvent ac) {
javax.faces.component.UIComponent myCommand = ac.getComponent( );
String id = myCommand.getId(); // get the id of the firing component
..... your code .........
}