JSF 细粒度的提交 ,事件驱动型的组件模型
FacesSevlet 处理 相当于struts的actionSevlet
jsF请求处理生命周期
jsf配置实例
public class Computer {
private double firstNumber = 0.0;
private double secondNumber = 0.0;
private double result;
private ComputerOper computerOpr = new ComputerOper();
public double getFirstNumber() {
return firstNumber;
}
public void setFirstNumber(double firstNumber) {
this.firstNumber = firstNumber;
}
public double getSecondNumber() {
return secondNumber;
}
public void setSecondNumber(double secondNumber) {
this.secondNumber = secondNumber;
}
public double getResult() {
return result;
}
public void setResult(double result) {
this.result = result;
}
public String add(){
result = computerOpr.add(this.firstNumber, this.secondNumber);
//必须是字符串
return "success";
}
}
public class ComputerOper {
public double add(double a,double b){
return a+b;
}
}
<?xml version='1.0' encoding='UTF-8'?>
<faces-config xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd"
version="1.2">
<application>
<view-handler>com.sun.facelets.FaceletViewHandler</view-handler>
</application>
<managed-bean>
<managed-bean-name>Computer</managed-bean-name>
<managed-bean-class>test.Computer</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
<navigation-rule>
<from-view-id>/index.jsp</from-view-id>
<navigation-case>
<from-outcome>success</from-outcome>
<to-view-id>/result.jsp</to-view-id>
</navigation-case>
</navigation-rule>
</faces-config>
<?xml version="1.0" encoding="UTF-8"?>
<!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"
xml:lang="en"
lang="en">
<body>
<f:view>
<h:form>
<h:panelGrid columns="3">
<h:outputLabel value="输入第一个数字:"></h:outputLabel>
<h:inputText id="firstNumber" value="#{Computer.firstNumber}"></h:inputText>
<h:message for="firstNumber"></h:message>
<h:outputLabel value="输入第二个数字:"></h:outputLabel>
<h:inputText id="secondNumber" value="#{Computer.secondNumber}"></h:inputText>
<h:message for="secondNumber"></h:message>
</h:panelGrid>
<h:commandButton value="相加" action="#{Computer.add}"></h:commandButton>
</h:form>
</f:view>
</body>
</html>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<context-param>
<param-name>javax.faces.CONFIG_FILES</param-name>
<param-value>/WEB-INF/faces-config.xml</param-value>
</context-param>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.faces</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<listener>
<listener-class>com.sun.faces.config.ConfigureListener</listener-class>
</listener>
<listener>
<listener-class>com.sun.faces.application.WebappLifecycleListener</listener-class>
</listener>
</web-app>
输入访问路径: