网上搜索到的jsf的简介:
准确地说,JSF是一个标准,而不是一个产品。目前,JSF已经有两个实现产品可供选择,包含Sun的参考实现和Apache的MyFaces。大部分的时候,我们所说的JSF都是指
Sun的参考实现。目前,JSF是作为JEE 5.0的一个组成部分,与JEE 5.0一起发布。 JSF的行为方法在POJO中实现,JSF的Managed Bean无需继承任何特别的类。因此,无需在
表单和模型对象之间实现多余的控制器层。JSF中没有控制器对象,控制器行为通过模型对象实现。
当然,JSF也允许生成独立的控制器对象。在Struts 1中,Form Bean包含数据,Action Bean包含业务逻辑,二者无法融合在一起。在JSF中,既可以将二者分开,也可以
合并在一个对象中,提供更多灵活的选择.JSF的事件框架可以细化到表单中每个字段。JSF依然是基于JSP/Servlet的,仍然是JSP/Servlet架构,因而学习曲线相对简单。在实
际使用过程中,JSF也会存在一些不足:
— 作为新兴的MVC框架,用户相对较少,相关资源也不是非常丰富。
— JSF并不是一个完全组件化的框架,它依然是基于JSP/Servlet架构的。
— JSF的成熟度还有待进一步提高。
个人认为,新型框架总会吸取一些已有成熟框架的优点,摒弃其缺点,或对已有框架的实现方式进行优化,多了解一些知识总是会丰富自己的经验,目前公司内一部分应用使用
到了jsf
一、jsf最简单案例
红框中为jsf所需jar,javaee中已经存在有jsf所需核心jar和依赖jstl.jar,如第一个红框内所示,只需要导入第二个红框的依赖jar。
首先配置web.xml,jsf是一个MVC框架,每个mvc框架都有一个前端控制器来处理所有的请求,jsf的前端控制器为:FacesServlet.
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
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-app_2_5.xsd">
<display-name>JSF Demo</display-name>
<description>JSF Demo</description>
<servlet>
<servlet-name>fs</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>fs</servlet-name>
<url-pattern>*.faces</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
每个mvc框架也都会有自己的配置文件,jsf的默认配置文件在/WEB-INF/下,文件名默认为:faces-config.xml
<?xml version="1.0"?>
<!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>/pages/index01.jsp</from-view-id>
<navigation-case>
<from-outcome>login</from-outcome>
<to-view-id>/pages/welcome.jsp</to-view-id>
</navigation-case>
</navigation-rule>
<managed-bean>
<managed-bean-name>user</managed-bean-name>
<managed-bean-class>domain.User</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
</faces-config>
超级简单的javabean
package domain;
public class User {
private String userName;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<a href="${pageContext.request.contextPath }/pages/index01.faces">去输入页</a>
</body>
</html>
index01.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="h" uri="http://java.sun.com/jsf/html" %>
<%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index01.jsp' starting page</title>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<title>第一个JSF程序</title>
</head>
<body>
<h3>请输入姓名:</h3>
<f:view>
<h:form>
<h:inputText value="#{user.userName }"/>
<h:commandButton value="提交" action="login"/>
</h:form>
</f:view>
</body>
</html>
welcome.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
<%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'welcome.jsp' starting page</title>
<meta content="text/html;charset=UTF-8">
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<h3>数据显示页面</h3>
<f:view>
<h:outputText value="#{user.userName }"/>
</f:view>
</body>
</html>
在index.jsp中点击“去输入页”,则会根据这个路径和faces-config.xml中的from-view-id标签内容进行对比,“去输入页”的链接url为
${pageContext.request.contextPath }/pages/index01.faces,即访问${pageContext.request.contextPath }/pages/index01.jsp页面,刚好和
from-view-id对应的值相同,则会根据index.jsp中的h:commandButton标签action属性值去faces-config.xml中查找访问的页面所在的navigation-rule
中的from-outcome的值,并进行比较,若相同,则会进入到相应的页面中。faces-config.xml中managed-bean标签就比较好理解了。总的来讲,jsf的
第一个程序还是相当简单的