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

第一个JSF程序

2010-11-30 19:18 211 查看
Myeclipse8以上的版本不需要加jar包jsf-api.jar,jsf-impl.jar

1.web.xml配置

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

<welcome-file-list>

<welcome-file>index.jsp</welcome-file>

</welcome-file-list>

<!-- JSF Demo HelloWorld -->

<description>JSF HelloWorld</description>

<servlet>

<servlet-name>facesServlet</servlet-name>

<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>facesServlet</servlet-name>

<url-pattern>*.faces</url-pattern>

</servlet-mapping>

<!-- JSF Demo HelloWorld Over -->

</web-app>

2.在web-INF 新建faces-config.xm

<?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_2_0.xsd"

version="2.0">



<navigation-rule>

<from-view-id>/pages/index.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>com.yl.bean.UserBean</managed-bean-class>

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

</managed-bean>

</faces-config>



3.建立简单java bean

package com.yl.bean;

public class UserBean {

private String username;

private int age;

public String getUsername() {

return username;

}

public void setUsername(String username) {

this.username = username;

}

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}



}



4.建立pages/index.jsp和welcome.jsp

index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

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

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

<html>

<head>





<title>This is my first JSF</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">

</head>



<body>

<f:view>

<h:form>

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

<h:commandButton value="提交" action="login"></h:commandButton>

</h:form>

</f:view>

</body>

</html>

welcom.jsp



<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

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

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

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

这是第一个JSF程序,HelloWorld

<f:view>

<h:outputText value="#{user.username}"></h:outputText>

</f:view>

</body>

</html>



5.测试

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