关于JSF中UI控件binding的头痛问题,恳请指教
我照着下面的例子做,系统报错
严重: Servlet.service() for servlet faces threw exception
javax.faces.el.EvaluationException: Exception while invoking expression #{Backing.listen}
.....
Caused by: java.lang.NullPointerException
at jwc.compet.view.bean.BackingBean.listen(BackingBean.java:20)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.myfaces.el.MethodBindingImpl.invoke(MethodBindingImpl.java:129)
... 28 more
恳请指教,问题出在哪里?
JSF中UI控件binding属性的用法
在.Net中,binding是个十分容易理解的东西,但最近学习JSF,总理解不了binding属性的用法,查了一些资料,都是简单的说明,对这个属性没有什么例子。今天星期天,没事来到公司,突然对这个属性领悟了,觉得特别高兴。等我理解后,觉得这个属性是十分简单的,没理解之前,隔层窗户纸,总那么朦朦胧胧的。现在把这个小例子写出来,以做备忘。
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="h" uri="http://java.sun.com/jsf/html" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsf/core" %>
<html>
<head>
<title>
Simple jsp page
</title>
</head>
<body>
<c:view>
<c:loadBundle basename="jsfResource" var="msg"></c:loadBundle>
<h:form>
<h:outputText value="#{msg.username}"></h:outputText> :
<h:inputText value="" binding="#{Backing.txtComponentInput}"></h:inputText>
<br>
<h:commandButton value="#{msg.btnSubmit}" actionListener="#{Backing.listen}"></h:commandButton>
</h:form>
</c:view>
</body>
</html>
index.jsp文件说明,16行就是binding绑定,18行是action事件。其实binding就是把控件的实例以属性的形式存储到bean里面,便于其他函数或方法操作这个控件。在.Net中,每个服务器控件就相当于一个类,服务器控件的属性就是类中的属性。程序编写人员对控件value,color等属性的操作其实就是对类中属性的操作(理解不正确之处,望给予斧正)。这里,JSF的思想和.Net是一致的。把控件的以实例的形式放到bean中,这样就可以用程序随意操作这个控件的各个属性了。
BackingBean.java
import javax.faces.component.UIInput;
import javax.faces.event.ActionEvent;
import javax.faces.context.FacesContext;
/**
* Created by IntelliJ IDEA.
* User: Administrator
* Date: 2006-1-15
* Time: 10:02:59
* To change this template use File | Settings | File Templates.
*/
public class BackingBean {
UIInput txtComponentInput;
public UIInput getTxtComponentInput() {
return txtComponentInput;
}
public void setTxtComponentInput(UIInput txtComponentInput) {
this.txtComponentInput = txtComponentInput;
}
public void listen(ActionEvent e) {
FacesContext context = FacesContext.getCurrentInstance();
txtComponentInput.setValue("@@@@@@");
txtComponentInput.setRendered(true);
System.out.println(e.getComponent().getClientId(context));
}
}
BackingBean.java文件说明:主要是27,28行,其意思是当按钮的action事件触发后,就控件txtComponentInput进行操作,这里,我只是对Value和Rendered两个属性进行了操作,当然可以对其他属性进行随意操纵。这里的txtComponentInput其实就是<h:inputText value="" binding="#{Backing.txtComponentInput}"></h:inputText>,把此控件绑定到bean中,便于用程序对其进行动态控制。
faces-config.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 <!DOCTYPE faces-config PUBLIC "-//Sun Microsystems,
Inc.//DTD JavaServer Faces Config 1.0//EN"
"http://java.sun.com/dtd/web-facesconfig_1_0.dtd">
<faces-config>
<navigation-rule>
<from-view-id>/index.jsp</from-view-id>
<navigation-case>
<from-outcome>login</from-outcome>
<to-view-id>/index.jsp</to-view-id>
</navigation-case>
</navigation-rule>
<managed-bean>
<managed-bean-name>Backing</managed-bean-name>
<managed-bean-class>BackingBean</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
</faces-config>
faces-config.xml文件大家一看就是知道意思,就不多说了。
下面是运行截图.(当点击Submit按钮时,input被负值@@@@@@)