I have recently come across a question involving JSF and Javabeans.
To get a value from an input you need a declared property with getter and setter in the bean. Now I am trying to generate a list of entities to edit a value (in this code example called number), but how can i retrieve these values (as the ID of h:inputText is empty at this point)?
I have tried to name this ID "name_#{bean.id}", but there is no way to set this value in the bean.
Any help would be appreciated!
index.xhtml
<ui:repeat var="entity" value="#{bean.getEntities()}">
<p>
Value:
<h:inputText id="" value="#{entity.number}" />
</p>
</ui:repeat>
Bean.java
public List<Entity> getEntities() {
return entities;
}
You don't need the client id of the inputText to get the values to the server. As stated in Daniel's comment changes are saved if you submit the surrounding form.
You don't even need to set the id parameter. JSF does it for you.
But your value attribute is not correct. It needs to be
value="#{bean.entities}"
Related
Hello dear folks of stack overflow.
I encountered a problem recently in my Struts application.
I have a jsp which displays some bean value correctly (i paste only relevant part of the code, i simplified to the extreme):
<table>
<logic:iterate name="bean" property="list1" id="listItem">
<tr>
<td>
<html:checkbox name="listItem" property="selected">
</html:checkbox>
</td>
</logic:iterate>
</table>
My bean has a list1 property with its getter and setter
private List<RandomObject> list1;
public List getList1() {
return list1;
}
public void setList1(List list1) {
this.list1=list1;
}
and my sub-bean has a selected property:
private boolean selected;
public boolean isSelected() {
return selected;
}
public void setSelected(boolean selected) {
this.selected = selected;
}
Now, when enter this jsp, the values i get are correct, ie: if my mapped object in DB was at selected=true, the checkbox is checked. What i dont get is how do i save the changes i make in this form. When i submit, all the changes are lost. This is pure struts related, because in debug when i enter the StrutsAction linked to submit, the ActionForm i get has already lost all interesting values. Also i feel like it has to do with the fact that the value i want to retain isn't directly stored on the main bean, but rather is a property of a sub-bean, because on the same page there are a lot of other properties directly on the main bean that i have no trouble saving.
What did i miss ?
It's probably declaring incorrect name attributes in the rendered HTML. If you look at the generated source code for your page it probably looks like this:
<input type="checkbox" name="selected" ...>
which would look for a selected field in your form bean class.
If you're using a collection that's a field in your form bean you want to be using the <nested:form>, <nested:iterate> and <nested:checkbox Struts tags, not the <html:form>,and` ones. So it would then look like this:
<nested:form action="foo">
...
<nested:iterate property="list1" id="listItem">
<tr>
<td>
<nested:checkbox property="selected">
</nested:checkbox>
</td>
</tr>
</nested:iterate>
...
</nested:form>
Note that I've removed the name attributes from the Struts tags, because they're not needed (in my experience they actually cause all kinds of problems - if you use <nested:*> tags don't use a name attribute). In the case of the <nested:iterate> the level of nesting is the form bean itself, so it knows that it needs to look for the list1 property in the form bean.
That tag creates its own nesting level, so the <nested:checkbox knows that it needs to look for the selected property of the current element in the iteration.
The rendered HTML will look something like this (for the first element):
<input type="checkbox" name="list1[0].selected" ...>
which means the selected field of the first element (index 0) in the collection referenced by the list1 field of your form bean.
And, of course, you'll need to make sure that you're using a session-scoped form bean, rather than a request-scoped one.
in my case i had to put the name attribute into the iterate otherwise it gives me error(cannot find property into any bean...)
I resolved deleting the attribute name into the checkbox: it seems it create a new object at page scope that is not connected with the form.
This is my code:
<nested:iterate id="apertura" type="it.puglia.innova.view.actionform.AperturaForm" indexId="index" name="strutturaRuraleForm" property="listAperturaForm">
<nested:checkbox styleId="checkbox_${index}" property="flagContinuato" onchange="changeOrarioContinuato(${index})"/>
that's it :-) didn't need to change html:form to nested too.
Bye
I am new to struts2 and looking at the existing code and elsewhere in the net, I thought my below code should work. I am trying to select some user ids in s:select box in JSP and I want to use these ids in my action class. I am using modelDriven for it and users are in a list
JSP code snippet
<s:select id="selectedAgents"
name="selectedUserList"
multiple="true"
list="selectedUserList"
/>
the selected user ids in the page comes to this box. and when the form is submitted, I expect to see the selectUserList in action.
In the action I have
public class WorkLoadReportAction extends GenericAction implements ModelDriven<WorkloadReportDTO>
...
private WorkloadReportDTO userReportInputData = new WorkloadReportDTO();
...
#Override
public WorkloadReportDTO getModel() {
return userReportInputData;
}
the WorkloadReportDTO has List<String> selectedUserList and its getter and setter.
Now in the method of the action(called from submit), I dont see the selectedUserList populated.
What am I missing?
In your Jsp :
<s:select id="selectedAgents"
name="selectedUserList"
multiple="true"
list="selectedUserList"
/>
You have name attribute value and list attribute value as same . Try changing the value of either name attribute or list attribute .
Hope this Solves your problem
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
I have this value ${agact.ppr} that i want to show in my page jsp. It can be shown like this easly:
<c:out value="${agact.ppr}" />
but I want to use it into input form like this:
<form:input type="text" value="${agact.ppr}" path="ppr" />
but it's not working I don't know why. I displayed others attributes in input fields without problem and this one no.
My class is like this :
Class Agent{
int ppr;
/** setters and getters**/
}
Try removing the 'value' attribute, 'path' should be all you need there. Also make sure the getter and setter are valid.
I have a #ModelAttribute account which has a field named title. I need to display this field in my JSP, and also bind it in the next call cycle. If I do this;
Title: ${editAccountForm.account.title} <br/>
It only displays the value. When someone submits the Form in the JSP, account is empty again. How do I get the label to reflect the value, just like a form:input tag?
I tried this:
<form:label path="account.issuer">some text</form:label> <br/>
but it dint work. Please help.
You can put a
<input type="hidden" name="account.title" value="${editAccountForm.account.title}" />
The name attribute must be the same as the one generated by a spring form:input.
but that is the same to displaying it normally ${editAccountForm.account.title}. After that, populate the value in a hidden field. that will update the value in the model