2013-02-15 68 views
0

我是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()); 
    } 
} 
+0

如果所提出的解決方案爲你工作,不要忘了[標記爲一個答案(http://meta.stackexchange.com/a/5235/182862) – 2013-02-18 15:42:52

回答

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> 
+0

感謝您的幫助...... – rishi 2013-05-17 05:31:03