blogjava's web log

          blogjava's web log
          ...

          jsf學習三(簡單登陸)


          學習掌握:

          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配置,這就是動態(tài)導航
          ????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}"/>
          <!--文本框?用戶名?“l(fā)ogin"?在配置文件?實際是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子元素處理的結(jié)果-->

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



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

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

          評論

          # re: jsf學習三(簡單登陸) 2008-05-27 14:10 wdiy

          import domain.SecurityManager;
          這個導入的包是什么啊。我怎么沒有啊。你用的是什么框架。  回復  更多評論   

          # re: jsf學習三(簡單登陸) 2008-07-03 14:44 海中沙

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

          導航

          常用鏈接

          留言簿(44)

          新聞檔案

          2.動態(tài)語言

          3.工具箱

          9.文檔教程

          友情鏈接

          搜索

          最新評論

          主站蜘蛛池模板: 青铜峡市| 阜宁县| 宿州市| 丹东市| 简阳市| 四子王旗| 微山县| 越西县| 蒙自县| 富川| 台南县| 秦安县| 唐河县| 株洲市| 镇江市| 武强县| 天峨县| 江山市| 彭阳县| 平度市| 家居| 石屏县| 黑河市| 清镇市| 玉田县| 富锦市| 通城县| 瑞丽市| 贵溪市| 信阳市| 金塔县| 敦煌市| 哈巴河县| 青龙| 手游| 辛集市| 弥勒县| 新宁县| 遂昌县| 娄烦县| 班玛县|