您的位置:首页 > Web前端 > JavaScript

jsf:jsf入门案例

2012-04-16 11:37 232 查看
UserBean.java



package org.baicai.jsf_document.simple_demo;

public class UserBean

{

private String name;

private String password;

public String getName()

{

return name;

}

public void setName(String name)

{

this.name = name;

}

public String getPassword()

{

return password;

}

public void setPassword(String password)

{

this.password = password;

}

@Override

public String toString()

{

return String.format("name:%s,password:%s",name,password);

}

}

WebRoot/index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<!--第一步: 添加jsf的基本(jsf/html与html标签作用几乎一致 )标签标签库,和jsf的核心(jsf/cord jsf特有的标签)标签标签库 -->

<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>

<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<!-- f:view 相当于超文本标记语言的html,是必须要添加的。否则会报下面的错 -->

<!-- Component javax.faces.component.UIViewRoot@92b535 not expected type.

Expected: javax.faces.component.UIForm. Perhaps you're missing a tag? -->

<f:view>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Jsf Menu</title>

</head>

<body>

<!-- h:form,当页面有h:commandLink,h:commandButton标签时该标签是必须要添加的标签 否则这些标签

没有用 就相当于reset sbumit没有放在form中一样 -->

<!--This link is disabled as it is not nested within a JSF form. -->

<h:form>

<!-- h:commandLink 链接标签,相当于html中的a元素 . action属性的值有两种形式 ,本形式会直接根据导航

进到下一个页面,value属性的值为页面的显示值 -->

<h:commandLink action="simpleDemo" value="第一个jsf演示"></h:commandLink>

</h:form>

</body>

</f:view>

</html>

WebRoot/simpleDemo/index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"

pageEncoding="UTF-8"%>

<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>

<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<f:view>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>simpleDemo</title>

</head>

<body>

<h:form>

<!-- outputText标签用来在页面显示文本 value属性值为页面要显示的文本值 -->

<h:outputText value="请输入用户名"></h:outputText>

<!-- inputText标签用来接受用户的信息 value属性值为保存用户信息的变量-->

<h:inputText value="#{user.name}"></h:inputText>

<h:outputText value="请输入用户名"></h:outputText>

<!--h:inputSecret 密码框-->

<h:inputSecret value="#{user.password}"></h:inputSecret>

<!--commandButton提交标签 action,value属性和commandLink的用法一致 -->

<h:commandButton action="showBean" value="登录" ></h:commandButton>

</h:form>

</body>

</f:view>

</html>

WebRoot/simpleDemo/welcome.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"

pageEncoding="UTF-8"%>

<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>

<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<f:view>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Insert title here</title>

</head>

<body>

<h:form>

<h3>

欢迎<h:outputText value="#{user.name}"/>登录!

</h3>

</h:form>

</body>

</f:view>

</html>

WebRoot/WEB-INF/web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:javaee="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">

<!-- 指定加载多个faces-config.xml文件,文件之间用逗号分开。 默认会加载faces-config.xml文件, -->

<context-param>

<param-name>javax.faces.CONFIG_FILES</param-name>

<param-value>

/WEB-INF/faces-config/faces-config.xml,

/WEB-INF/faces-config/simpledemo-faces-config.xml

</param-value>

</context-param>

<!-- faces 的核心配置 -->

<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>

WebRoot/WEB-INf/faces-config/faces-config.xml

<?xml version="1.0" encoding="UTF-8"?>

<!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>simpleDemo</from-outcome>

<!--触发事件后陈显结果的页面 -->

<to-view-id>/simpleDemo/index.jsp</to-view-id>

</navigation-case>

</navigation-rule>

</faces-config>

WebRoot/WEB-INf/faces-config/simpledemo-faces-config.xml

<?xml version="1.0" encoding="UTF-8"?>

<!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>

<!--在simpleDemo/index.jsp页面中触发了某一事件,根据返回结果的字符串打开/simpleDemo/welcome.jsp页面 -->

<navigation-rule>

<from-view-id>/simpleDemo/index.jsp</from-view-id>

<navigation-case>

<from-outcome>showBean</from-outcome>

<to-view-id>/simpleDemo/welcome.jsp</to-view-id>

</navigation-case>

</navigation-rule>

<!-- manageBean的管理 -->

<managed-bean>

<!--类的别名称 用于方便页面调用类的方法,属性 -->

<managed-bean-name>user</managed-bean-name>

<!--类的路径 -->

<managed-bean-class>org.baicai.jsf_document.simple_demo.UserBean</managed-bean-class>

<!--类的作用范围 -->

<managed-bean-scope>session</managed-bean-scope>

</managed-bean>

</faces-config>

效果: 注意在访问index页面时 后缀为faces 否则程序会报错

org.apache.jasper.JasperException: javax.servlet.ServletException: javax.servlet.jsp.JspException: Cannot find FacesContext






内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: