0
我正在構建一個基於ICEmobile與JSF 2.1和Maven的web應用程序。我遇到這個問題時,我的jsf頁面沒有查找它的bean,但我仍然可以生成和部署項目沒有錯誤,我通過Eclipse中的運行配置部署該項目:clean tomcat7:run-war
。即使我通過註釋或applicationContext.xml聲明瞭該bean,頁面仍然不會正確調用該bean。我可以獲取bean中字段的值,但不能調用該bean中的方法System.out.println("Pressed");
不會向控制檯輸出任何內容。我猜這肯定是與我的配置有關,但我不知道該在哪裏查看。請給我一些提示。由於jsf不尋找它的豆
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:util="http://java.sun.com/jsf/composite/components"
xmlns:mobi="http://www.icesoft.com/icefaces/mobile/component"
xmlns:ice="http://www.icesoft.com/icefaces/component"
xmlns:icecore="http://www.icefaces.org/icefaces/core">
<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"></meta>
<meta name="viewport"
content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=0" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<title>ICEfaces Mobile Showcase</title>
<mobi:deviceStylesheet media="screen" />
</h:head>
<h:body>
<mobi:pagePanel>
<f:facet name="body">
<h:form>
<mobi:commandButton value="#{buttonBean.buttonName}" buttonType="important" actionListener="#{buttonBean.buttonPress}">
<f:attribute name="buttonState" value="change"/>
</mobi:commandButton>
</h:form>
</f:facet>
</mobi:pagePanel>
</h:body>
</html>
豆:
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;
@ManagedBean(name = ButtonBean.BEAN_NAME)
@SessionScoped
public class ButtonBean implements Serializable {
private static final long serialVersionUID = 1L;
public static final String BEAN_NAME = "buttonBean";
private String buttonName = "0";
public String getButtonName() {
return buttonName;
}
public void setButtonName(String buttonName) {
this.buttonName = buttonName;
}
public void buttonPress(ActionEvent event){
System.out.println("Pressed");
String buttonState = (String)event.getComponent().getAttributes().get("buttonState");
if (buttonState.equals("change") && buttonName.equals("0")) buttonName = "1";
else if (buttonState.equals("change") && buttonName.equals("1")) buttonName = "0";
}
}
你還可以添加包含註釋的bean聲明嗎?刪除方法中的內容如果它很笨重 –
我已經更新了xhtml和bean。顯然,我發現jsf頁面沒有拿起這個bean,它加載了buttonName並顯示「0」,但它並沒有調用該方法。 – Duc
只是好奇,你有任何理由用'@ SessionScoped'而不是'@ RequestScoped'來註釋這個bean嗎?希望這是因爲你遺漏了一條信息,否則我會建議你閱讀BalusC的這篇文章:http://stackoverflow.com/questions/7031885/how-to-choose-the-right-bean -scope/7031941#7031941 – Aquillo