学习掌握:
jsf 验证
jsf 国际化
jsf 导航
login_zh_CN.properties
title=第一个jsf程序
username=用户名
password=密码
submit=登陆
usererror=用户名必须添
passerror=密码错误
这个文件放在 新建的 firstjsf包下
编译
native2ascii –encoding gb2312 login_src.properties login_zh_CN.properties
新建文件
Login.java
放在
login.firstjsf包下.
src
package login.firstjsf;
import javax.faces.context.FacesContext;
import javax.faces.component.UIComponent;
import javax.faces.application.FacesMessage;
import javax.faces.validator.ValidatorException;
import java.util.ResourceBundle;
import java.util.Map;
import domain.SecurityManager;
import java.io.Serializable;
public class Login extends Object implements Serializable{
private String username;
private String password;
private ResourceBundle bundle;
private FacesMessage loginErrorMessage;
private FacesMessage usernameRequiredMessage;
private FacesMessage passwordRequiredMessage;
public LogonForm() {
// 初始化资源文件
bundle = ResourceBundle.getBundle("login.logon");//得到国际化资源文件
usernameRequiredMessage = new FacesMessage(FacesMessage.SEVERITY_INFO, message, null);
string message = bundle.getString("usererror");
passwordRequiredMessage = new FacesMessage(FacesMessage.SEVERITY_INFO, message, null);
message = bundle.getString("passworderror");
}
public String logining() {
if (getUsername()=="okok") {
return "success";
//success在 faces-config.xml配置,这就是动态导航
else
return "failure";
}
public String getPassword() {
return password;
}
public void setPassword(String string) {
password = string;
}
public String getUsername() {
return username;
}
public void setUsername(String string) {
username = string;
}
//前台验证 没有填提示错误信息 用户必须填写
public void validateName(FacesContext context, UIComponent toValidate,
Object value) throws ValidatorException {
if(name.length()< 1|| length > 16) {
throw new ValidatorException(usernameErrorMessage);
}
return;
}
//验证密码必填
public void validatePassword(FacesContext context, UIComponent toValidate,
Object value) throws ValidatorException {
if(name.length()< 1|| length > 16) {
throw new ValidatorException(passwordErrorMessage);
}
return;
}
}
前台
<!--导入jsf标签-->
<%@taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<%@taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<!--国际化 文件-->
<f:loadBundle basename="firstjsf.login" var="first"/>
<f:view>
<head>
<title>
<!--输出标题-->
<h:outputText value="#{first.title}"/>
</title>
</head>
<body>
<h:form>
<!--输出用户名-->
<h:outputText value="#{first.username}"/>
<!--文本框 用户名 “login" 在配置文件 实际是login.firstjsf.Login类 validator 验证-->
<h:inputText id="username" value="#{login.username}" maxlength="20"validator="#{login.validateName}"/>
<!--输出密码-->
<h:outputText value="#{first.Password}"/>
<!--文本框--〉
<h:inputText id="username" value="#{login.password}" maxlength="16" validator="#{login.validatePassword}"/>
<!--btton 按钮-->
<h:commandButton value="#{login.submit}" action="#{login.logining}"/>
<!--消息提示-->
<h:messages showSummary="true" showDetail="false" layout="table"/>
</f:view>
</body>
web.xml 配置文件
<?xml version="1.0" encoding="UTF-8"?>
<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">
<display-name>JSFWebModular</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>*.jsf</url-pattern>
</servlet-mapping>
</web-app>
jsf faces-config.xml 文件
配置受管理的Bean
<managed-bean>
<description>first jsf</description>
<managed-bean-name>login</managed-bean-name>
<managed-bean-class>login.firstjsf.Login</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
导航配置
<navigation-rule>
<!--from-view-id 标识初始页-->
<from-view-id>/Logon.jsp</from-view-id>
<!--navigation-case 标识一个导航块-->
<navigation-case>
<from-outcome>success</from-outcome>
<!--to-view-id元素为这个导航块指定目标页-->
<to-view-id>/ok.jsp</to-view-id>
</navigation-case>
<navigation-case>
<!--from-outcome元素是navigation-rule中from-view-id子元素处理的结果-->
<from-outcome>failure</from-outcome>
<to-view-id>/error.jsp</to-view-id>
</navigation-case>
注:
要实现动态导航,按钮或链接必须有一个方法引用,以用于调用相应的方法,导航处理器根据方法返回的字符串来匹配导航规则