Problems with JSF selectOneMenu onchange
Hello guys!I got a problem with a JSF h:selectOneMenu onchange.
The idea is that the user choose a category for system parameters and then get a couple of textfields for the systemparameters to edit.
How ever my first approach was with just Strings and List<String> and it worked fine.
How ever when I put it in the target environment that, I think just uses JSF 1.2, I got an error saying that I could not use String, I must use SelectItem.
So back to the drawingboard and I change it all to use SelectItems and List<SelectItem>.
But now the onchange does not work.
I hope you guys can help me out.
First my dependencies:
[code]<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>2.1.7</version>
</dependency>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-impl</artifactId>
<version>2.1.7</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
</dependency>
<!-- Tomcat 6 need this -->
<dependency>
<groupId>com.sun.el</groupId>
<artifactId>el-ri</artifactId>
<version>1.0</version>
</dependency>[/code]
Then the idea is that this backingbean (method: setPresentCategory) should be called when the user change the select see below. But it does not get called:
[code]
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
public class SystemParameterManager {
private final SelectItem[] parameters1 = { new SelectItem("a", "A"),
new SelectItem("b", "B"), new SelectItem("c", "C") };
private final SelectItem[] parameters2 = { new SelectItem("d", "D"),
new SelectItem("e", "E"), new SelectItem("f", "F") };
private final SelectItem[] parameters3 = { new SelectItem("g", "G"),
new SelectItem("h", "H") };
private List<SelectItem> categories = Arrays.asList(new SelectItem[] {
new SelectItem("parameters1", "parameters1"),
new SelectItem("parameters2", "parameters2"),
new SelectItem("parameters3", "parameters3") });
private List<SelectItem> presentParameters = Arrays.asList(parameters2);
private SelectItem presentCategory = categories.get(1);
public String save() {
System.out.println("SAVE: ");
Map<String, String> requestParams = FacesContext.getCurrentInstance()
.getExternalContext().getRequestParameterMap();
Set<String> keySet = requestParams.keySet();
for (SelectItem selectItem : presentParameters) {
for (String key : keySet) {
if (selectItem.getLabel().equals(key)) {
selectItem.setValue((String) requestParams.get(key));
System.out.println("SAVING: " + selectItem.getValue());
break;
}
}
}
return "success";
}
public List<SelectItem> getCategories() {
return categories;
}
public void setCategories(List<SelectItem> categories) {
this.categories = categories;
}
public List<SelectItem> getPresentParameters() {
return presentParameters;
}
public void setPresentParameters(List<SelectItem> presentParameters) {
this.presentParameters = presentParameters;
}
public SelectItem getPresentCategory() {
return presentCategory;
}
public void setPresentCategory(SelectItem presentCategory) {
System.out.println("SETPRESENTCATEGORY: " + presentCategory);
if (presentCategory.getLabel().equals("parameters1")) {
setPresentParameters(Arrays.asList(parameters1));
this.presentCategory = categories.get(0);
} else if (presentCategory.getLabel().equals("parameters2")) {
setPresentParameters(Arrays.asList(parameters2));
this.presentCategory = categories.get(1);
} else if (presentCategory.getLabel().equals("parameters3")) {
setPresentParameters(Arrays.asList(parameters3));
this.presentCategory = categories.get(2);
}
}
}
[/code]
The page is this xhtml-file
[code]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets">
<body>
<div>
<f:view>
<h:form id="SPF" prependId="false">
<h:outputText value="Select your system group" />
<h:selectOneMenu id="presentCategory" value="#{SPM.presentCategory.value}" onchange="document.getElementById('SPF').submit();">
<f:selectItems value="#{SPM.categories}" var="selectItem" itemValue="#{selectItem.value}" itemLabel="#{selectItem.label}" />
</h:selectOneMenu>
<br/>
<h:dataTable value="#{SPM.presentParameters}" var="selectItem">
<h:column>
#{selectItem.label}
</h:column>
<h:column>
<input type="text" name="#{selectItem.label}" value="#{selectItem.value}" />
</h:column>
</h:dataTable>
<h:commandButton action="#{SPM.save}" value="Spara" id="submitButton" />
</h:form>
</f:view>
</div>
</body>
</html>
[/code]
And the faces-config got this tags for the backing bean:
[code]
<managed-bean>
<managed-bean-name>SPM</managed-bean-name>
<managed-bean-class>jsf_test.manager.SystemParameterManager</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
[/code]
So if you guys got any ideas how to reload the page when the select gets changed, please let me know.
Best regards
Fredrik
Btw I'm from sweden feel free th answer in danish.