我是jsf的新手。我正在我的食堂应用程序中实现“valueChangeListener”。在支持bean中,我有一个字符串映射。
我在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());
}
}
答案 0 :(得分:1)
您使用Map<String, String>
来保存<h:selectOneMenu>
所选值的问题:
<h:selectOneMenu id="chinese" value="#{mychinese.chineseName}" onchange="submit()">
...
</h:selectOneMenu>
为了解决这个问题,您应该将value
标记属性绑定到托管bean中的String
属性。
MyChinese课程修改:
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修改:
<h:selectOneMenu id="chinese" value="#{mychinese.selectedChineseName}"
onchange="submit()">
...
</h:selectOneMenu>
请注意,通过此操作,您可能不会需要valueChangeListener
。
请注意,HTML中存在问题:
<head>
元素不得包装<body>
元素,首先编写<head>
,关闭它,然后它出现<body>
及其,嗯,身体。<h:form>
会生成纯HTML <form>
,因此必须位于<body>
标记内,而不是在外面。<h:form>
应该只包含将在请求中发送的元素,不再包含元素,这是为了在重复时与托管bean中的值产生混淆。修复此代码:
<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>