Reputation: 1
I'm new jsf. I fill combo boxes dynamically according to uploaded txt. But when ı want to get selected combo box values. I research it and applied my code but it does not work. Selected string always return null.
<p:dataTable id="cellEditingTable" var="message"
value="#{messageTableController.menuList}" paginator="true"
paginatorPosition="bottom" editable="true" editMode="cell">
<f:facet name="header">
PRE_INSTALL
</f:facet>
<p:column>
<f:facet name="header">
<h:outputText value="Value" />
</f:facet>
<h:outputText value="#{message.value}" />
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="Assign" />
</f:facet>
<p:selectOneMenu value="#{messageTableController.selected}"
style="width:96%">
<f:selectItem itemLabel="#{message.assign}" itemValue="1" />
<f:selectItem itemLabel="#{message.combo}" itemValue="2" />
</p:selectOneMenu>
</p:column>
</p:dataTable>
<br />
<h:commandButton value="Submit Training"
action="#{messageTableController.submitTraining}">
</h:commandButton>
my class
@ManagedBean
@ViewScoped
@SessionScoped
public class MessageTableController implements Serializable {
private static final long serialVersionUID = 20111020L;
private UploadedFile file;
private File f;
private BufferedReader br;
private String line;
private String[] str;
private String selected = "s";
private List<preList> menuList;
public String getSelected() {
return selected;
}
public void setSelected(String selected) {
this.selected = selected;
}
public List<preList> getMenuList() {
return menuList;
}
public void setKullaniciList(List<preList> menuList) {
this.menuList = menuList;
}
public UploadedFile getFile() {
return file;
}
public void setFile(UploadedFile file) {
this.file = file;
}
public void upload() throws IOException {
if (file != null) {
FacesMessage message = new FacesMessage("Succesful", file.getFileName() + " is uploaded.");
FacesContext.getCurrentInstance().addMessage(null, message);
readUploadedFile();
}
}
public void readUploadedFile() throws IOException {
f = new File(file.getFileName());
br = new BufferedReader(new FileReader(f));
menuList = new ArrayList<preList>();
while ((line = br.readLine()) != null) {
if (line.contains("PI")) {
str = line.split("=");
if(str[1].contains("DISABLE")){
menuList.add(new preList(str[0], str[1], "ENABLE"));
}
else if(str[1].contains("ENABLE")){
menuList.add(new preList(str[0], str[1], "DISABLE"));
}
else if(str[1].contains("YES")){
menuList.add(new preList(str[0], str[1], "NO"));
}
else if(str[1].contains("NO")){
menuList.add(new preList(str[0], str[1], "YES"));
}
else{
menuList.add(new preList(str[0], str[1], ""));
}
}
}
br.close();
}
public String submitTraining(){
return "selectedOptions";
}
public class preList {
private String value;
private String assign;
private String combo;
public preList(String value, String assign, String combo) {
this.assign = assign;
this.value = value;
this.combo = combo;
}
//set get methods...
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:p="http://primefaces.org/ui">
<h:outputText value="#{messageTableController.selected}" />
</html>
Output photo When I click submit button empty page is opened
Upvotes: 0
Views: 90
Reputation: 309
Your ManagedBean shouldn't have view and session scope at the same time, it's either or. I haven't tried out what happens when you add both annotations, but it seems like it applies view scope, as the selected value is not there anymore after you navigated to your second xhtml file (it's not the same view -> new instance of your bean). I hope I could help you :)
Upvotes: 1