How can i get value from <h:selectManyCheckbox> - java

I have one collection , which I'm displaying in my .xhtml page by <h:dataTable>. I want to reach a few goals: at first I want to set list.check value by <h:selectManyCheckbox>, at second I want whatever the values remains selected until the end of current session. Now It displays correctly, but when I select some value it doesn't transmit it in list.check property. I'm using JSF v.2.2.
Code in JSF bean:
private List<AnswerDTO> answerListDto;
//getters and setters
Code in .xhtml
<h:form>
<h:dataTable value="#{main.answerListDto}" var="list">
<h:column>
<h:selectManyCheckbox value="#{list.check}">
<f:selectItem itemValue="1" itemLabel="#{list.ansValue}" />
</h:selectManyCheckbox>
</h:column>
</h:dataTable>
</h:form>
AnswerDTO class:
public class AnswerDTO implements Serializable, DTO {
private static final long serialVersionUID = 1L;
private Integer id;
private Question questId;
private String ansValue;
private String ansStatus;
private String check;
//getters and setters
}

Strange use of selectManyCheckbox I must say. Concrete problem is that value of selectManyCheckbox must be an array.
My opinion is that selectBooleanCheckbox should be used instead of selectManyCheckbox. Your check property can have only two values, an empty array (if checkbox is not selected), and array of length 1 with value which is equal to ansValue (if checkbox is selected).

Related

pass form values in managed bean

i want to pass my form value into my managed bean in order to process it but i always got a null value when i try to retrieve the value in my action method
My bean
#ManagedBean(name="datas")
#SessionScoped
public class Datas implements Serializable {
private static final long serialVersionUID = 1L;
private String ID_primary;
public Datas(){
}
public Datas (String ID_primary){
this.ID_primary=ID_primary;
}
public String getID_primary() {
return ID_primary;
}
public void setID_primary(String ID_primary) {
this.ID_primary= ID_primary;
}
public void process() {
Map<String, String> request=FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
String value = request.get("ID_primary"); // always return null
System.out.println("ID" + value);
}
}
My form
<h:form>
<div align="left">
<h:inputText id="ID_primary" name="ID_primary" value="#{datas.ID_primary}" />
</div>
..........
<div align="right">
<h:commandButton action="#{datas.process()}" value="Create" type="submit" />
</div>
</h:form>
Thank you very much for your help
You have two different form elements, when you click commandButton in the bottom form, values from the top form are not send with the request.
You can have tags like div inside a form, so you can make on big form with divs inside if it is the reason why you have have split them.
Also add #ManagedProperty annotation to ID_primary field:
public class Datas implements Serializable {
private static final long serialVersionUID = 1L;
#ManagedProperty(value = "#{Datas.ID_primary}")
private String ID_primary;
[..]
EDIT:
Ok, my bad, I have not looked at the code carefully. In ManagedBean with ManagedProperty this property will be set automatically by JSF, so you can read it like this:
public void process() {
System.out.println("ID" + getID_primary());
}

Getting results for questionnaire JSF

I have just one idea as realize getting user's answers. I decide use List<String> questionsWithAnswers and put in this list pairs like '1_1', '1_2', '1_3', '2_1'. Something like this. But even with this idea I have a problem. And also important I'll use web-service (it's a constraint for using map for example as structure for saving pairs question and answer).
******** My bean ********
public class TestBean implements Serializable{
private static final long serialVersionUID = 1L;
private List<Test> testList;
private List<Answer> answerList;
private List<Subject> subjectList;
private Map<Question, List<QuestionAnswer>> mapQestionsWithAnswers;
private List<String> questionAnswerList;
private Long subjectId = 0L;
private Test test;
//...
}
getTest.xhtml
<c:forEach items="#{test.testBean.mapQestionsWithAnswers}"
var="entry">
<h:inputHidden value="#{entry.key.questionId}" />
<h:outputText value="#{entry.key.question}" rendered="#{not empty entry.value}"/>
<h:selectManyCheckbox value="#{test.testBean.questionAnswerList}" layout="pageDirection">
<f:selectItems value="#{entry.value}" var="ans"
itemValue="#{fnc:concat(ans.answer.answerId, entry.key.questionId)}"
itemLabel="${ans.answer.answer}" />
</h:selectManyCheckbox>
</c:forEach>
<h:commandButton value="#{msgs['page.content.passtest']}" action="#{test.passTest}" />
For concatination questionId with answerId I use concat. I find this in Concatenate strings in JSF/JSP EL and Javascript
But I cant get value entry.key.questionId inside itemValue="#{fnc:concat(ans.answer.answerId, entry.key.questionId)}"
I don't understand why.
Where can I make a mistake? And I want to ask some source on more logic and simple decision similar problem. Thanks
You don't need to concatenate :
itemValue="#{fnc:concat(ans.answer.answerId, entry.key.questionId)}"
could be as simple as :
itemValue="#{ans.answer.answerId}, #{entry.key.questionId}"
Why not using your own method to concatenate? You can use a bean named concatenatorBean having method
String customConcat(String str1, String str2) {
return str1+"_"+str2;
}
Then in getTest.xhtml you can use your own concatenator to solve the problem.

How do I set a String variable using inputText in JSF?

I have a flow.xml file that's calling a method on a bean. I just want to have a string that I get from a textbox passed in as a parameter to the method. I've added a h:inputText to my xhtml page, but I can't get it to set a String variable in the corresponding flow. What is the simplest possible way I can get a value from a textbox into a flowscope String variable?
public class SomeBean{
String val;
//getters & setters
public String foo(){
System.out.println(val);
return "SUCCESS";
}
}
xhtml
<h:form>
<h:inputtext value="#{someBean.val}"/>
<h:commandButton action="#{someBean.foo}"/>
</h:form>
as html it will render a html form with a textbox and a submit button on licking button it will call foo() and text from txtbox will get binded to val
MyBean myBean = (MyBean) facesContext.getApplication().createValueBinding("#{flowScope.myBean}").getValue(facesContext);
String val = myBean.GetBeanProperty();
Here you should
more examples.

Why input (for example h:inputText) nested in h:dataTable do not update Bean model? [duplicate]

This question already has an answer here:
Using <h:dataTable><h:inputText> on a List<String> doesn't update model values
(1 answer)
Closed 6 years ago.
I have jsf page:
....
<form jsfc="h:form" action="">
<h:dataTable value="#{newMusician.strings}" var="preferredMusicGenre" id="musicGenresSelectTable">
<h:column>
<h:inputText value="#{preferredMusicGenre}" immediate="true"/>
</h:column>
</h:dataTable>
<p>
<input type="submit" jsfc="h:commandButton" value="Add" action="#{newMusician.saveNewMusician}"/>
</p>
</form>
....
And managed bean that has ArrayList of Strings:
#ManagedBean
#ViewScoped
public class NewMusician {
private ArrayList<String> strings = new ArrayList<String>();
public NewMusician() {
strings.add("olo");
}
public ArrayList<String> getStrings() {
return strings;
}
public void saveNewMusician() {
.....
}
....
}
Problem: When I change text in and press save button, in saveNewMusician() method I can see that ArrayList "strings" contain the same old value "olo", but not that one I inserted in input field.
The same problem if use h:selecOneMenu.
Situation is changed if use not string, but object that aggregate string and set value into string.
So if I'll use some POJO and change inputText to:
<h:inputText value="#{preferredMusicGenrePojo.string}" immediate="true"/>
Everything becomes Ok.
Question:
Why usage of 1 level getter <h:inputText value="#{preferredMusicGenre}"/> is incorrect, but usage of 2 level getter: <h:inputText value="#{preferredMusicGenrePojo.text}"/> is Ok?
A String is immutable. It doesn't have a setter for the value. You need to wrap this around in a bean (or POJO as you call it).
public class Musician {
private String preferredGenre;
// Add/generate constructor, getter, setter, etc.
}
Then change your managed bean as follows.
#ManagedBean
#ViewScoped
public class NewMusician {
private ArrayList<Musician> musicians = new ArrayList<Musician>();
public NewMusician() {
musicians.add(new Musician("olo"));
}
public ArrayList<Musician> getMusicians() {
return musicians;
}
public void saveNewMusician() {
// ...
}
// ...
}
And your datatable:
<h:dataTable value="#{newMusician.musicians}" var="musician">
<h:column>
<h:inputText value="#{musician.preferredGenre}" />
</h:column>
</h:dataTable>

JSF/SEAM: How to pre-select checkboxes in a form

I have a form where i have to preselect some checkboxes. How is that possible with jsf/seam? In plain html you would just add the "checked" (or checked="checked") as attribute to the checkboxes. But with f:selectItems I have no clue...also the Object "SelectItem" does not provide any setter for this...
You need to preset them in the property behind the component's value attribute as you usually do for every UIInput component. You can do that in the bean's constructor or initialization block.
Here's a basic example:
<h:selectManyCheckbox value="#{bean.selectedItems}">
<f:selectItems value="#{bean.selectItems}" />
</h:selectManyCheckbox>
Bean:
private List<String> selectedItems; // +getter +setter.
private List<SelectItem> selectItems; // +getter.
public Bean() {
// Preset the selected items.
this.selectedItems = new ArrayList<String>();
this.selectedItems.add("valueToBePreselected1");
this.selectedItems.add("valueToBePreselected2");
// Those values should be exactly the same as one of the SelectItem values.
// I.e. the Object#equals() must return true for any of them.
}
Populate the property you use in "value" before rendereing the page (for example using a phase listener)
<h:selectManyCheckbox value="#{selectManyCheckBoxBean.selectedItems}">
<f:selectItem itemLabel="India" itemValue="India" />
<f:selectItem itemLabel="China" itemValue="China" />
<f:selectItem itemLabel="Germany" itemValue="Germany" />
<f:selectItem itemLabel="USA" itemValue="USA" />
</h:selectManyCheckbox>

Categories

Resources