Reputation: 1862
I am new to jsf. I am implementing "valueChangeListener" in my canteen application. In backing bean I have a map of strings.
I have a dropdown in jsp page and when I select value from dropdown , listner is fired. I want to show corresponding value in text box. but its giving me "setting value == for null converter" error.
when i implemented converter, error changes and now its showing error that "Bean property is not writable".
I am using jsf 1.2. I have seen many posts regarding this but nothing worked.
plz guys help me.....Thanks in advance.
//jsp code
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<f:view>
<h:form id="myform">
<html>
<head>
<body>
<h:outputText value="select chinese" /><h:selectOneMenu id="chinese" value="#{mychinese.chineseName}" onchange="submit()">
<f:valueChangeListener type="backing_Bean.ChineseValueListner" />
<f:selectItems value="#{mychinese.chineseName}" />
</h:selectOneMenu>
<br>
<h:outputText value="Message from server" /><h:inputText value="#{mychinese.chinesemessage}" />
<h:message for="chinese"/>
<body>
<head>
<html>
</h:form>
<f:view>
// backing bean code is as follows
package backing_Bean;
import java.util.LinkedHashMap;
import java.util.Map;
public class MyChinese {
private static Map<String, String> chineseName;
private String chinesemessage="Select-Chinese";
static {
chineseName = new LinkedHashMap<String, String>();
chineseName.put("Select-Chinese", "Chinese");
chineseName.put("Manchurian", "Manchurian");
chineseName.put("Hakka", "Hakka");
chineseName.put("Sezvan", "Sezvan");
chineseName.put("Singapori", "Singapori");
}
public String getChinesemessage() {
return chinesemessage.toString();
}
public void setChinesemessage(String chinesemessage) {
this.chinesemessage = chinesemessage;
}
public Map<String, String> getChineseName() {
return chineseName;
}
public void setChineseName(Map<String, String> chineseName) {
MyChinese.chineseName = chineseName;
}
}
//Listener code
package backing_Bean;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.AbortProcessingException;
import javax.faces.event.ValueChangeEvent;
import javax.faces.event.ValueChangeListener;
public class ChineseValueListner implements ValueChangeListener{
public void processValueChange(ValueChangeEvent event)
throws AbortProcessingException {
MyChinese chinese = (MyChinese) FacesContext.getCurrentInstance()
.getExternalContext().getSessionMap().get("mychinese");
chinese.setChinesemessage(event.getNewValue().toString());
FacesMessage fm = new FacesMessage();
fm.setDetail(chinese.getChinesemessage());
}
}
Upvotes: 0
Views: 268
Reputation: 85779
The problem's that you're using a Map<String, String>
to hold the selected value of your <h:selectOneMenu>
:
<h:selectOneMenu id="chinese" value="#{mychinese.chineseName}" onchange="submit()">
...
</h:selectOneMenu>
In order to solve this, you should bind the value
tag attribute to a String
attribute in your managed bean.
MyChinese class modification:
public class MyChinese {
private static Map<String, String> chineseName;
private String chinesemessage="Select-Chinese";
private String selectedChineseName;
//the getter/setter functions for the new selectedChineseName attribute
//the rest of your code
}
JSP modification:
<h:selectOneMenu id="chinese" value="#{mychinese.selectedChineseName}"
onchange="submit()">
...
</h:selectOneMenu>
Note that by having this, maybe you won't need the valueChangeListener
.
Note that you have problems in your HTML:
<head>
element must not wrap the <body>
element, first you write the <head>
, close it and then it comes the <body>
and it's, uhm, body.<h:form>
will generate a plain HTML <form>
,so it must be inside the <body>
tag, not outside.<h:form>
should only contain the elements that will be sent in the request, no more elements, this in order to generate confusion with the values in the managed bean when repeated.Fixing this code:
<f:view>
<html>
<head>
</head>
<body>
<h:form id="myform">
<h:outputText value="select chinese" />
<h:selectOneMenu id="chinese" value="#{mychinese.selectedChineseName}" onchange="submit()">
<f:valueChangeListener type="backing_Bean.ChineseValueListner" />
<f:selectItems value="#{mychinese.chineseName}" />
</h:selectOneMenu>
</h:form>
<br>
<h:outputText value="Message from server" />
<h:inputText value="#{mychinese.chinesemessage}" />
<h:message for="chinese"/>
</body>
</html>
<f:view>
Upvotes: 1