J2EE之JSF简单使用

设计一个项目对登录用户名、密码进行验证及对E-mail地址进行格式转换

简单操作步骤:

MyEclipse下进行:

1.创建WEB工程

按照1中的步骤,首先创建web工程,名称为jsflogin,然后对工程添加JSF支持;

2.创建受管的Bean

打开faces-cofig文件,在右边的outline中,右键managedBean,出现如下框,如图填写内容,设置Bean名称为loginBean

为loginBean创建四个属性,分别是usernamepasswordconverteremail

填写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.jsploginsuccess.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方法的返回字符串来填写,如果返回的是failuresuccess,则此处填写success),即可;然后再学则,点击login.jsp,再点击login.jsp,输入false,就会设置当输入错误时,再次返回登录界面的导航规则;

5.部署web应用

右键单击jsflogin工程,->export->WAR->路径为:相应的jboss,打开server->default->deploy,完成;

6.测试

启动jboss;完成后,代开浏览器,输入http://localhost:8080/jsflogin/login.faces,即出现如下界面:

输入相应的用户名,密码,Email,如图:
点击提交,出现如图所示:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值