设计一个项目对登录用户名、密码进行验证及对E-mail地址进行格式转换
简单操作步骤:
MyEclipse下进行:
1.创建WEB工程
按照1中的步骤,首先创建web工程,名称为jsflogin,然后对工程添加JSF支持;
2.创建受管的Bean
打开faces-cofig文件,在右边的outline中,右键managedBean,出现如下框,如图填写内容,设置Bean名称为loginBean;
为loginBean创建四个属性,分别是username,password,converter,email
填写LoginBean.java的代码,如下:
/**
*
*/
package cn.edu;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;
/**
* @author Administrator
*
*/
public class LoginBean {
private String username;
private String password;
private Converter converter;
private Email email;
public LoginBean() {
}
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 Converter getConverter() {
return new Converter(){
@Override
public Object getAsObject(FacesContext arg0, UIComponent arg1,
String arg2)throws ConverterException {
try{
String[] values=arg2.split("@");
Email email=new Email(values[0],values[1]);
return email;
}catch(Exception ex){
ex.printStackTrace();
throw new ConverterException("邮箱错误");
}
// TODO Auto-generated method stub
//return null;
}
@Override
public String getAsString(FacesContext arg0, UIComponent arg1,
Object arg2) {
Email email = (Email)arg2;
return "Email[before="+email.getBefore()+",after="+email.getAfter()+"]";
// TODO Auto-generated method stub
//return null;
}
};
}
public void setConverter(Converter converter) {
this.converter = converter;
}
public Email getEmail() {
return email;
}
public void setEmail(Email email) {
this.email = email;
}
public String login(){
if(!username.equals("zhangsan")||!password.equals("123"))
return "false";
return "true";
}
}
在相同的包中,创建一个Email类,Email.java代码如下:
package cn.edu;
public class Email {
private String before;
private String after;
public Email(String before,String after){
this.before=before;
this.after=after;
}
public String getBefore() {
return before;
}
public void setBefore(String before) {
this.before = before;
}
public String getAfter() {
return after;
}
public void setAfter(String after) {
this.after = after;
}
public String toString(){
return this.before+"@"+this.after;
}
}
3.创建JSP页面
在faces-config中,选择再在面板中点击,即可创建;通过此方法创建两个jsp,即login.jsp与loginsuccess.jsp;
打开login.jsp,填写如下代码:
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'login.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>
<f:view>
<h:form>
用户名:<h:inputText value="#{loginBean.username}"/></br>
密 码:<h:inputSecret value="#{loginBean.password}"></h:inputSecret></br>
E-mail:<h:inputText value="#{loginBean.email}" id="email" converterMessage="格式不正确">
<f:converter binding="#{loginBean.converter}"/></h:inputText></br>
<h:message for="email" style="color:red"></h:message>
<h:commandButton value="提交" action="#{loginBean.login}"></h:commandButton>
</h:form>
</f:view>
</body>
</html>
打开loginsuccess.jsp,填写如下代码:
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'loginsuccess.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>
<f:view>
登陆成功!<h:outputText value="#{loginBean.username}"/>欢迎您!</br>
您的邮箱是<h:outputText value="#{loginBean.email}"/>
</f:view>
</body>
</html>
4.创建导航规则
打开faces-config.xml文件,选择,点击login.jsp,再点击loginsuccess.jsp,即可出现相应对话框,输入“ture”(此中根据你在loginBean.java中的login方法的返回字符串来填写,如果返回的是failure或success,则此处填写success),即可;然后再学则,点击login.jsp ,再点击login.jsp ,输入false,就会设置当输入错误时 ,再次返回登录界面的导航规则;
5.部署web应用
右键单击jsflogin工程,->export->WAR->路径为:相应的jboss,打开server->default->deploy,完成;
6.测试
启动jboss;完成后,代开浏览器,输入http://localhost:8080/jsflogin/login.faces,即出现如下界面:
输入相应的用户名,密码,Email,如图:
点击提交,出现如图所示: