blogjava's web log

          blogjava's web log
          ...

          jsf學(xué)習(xí)三(簡(jiǎn)單登陸)


          學(xué)習(xí)掌握:

          jsf 驗(yàn)證
          jsf 國(guó)際化
          jsf 導(dǎo)航

          login_zh_CN.properties

          title=第一個(gè)jsf程序
          username=用戶名
          password=密碼
          submit=登陸
          usererror=用戶名必須添
          passerror=密碼錯(cuò)誤


          這個(gè)文件放在 新建的? 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");//得到國(guó)際化資源文件
          ?????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配置,這就是動(dòng)態(tài)導(dǎo)航
          ????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;
          ??}
          //前臺(tái)驗(yàn)證?沒有填提示錯(cuò)誤信息?用戶必須填寫
          ??public?void?validateName(FacesContext?context,?UIComponent?toValidate,

          ???????????????????????????????Object?value)?
          throws?ValidatorException?{
          ?
          ????
          if(name.length()<?1||?length?>?16)?{
          ?????
          throw?new?ValidatorException(usernameErrorMessage);
          ????}
          ????
          return;
          ??}
          //驗(yàn)證密碼必填
          ??public?void?validatePassword(FacesContext?context,?UIComponent?toValidate,

          ???????????????????????????????Object?value)?
          throws?ValidatorException?{


          ????
          if(name.length()<?1||?length?>?16)?{
          ?????
          throw?new?ValidatorException(passwordErrorMessage);
          ????}
          ????
          return;
          ??}
          }




          前臺(tái)

          <!--導(dǎo)入jsf標(biāo)簽-->
          <%@taglib?uri="http://java.sun.com/jsf/html"?prefix="h"%>
          <%@taglib?uri="http://java.sun.com/jsf/core"?prefix="f"%>

          <!--國(guó)際化?文件-->
          <f:loadBundle?basename="firstjsf.login"?var="first"/>
          <f:view>
          <head>
          <title>
          <!--輸出標(biāo)題-->
          ??
          <h:outputText?value="#{first.title}"/>
          </title>
          </head>
          <body>
          <h:form>
          <!--輸出用戶名-->
          <h:outputText?value="#{first.username}"/>
          <!--文本框?用戶名?“l(fā)ogin"?在配置文件?實(shí)際是login.firstjsf.Login類?validator?驗(yàn)證-->
          <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>

          導(dǎo)航配置

          <navigation-rule>
          ????
          <!--from-view-id?標(biāo)識(shí)初始頁(yè)-->
          ????
          <from-view-id>/Logon.jsp</from-view-id>
          ?????
          <!--navigation-case?標(biāo)識(shí)一個(gè)導(dǎo)航塊-->?
          ?????
          <navigation-case>
          ??????
          <from-outcome>success</from-outcome>
          <!--to-view-id元素為這個(gè)導(dǎo)航塊指定目標(biāo)頁(yè)-->
          ??????
          <to-view-id>/ok.jsp</to-view-id>
          ????
          </navigation-case>
          ????
          <navigation-case>
          <!--from-outcome元素是navigation-rule中from-view-id子元素處理的結(jié)果-->

          ??????
          <from-outcome>failure</from-outcome>
          ??????
          <to-view-id>/error.jsp</to-view-id>
          ????
          </navigation-case>



          注:
          要實(shí)現(xiàn)動(dòng)態(tài)導(dǎo)航,按鈕或鏈接必須有一個(gè)方法引用,以用于調(diào)用相應(yīng)的方法,導(dǎo)航處理器根據(jù)方法返回的字符串來匹配導(dǎo)航規(guī)則

          posted on 2006-08-22 22:27 record java and net 閱讀(2538) 評(píng)論(2)  編輯  收藏 所屬分類: jsf學(xué)習(xí)

          評(píng)論

          # re: jsf學(xué)習(xí)三(簡(jiǎn)單登陸) 2008-05-27 14:10 wdiy

          import domain.SecurityManager;
          這個(gè)導(dǎo)入的包是什么啊。我怎么沒有啊。你用的是什么框架。  回復(fù)  更多評(píng)論   

          # re: jsf學(xué)習(xí)三(簡(jiǎn)單登陸) 2008-07-03 14:44 海中沙

          沒必要在托管BEAN里驗(yàn)證數(shù)據(jù)吧?直接在前端設(shè)置<f:validator/>進(jìn)行輸入驗(yàn)證會(huì)更好。  回復(fù)  更多評(píng)論   

          導(dǎo)航

          常用鏈接

          留言簿(44)

          新聞檔案

          2.動(dòng)態(tài)語言

          3.工具箱

          9.文檔教程

          友情鏈接

          搜索

          最新評(píng)論

          主站蜘蛛池模板: 富平县| 礼泉县| 浦江县| 读书| 孟州市| 玛沁县| 中宁县| 怀柔区| 弥渡县| 北碚区| 开封市| 洪泽县| 禄劝| 安丘市| 清水河县| 大理市| 克东县| 安龙县| 怀来县| 衡东县| 宣威市| 宁武县| 南安市| 雅安市| 昆明市| 肇东市| 汕头市| 沙坪坝区| 游戏| 任丘市| 开原市| 平遥县| 繁峙县| 巴里| 土默特右旗| 始兴县| 浦北县| 天门市| 威海市| 磴口县| 墨竹工卡县|