我是jsf的新手。我正在食堂應用程序中實施「valueChangeListener」。在支持bean中,我有一個字符串映射。valueChnageListner不能在jsf中工作
我在jsp頁面中有一個下拉菜單,當我從下拉列表中選擇值時,listner被觸發。我想在文本框中顯示相應的值。但它給我「設置值爲null轉換器」錯誤。
當我實現轉換器,錯誤更改,現在它顯示錯誤,「Bean屬性不可寫」。
我正在使用jsf 1.2。我看過很多關於此的帖子,但沒有任何工作。
PLZ傢伙幫我.....在此先感謝。
// JSP代碼
<%@ 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>
//後臺bean的代碼如下
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());
}
}
如果所提出的解決方案爲你工作,不要忘了[標記爲一個答案(http://meta.stackexchange.com/a/5235/182862) – 2013-02-18 15:42:52