JSF入門例
1.javabeanを定義する
UserBean.java
2.web.xml構成
3.WEB-INFでfaces-configを定義.xml
4.indexを作成する.jsp
5.welcomeを作成する.jsp
OK
ページアクセス
http://localhost:8080/jsfdemo/pages/index.faces
UserBean.java
package com.cn.zhb;
public class UserBean {
private String name;
private String password;
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
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.faces</welcome-file>
</welcome-file-list>
</web-app>
3.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/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.cn.zhb.UserBean
</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
</faces-config>
4.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>JSF</title>
</head>
<body>
<f:view>
<h:form>
NAME: <h:inputText value="#{user.name}"/>
PASSWORD:<h:inputSecret value="#{user.password}"></h:inputSecret>
<p><h:commandButton value="LOGIN" action="login"/>
</h:form>
</f:view>
</body>
</html>
5.welcomeを作成する.jsp
<STRONG><%@ 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>JSF</title>
</head>
<body>
<f:view>
name:<h:outputText value="#{user.name}"/> <br>
password:<h:outputText value="#{user.password}"/><br>
<h3> JavaServer Faces!</h3>
</f:view>
</body>
</html></STRONG>
OK
ページアクセス
http://localhost:8080/jsfdemo/pages/index.faces