我正在使用 JSF 2.1 和 Maven 构建基于 ICEmobile 的 Web 应用程序。当我的 jsf 页面没有查找它的 bean 时,我遇到了这个问题,但我仍然可以毫无错误地构建和部署项目,我通过 Eclipse: 中的运行配置部署项目clean tomcat7:run-war
。即使我通过注释或 applicationContext.xml 声明了 bean,页面仍然没有正确调用 bean。我可以获取 bean 中字段的值,但无法调用该 bean 中的方法,System.out.println("Pressed");
不会向控制台提供任何输出。我猜这一定与我的配置有关,但我不知道在哪里查看。请给我一些提示。谢谢
<!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";
}
}