爪哇一角

          共同探討STRUTS#HIBERNATE#SPRING#EJB等技術(shù)
          posts - 3, comments - 6, trackbacks - 0, articles - 99
            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

          簡單的JSF實現(xiàn)依賴注入的例子

          Posted on 2009-01-07 15:05 非洲小白臉 閱讀(426) 評論(0)  編輯  收藏 所屬分類: JSF

          首先,寫一個IUserService的接口,接口中只有屬性方法: getUsername, getPassword, setUsername, setPassword四個方法.代碼如下:

          package net.moon.service;
          public interface IUserService ...{
           String getUsername();
           String getPassword();
           void setUsername(String username);
           void setPassword(String password);
          }

          用一個類實現(xiàn)該接口,代碼如下:

          package net.moon.model;
          import net.moon.service.IUserService;
          public class UserInfo implements IUserService...{
           private String username;
           private String password;
           
          /** *//**
            * @return the usernmae
            */
           public String getUsername() ...{
            return username;
           }

          /** *//**
            * @param usernmae the usernmae to set
            */
           public void setUsername(String usernmae) ...{
            this.username = usernmae;
           }

           /** *//**
            * @return the password
            */
           public String getPassword() ...{
            return password;
           }

           /** *//**
            * @param password the password to set
            */

          public void setPassword(String password) ...{
            this.password = password;
           }
          }

          寫一個事務(wù)類,實現(xiàn)login功能,其中有一個類型為IUserService的域,代碼如下:

          package net.moon.business;

          import net.moon.service.IUserService;

          public class UserBO ...{

           IUserService user;
           
           public String login()...{
            String result = "FAILED";
            //System.out.println(result);
            if(user.getUsername().equalsIgnoreCase("admin")
              && user.getPassword().equals("password"))...{
             result = "PASS";
            }
            return result;
           }

           /** *//**
            * @return the user
            */

           public IUserService getUser() ...{
            return user;
           }

           /** *//**
            * @param user the user to set
            */
           public void setUser(IUserService user) ...{
            this.user = user;
           }
          }


          注意該類中的屬性方法setUser, 其中的參數(shù)user作為IUserService的接口類型.然后就是用什么方法對user進行注入,這時候就要想到JSF中的Managed Properties.

          首先在,faces-config中配置UserInfo類為MBean,代碼如下:

           <managed-bean>
            <managed-bean-name>userInfo</managed-bean-name>
            <managed-bean-class>net.moon.model.UserInfo</managed-bean-class>
            <managed-bean-scope>session</managed-bean-scope>
           </managed-bean>

           然后將事務(wù)類UserBO也配置為MBean,代碼如下:

           <managed-bean>
            <managed-bean-name>userBO</managed-bean-name>
            <managed-bean-class>
             net.moon.business.UserBO
            </managed-bean-class>

           <managed-bean-scope>request</managed-bean-scope>
            <managed-property>
             <property-name>user</property-name>
             <property-class>
              net.moon.service.IUserService
             </property-class>
             <value>#{userInfo}</value>
            </managed-property>

           </managed-bean>

          為userBO這個MBean配置了一個Managed Property,也就是要求JSF在實現(xiàn)userBO時, 用userInfo這個MBean為其user這個域賦值,從而實現(xiàn)注入.

          接下來就是頁面的實現(xiàn)了,首先是login頁面, 代碼如下:

          <%...@ page contentType="text/html; charset=UTF-8" %>
          <%...@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
          <%...@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>

          <html>
           <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
            <title></title>
           </head>
           <body>
            <f:view>
             <h:form>
            <h:panelGrid border="1" columns="2">
             <h:outputText value="User Name:"></h:outputText>
             <h:inputText value="#{userInfo.username}"></h:inputText>

             <h:outputText value="Password:"></h:outputText>
             <h:inputText value="#{userInfo.password}"></h:inputText>
             </h:panelGrid>
             <h:commandButton value="Login" action="#{userBO.login}"></h:commandButton>
           </h:form>
            </f:view>
           </body>
          </html>

          表示登錄成功的頁面welcome.jsp,代碼如下:

          <%...@ page contentType="text/html; charset=UTF-8" %>
          <%...@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
          <%...@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>

          <html>
           <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
            <title></title>
           </head>
           <body>
            <f:view>
             <h:form>
                  <h:outputText value="Welcome , #{userInfo.username}"></h:outputText>
                  </h:form>
            </f:view>
           </body>
          </html>

          配置導航如下:

           <navigation-rule>
            <display-name>login</display-name>
            <from-view-id>/login.jsp</from-view-id>
            <navigation-case>
             <from-action>#{userBO.login}</from-action>
             <from-outcome>PASS</from-outcome>
             <to-view-id>/welcome.jsp</to-view-id>
            </navigation-case>
            <navigation-case>
                      <from-action>#{userBO.login}</from-action>
                      <from-outcome>FAILED</from-outcome>
                      <to-view-id>/login.jsp</to-view-id>
                  </navigation-case>
           </navigation-rule>

          主站蜘蛛池模板: 北辰区| 沽源县| 遵化市| 天津市| 朝阳区| 威远县| 深州市| 吉林省| 东兰县| 茌平县| 阿拉善右旗| 聊城市| 开远市| 丹凤县| 紫阳县| 黄冈市| 永福县| 积石山| 千阳县| 陇川县| 铜梁县| 德庆县| 河津市| 文山县| 河源市| 沙河市| 北流市| 旅游| 邵阳市| 伊宁市| 南华县| 锡林郭勒盟| 石家庄市| 南阳市| 桂林市| 理塘县| 长海县| 庆元县| 卫辉市| 清涧县| 四川省|