Dynamically added component are growing vertically - jsf-2

I am following the option suggested here to dynamically adding inputtext box in my PrimeFaces v3.5 application. The only problem I am facing is that the newly added inputtext are growing vertically. Screenshot is attached.

The <h:dataTable> generates a HTML <table> element wherein each iteration generates a <tr> element which represents naturally a new row.
If you don't want to generate a table at all, then you should not be using a <h:dataTable>, but e.g. <ui:repeat>.
<h:form>
<ui:repeat value="#{bean.items}" var="item">
<h:inputText value="#{item.value}" />
</ui:repeat>
<h:commandButton value="add" action="#{bean.add}" />
<h:commandButton value="submit" action="#{bean.submit}" />
</h:form>
This doesn't generate any markup and the HTML <input> elements will by default end up in a single line.

Related

security in Jsf2 guessing and modifying url should not be allowed

I have an application up and running. I have a page addstudent.xhtml page where the user adds the student details.When user finishes adding and submits he will be redirected displaystudent.xhtml page.
My question is when user directly types on browser
localHost:8081/Student/displaystudent.xhtml. Page is displayed with blank values.This should not happen. How do I prevent this even though he types on browser.
Both the files addstudent.xhtml displaystudent.xhtml pages are in web-inf folder. I am using Jsf2, primefaces 3.5
Make displaystudent.xhtml an include file which you render conditionally in addstudent.xhtml depending on whether the student is successfully added.
E.g., assuming standard JSF:
<h:panelGroup id="addStudent" layout="block">
<h:form rendered="#{empty bean.student.id}">
<h2>Add student</h2>
<h:inputText value="#{bean.student.name}" />
...
<h:commandButton value="Add" action="#{bean.add}">
<f:ajax execute="#form" render=":addStudent" />
</h:commandButton>
</h:form>
<h:panelGroup layout="block" rendered="#{not empty bean.student.id}">
<h2>Added student</h2>
<dl>
<dt>Student name</dt><dd>#{bean.student.name}</dd>
</dl>
</h:panelGroup>
</h:panelGroup>
(simplified; you could of course split off the add student to <ui:include src="/WEB-INF/displaystudent.xhtml"> and so on)
The button, whereby the add() method should return void or null, re-renders the <h:panelGroup id="addStudent">. If the student was successfully been added in the DB, then it should have a non-empty ID. This will cause the form to be hidden and the results panel to be shown.
See also:
Conditionally displaying JSF components
How to navigate in JSF? How to make URL reflect current page (and not previous one)

Value not retained if two different components are rendered using mutually exclusive rendered value

I want to render only one of two different component which will have same value in the bean based a check box. I was able to do so using the rendered attribute but the values are not re-rendered, it gets reflected only when i invoke action. What am i missing here?
<h:selectBooleanCheckbox value="#{bean.mask}">
<f:ajax event="click" listener="#{Bean.mask}" execute="#form" render="#form"/>
</h:selectBooleanCheckbox>
<h:inputSecret id="id1" value="#{Bean.value}" rendered="#{Bean.mask}" redisplay="true"/>
<h:inputText id="id2" value="#{Bean.value}" rendered="#{!Bean.mask}"/>
PS: I am able to see the value in the bean. Only thing is the value is not updated in the text box or secret box.

JSF not firing bean setters when using f:ajax

This is an update on a previous post I made regarding conditional rendering of page components. I can now conditionally render different components on the page based on selected user inputs using the f:ajax tag. The trouble is that when I click my commandbutton the inputs that are conditionally rendered using ajax aren't being read on submit. I am guessing it is because the whole page isn't rerendered when I add in the new components and the submit button can't see them, even though they are there. Here is the segment of my form page(it is wrapped in a h:form tag):
<h:outputLabel for="selectPersonType" value="Select Type: "/>
<h:selectOneMenu id="selectPersonType" value="#{addPersonBean.personType}" label="Person Type" required="true">
<f:selectItem itemValue=""/>
<f:selectItems value="#{listBean.personTypes}" />
<f:ajax render="results"/>
</h:selectOneMenu>
<h:message for="selectPersonType" style="color: red"/>
</h:panelGrid>
<h:panelGroup id="results">
<h:panelGroup rendered="#{addPersonBean.personType == 'STUDENT'}">
<h:outputLabel for="selectSchoolType" value="School Type: " />
<h:selectOneMenu id="selectSchoolType" value="#{addPersonBean.schoolType}">
<f:selectItems value="#{listBean.schoolTypes}" />
<f:ajax execute="selectSchoolType"/>
</h:selectOneMenu>
</h:panelGroup>
<h:panelGroup rendered="#{addPersonBean.personType == 'PATIENT'}">
<h:outputLabel for="smoker" value="Smoker? " />
<h:selectBooleanCheckbox id="smoker" value="#{addPersonBean.smoker}">
<f:ajax execute="smoker"/>
</h:selectBooleanCheckbox>
</h:panelGroup>
</h:panelGroup>
<h:commandButton value="Add Person" action="#{addPersonBean.action}"/>
The trouble here is that the components with ids 'selectSchoolType' and 'smoker's values don't get set when I click the commandButton because they are rendered conditionally using the selectPersonType select menu after the page has loaded. Is there a way to fire the components when their values change instead of when I click the submit button (therefore, their values should be processed before I even click submit). As you can see I have tried to use f:ajax with the execute attribute attached to the components in question but it didn't seem to work. (I believe the default events for these components are valueChange so haven't added them to the f:ajax tags, but I have tried 'change' and 'valueChange').
All your <f:ajax> things are inside a conditionally rendered component. This construct will fail when the condition behind rendered attribute evaluates to false during processing the form submit. This will happen when the #{addPersonBean} is in the request scope and doesn't preinitialize the condition behind the rendered attribute during (post)construction, or when it's performing business logic in getters/setters in a wrong way.
Placing the #{addPersonBean} in the view scope and ensuring that you aren't doing business logic in getters/setters should fix this problem.
See also:
commandButton/commandLink/ajax action/listener method not invoked or input value not updated - point 5 applies to you
How to choose the right bean scope?

How can I set numbers within IDs dynamically in JSF

I design a JavaEE website with the help of JSF2 and Facelets. The code beyond is just an example how the JSF-Code looks like.
When the user of the website wants to create new users but forgets to set e.g. the firstname, an error occurs and the element will be highlighted (red color and red frame around the element).
For highlighting I have to use the attribute:
class="#{faceletUtils.getLabelStyleClass('createUsersForm:allUsers:0:firstName')}"
The problem is that I have to set the number 0 dynamically but as far as I know nesting is not allowed in JSF. I could use #{curUser.number} to get the number.
Question: How can I set the numbers in the for and class attributes?
for="createUsersForm:allUsers:0:firstName"
class="#{faceletUtils.getLabelStyleClass('createUsersForm:allUsers:0:firstName')}"
An example of the JSF-Code:
<h:form id="createUsersForm">
<ui:repeat id="allUsers" value="#{createUserController.users}" var="curUser">
<div class="formLine">
<label for="createUsersForm:allUsers:0:firstName" class="#{faceletUtils.getLabelStyleClass('createUsersForm:allUsers:0:firstName')}">
<h:outputText value="#{userStaticText['user.firstName.label']}"/>
</label>
<h:inputText id="firstName" value="#{curUser.firstName}" class="#{faceletUtils.getLabelStyleClass('createUsersForm:allUsers:0:firstName')}"/>
</div>
</ui:repeat>
</h:form>
Best regards,
Jana
Just use <h:outputLabel> instead of <label>. JSF will automatically use the right for value. Further you can use #{component.clientId} to get the client ID of the current component (although I think the particular functional requirement of highlighting invalid fields can be solved in a much simpler manner).
<h:form id="createUsersForm">
<ui:repeat id="allUsers" value="#{createUserController.users}" var="curUser">
<div class="formLine">
<h:outputLabel for="firstName" styleClass="#{faceletUtils.getLabelStyleClass(component.clientId)}"
value="#{userStaticText['user.firstName.label']}"/>
<h:inputText id="firstName" value="#{curUser.firstName}" styleClass="#{faceletUtils.getLabelStyleClass(component.clientId)}"/>
</div>
</ui:repeat>
</h:form>
Note that I fixed the wrong class attribute of <h:inputText> as well.

how to render selected checkbox value in jsf when i select checkbox

i have renderd checkboxes. when i select checkbox that checkbox value shoud be displayed in panel.i have written code for this but not worked
<h:selectManyCheckbox id="chkedition" value="#{newspaperBean.selectedEditions}" layout="lineDirection" styleClass="nostyle">
<f:selectItems value="#{newspaperBean.searchEditionByNewspaper()}" var="item" itemLabel="#{item.editionName}" itemValue="#{item.editionName}"/>
<f:ajax render="display" event="select"/>
</h:selectManyCheckbox>
</td>
</tr>
<tr>
<td colspan="2">
<p:panel id="display" header="You have selected">
<c:forEach items="#{newspaperBean.selectedEditions}" var="it">
<h:outputText value="#{it}"/><br/>
</c:forEach>
</p:panel>
</td>
</tr>
this worked when i submit form but i want that when i select checkbox.
Remove the event="select" attribute. This is the incorrect event to hook on. It's only triggered when you select any text in input fields and textareas, not when you click the checkbox.
<f:ajax render="display" />
The default event is already valueChange which will resolve to click in checkboxes and this is exactly what you need. You can if necessary explicitly specify it by event="click", but as said, it's the default value already, so just omit it.
Another potential problem is the <c:forEach> in the panel. This will fail if the bean is view scoped because it runs during view build time and get its own separate instance of the view scoped bean which may not contain the submitted data. You'd need to use <ui:repeat> instead.
<ui:repeat value="#{newspaperBean.selectedEditions}" var="it">
<h:outputText value="#{it}"/><br/>
</ui:repeat>
See also:
JSTL in JSF2 Facelets... makes sense?

Resources