标签:des style blog http os java io strong for
一 什么是JSF:
JSF是JCP开发的一种Java标准,用于构建web应用程序新标准的Java框架,他提供了一种以组件为中心来开发Java web用户界面的方法,从而简化了开发。
二 JSF体系结构:
JSF的主要优势就是它是严格遵循“ 模型 - 视图 - 控制器 ” 即MVC设计模式的框架, 用户界面代码(视图)与应用程序数据和逻辑(模型)的清晰分离使JSF应用程序更易于管理。所有与应用程序交互的访问都交由FacesServlet(控制器)来处理。
三 JSF小例子:
1. 代码结构及Jar包:
2. 配置web.xml文件:
<?xml version="1.0" encoding="ISO-8859-1"?> <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4"> <description>JSF Demo</description> <display-name>JSF Demo</display-name> <servlet> <servlet-name>Faces Servlet</servlet-name> <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> <load-on-startup>1</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.html</welcome-file> </welcome-file-list> </web-app>FacesServlet是整个应用的前端控制器, 所有的请求都通过FacesServlet来处理。
上面的配置中,我们将所有的.faces的请求交由FaceServlet来处理,FaceServlet会找到对应的jsp页面
例如:如果请求是/index.faces的话,它会帮我们找到/index.jsp页面
3. 创建受管Bean :充当控制器的角色
public class User {
	private String username;
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
}
<?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>/index.jsp</from-view-id> <navigation-case> <from-outcome>login</from-outcome> <to-view-id>/hello.jsp</to-view-id> </navigation-case> </navigation-rule> <managed-bean> <managed-bean-name>user</managed-bean-name> <managed-bean-class>com.zdp.domain.User</managed-bean-class> <managed-bean-scope>session</managed-bean-scope> </managed-bean> </faces-config>
(1)第一个页面: 用户登录 index.jsp
<%@taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%@taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<%@page contentType="text/html;charset=utf-8"%>
<html>
<head>
<title>hello, JSF</title>
</head>
<body>
	<f:view>
		<h:form>
			<h3>please input your name</h3> 
			<h:inputText value="#{user.username}" /><p>
			<h:commandButton value="submit" action="login" />
		</h:form>
	</f:view>
</body>
</html>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@page contentType="text/html;charset=utf-8"%>
<html>
<head>
<title>welcome, JSF</title>
</head>
<body>
   <f:view>
       hi!<h:outputText value="#{user.username}"/> 
       <h3>welcome to use JavaServer Faces!</h3>
   </f:view>
</body>
</html>6. 访问地址:http://localhost:8080/jsf/index.faces
参考文章:http://blog.csdn.net/qjyong/article/details/1833457
标签:des style blog http os java io strong for
原文地址:http://blog.csdn.net/zdp072/article/details/38847859