我是 jsf 的新手。我正在我的食堂应用程序中实现“valueChangeListener”。在支持 bean 中,我有一个字符串映射。
我在 jsp 页面中有一个下拉列表,当我从下拉列表中选择值时,listner 被触发。我想在文本框中显示相应的值。但它给了我“为空转换器设置值==”错误。
当我实现转换器时,错误发生了变化,现在它显示“Bean 属性不可写”的错误。
我正在使用 jsf 1.2。我看过很多关于这个的帖子,但没有任何效果。
请大家帮帮我.....提前谢谢。
//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>
// backing 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());
}
}