JSF HelloWorld

由于下个项目要用到Jboss Seam,百度了下,说Jboss Seam基于JSF和EJB3,所以就动手练练,学习下JSF。在学校的时候曾经看过JSF方面的书,但现在都忘的差不多了,只大概的记得几个标签。

下面用JSF做个简单的登陆实例。

开发工具:Myeclipse 8.0 (含JDK、Tomcat)

1、包结构图


很简单,2个页面(登陆页面,和登陆成功页面),一个Java Bean。

2、Java Bean User.java

/**
* @author 胡千好
* @date 2010-6-14
*/
package com.huqianhao.jsf.bean;

/**
* @author Administrator
*
*/
public class User {
private String id;
private String name;
private String username;
private String password;
private String message;

public String verify(){
if(username.equals("hqh")&&password.equals("admin")){
message = "登陆成功!";
return "success";
}else{
message = "用户名或密码不正确!";
return "failure";
}
}

public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}

}
 

3、配置文件Web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
version="2.5"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.faces</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>login.faces</welcome-file>
</welcome-file-list>
</web-app>

4、JSF配置文件faces-config.xml

 

 

<?xml version="1.0" encoding="UTF-8"?>
<faces-config version="1.2" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xi="http://www.w3.org/2001/XInclude" 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_1_2.xsd">
<managed-bean>
<managed-bean-name>user</managed-bean-name>
<managed-bean-class>com.huqianhao.jsf.bean.User</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
<navigation-rule>
<display-name>login</display-name>
<from-view-id>/login.jsp</from-view-id>
<navigation-case>
<from-outcome>success</from-outcome>
<to-view-id>/success.jsp</to-view-id>
</navigation-case>
<navigation-case>
<from-outcome>failure</from-outcome>
<to-view-id>/login.jsp</to-view-id>
</navigation-case>
</navigation-rule>
</faces-config>

5、JSP页面login.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>

<html>
<head>

<title>My JSP 'login.jsp' starting page</title>

</head>

<body>
<f:view>
<h:form >
用户名:<h:inputText value="#{user.username}"></h:inputText><br/>
密码:<h:inputSecret value="#{user.password}"></h:inputSecret><br/>
<h:commandButton action="#{user.verify}" value="登陆"></h:commandButton>
</h:form>
<h:outputText value="#{user.message}" style="color:red;"></h:outputText>
</f:view>
</body>
</html>

6、JSP页面success.jsp

<f:view>
<h:outputText value="#{user.username}"></h:outputText>,你好!
<h:outputText value="#{user.message}"></h:outputText>
</f:view>

7、演示

如果用户名、密码不正确,将会提示相应信息。

 

 

输入正确的用户名、密码后,将跳转到成功页面。

 

 

OK,到此结束!
 

你可能感兴趣的:(J2EE)