设计一个项目对登录用户名、密码进行验证及对E-mail地址进行格式转换
简单操作步骤:
MyEclipse下进行:
1.创建WEB工程
按照1中的步骤,首先创建web工程,名称为jsflogin,然后对工程添加JSF支持;
2.创建受管的Bean
打开faces-cofig文件,在右边的outline中,右键managedBean,出现如下框,如图填写内容,设置Bean名称为loginBean;
为loginBean创建四个属性,分别是username,password,converter,email
填写LoginBean.java的代码,如下:
/**
*
*/
packagecn.edu;
importjavax.faces.component.UIComponent;
importjavax.faces.context.FacesContext;
importjavax.faces.convert.Converter;
importjavax.faces.convert.ConverterException;
/**
*@authorAdministrator
*
*/
publicclassLoginBean{
privateStringusername;
privateStringpassword;
privateConverterconverter;
privateEmailemail;
publicLoginBean(){
}
publicStringgetUsername(){
returnusername;
}
publicvoidsetUsername(Stringusername){
this.username=username;
}
publicStringgetPassword(){
returnpassword;
}
publicvoidsetPassword(Stringpassword){
this.password=password;
}
publicConvertergetConverter(){
returnnewConverter(){
@Override
publicObjectgetAsObject(FacesContextarg0,UIComponentarg1,
Stringarg2)throwsConverterException{
try{
String[]values=arg2.split("@");
Emailemail=newEmail(values[0],values[1]);
returnemail;
}catch(Exceptionex){
ex.printStackTrace();
thrownewConverterException("邮箱错误");
}
//TODOAuto-generatedmethodstub
//returnnull;
}
@Override
publicStringgetAsString(FacesContextarg0,UIComponentarg1,
Objectarg2){
Emailemail=(Email)arg2;
return"Email[before="+email.getBefore()+",after="+email.getAfter()+"]";
//TODOAuto-generatedmethodstub
//returnnull;
}
};
}
publicvoidsetConverter(Converterconverter){
this.converter=converter;
}
publicEmailgetEmail(){
returnemail;
}
publicvoidsetEmail(Emailemail){
this.email=email;
}
publicStringlogin(){
if(!username.equals("zhangsan")||!password.equals("123"))
return"false";
return"true";
}
}
在相同的包中,创建一个Email类,Email.java代码如下:
packagecn.edu;
publicclassEmail{
privateStringbefore;
privateStringafter;
publicEmail(Stringbefore,Stringafter){
this.before=before;
this.after=after;
}
publicStringgetBefore(){
returnbefore;
}
publicvoidsetBefore(Stringbefore){
this.before=before;
}
publicStringgetAfter(){
returnafter;
}
publicvoidsetAfter(Stringafter){
this.after=after;
}
publicStringtoString(){
returnthis.before+"@"+this.after;
}
}
3.创建JSP页面
在faces-config中,选择再在面板中点击,即可创建;通过此方法创建两个jsp,即login.jsp与loginsuccess.jsp;
打开login.jsp,填写如下代码:
<%@pagelanguage="java"import="java.util.*"pageEncoding="GBK"%>
<%@tagliburi="http://java.sun.com/jsf/html"prefix="h"%>
<%@tagliburi="http://java.sun.com/jsf/core"prefix="f"%>
<%
Stringpath=request.getContextPath();
StringbasePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">
<html>
<head>
<basehref="<%=basePath%>">
<title>MyJSP'login.jsp'startingpage</title>
<metahttp-equiv="pragma"content="no-cache">
<metahttp-equiv="cache-control"content="no-cache">
<metahttp-equiv="expires"content="0">
<metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">
<metahttp-equiv="description"content="Thisismypage">
<!--
<linkrel="stylesheet"type="text/css"href="styles.css">
-->
</head>
<body>
<f:view>
<h:form>
用户名:<h:inputTextvalue="#{loginBean.username}"/></br>
密码:<h:inputSecretvalue="#{loginBean.password}"></h:inputSecret></br>
E-mail:<h:inputTextvalue="#{loginBean.email}"id="email"converterMessage="格式不正确">
<f:converterbinding="#{loginBean.converter}"/></h:inputText></br>
<h:messagefor="email"style="color:red"></h:message>
<h:commandButtonvalue="提交"action="#{loginBean.login}"></h:commandButton>
</h:form>
</f:view>
</body>
</html>
打开loginsuccess.jsp,填写如下代码:
<%@pagelanguage="java"import="java.util.*"pageEncoding="GBK"%>
<%@tagliburi="http://java.sun.com/jsf/html"prefix="h"%>
<%@tagliburi="http://java.sun.com/jsf/core"prefix="f"%>
<%
Stringpath=request.getContextPath();
StringbasePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">
<html>
<head>
<basehref="<%=basePath%>">
<title>MyJSP'loginsuccess.jsp'startingpage</title>
<metahttp-equiv="pragma"content="no-cache">
<metahttp-equiv="cache-control"content="no-cache">
<metahttp-equiv="expires"content="0">
<metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">
<metahttp-equiv="description"content="Thisismypage">
<!--
<linkrel="stylesheet"type="text/css"href="styles.css">
-->
</head>
<body>
<f:view>
登陆成功!<h:outputTextvalue="#{loginBean.username}"/>欢迎您!</br>
您的邮箱是<h:outputTextvalue="#{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,如图:
点击提交,出现如图所示: