Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 304|回复: 0

[jsf学习]JSF实例学习--JSF Weekly 电子报订阅

[复制链接]
  • TA的每日心情
    开心
    2021-3-12 23:18
  • 签到天数: 2 天

    [LV.1]初来乍到

    发表于 2014-10-10 03:54:38 | 显示全部楼层 |阅读模式
    JSF Weekly 电子报订阅。这是网上来的一个JSF入门实例,原来它是jsf1.0版本的,我将其改为了jsf1.1版。





      这个例子主要有三个特点:
    一、中英文界面且资源包使用类,如中文资源包:
    1. /*
    2. * $Id: Resources_zh.java,v 1.2 2003/06/17 01:14:03 jack Exp $
    3. *
    4. */
    5. package net.jackwind.jsf;
    6. // Chinese lang. resource bundle.
    7. public class Resources_zh extends java.util.ListResourceBundle {
    8.         static final Object[][] contents = new String[][] {
    9.                 { "locale", "请选择您的语言" },
    10.                 { "title", "JSF 演示:  电子报订阅系统 " },
    11.                 { "prompt", "您正在订阅" },
    12.                 { "newsletter", "电子报" },
    13.                 { "email", "电子信箱" },       
    14.                 { "password", "密码" },       
    15.                 { "confirmPassword", "确认密码" },       
    16.                 { "format", "电子邮件格式" },       
    17.                 { "donotuse", "请不要寄给我更多广告信息" },
    18.                 { "submit", "订阅" },
    19.                 { "url-welcome", "首页" },
    20.                 { "url-subscribe", "订阅" },
    21.                 { "passwords-not-match", "密码与重覆密码不相配。 " },
    22.                 { "go-back", "退回" },
    23.                 { "thank-you", "您的订阅申请已被提交,感谢支持。" },
    24.                 { "details", "订阅详细信息" }
    25.         };
    26.        
    27.         public Object[][] getContents() {
    28.                 return contents;
    29.         }
    30. }
    复制代码
    二、定义了一个组件,它将用户提交的邮件地址输出时添加了一个email链接,这仅仅为了学习自定义组件。如下如示:





      
    1. /*
    2. * $Id: UIOutputEmail.java,v 1.1 2003/06/20 16:48:49 jack Exp $
    3. *
    4. */
    5. package net.jackwind.jsf;
    复制代码
    import java.io.IOException;
    import java.util.StringTokenizer;
    import javax.faces.component.UIOutput;
    import javax.faces.context.FacesContext;
    import javax.faces.context.ResponseWriter; /**
    * @author JACK (Jun 19, 2003)
    * @class UIOutputEmail
    */
      
       
       
       

       
      

    1.   public class UIOutputEmail extends UIOutput {

    2.   private String host;

    3.   private String user;

    4.   

    5.   private void parseEmail(String email) {

    6.      StringTokenizer st = new StringTokenizer(email, "@");

    7.   i   f (st.countTokens() != 2)

    8.        throw new IllegalArgumentException("Invalid email address: " + email);

    9.      user = st.nextToken();

    10.      host = st.nextToken();

    11.   }

    12.   

    13.   // This method indicates whether this component renders itself

    14.   // or delegates rendering to a renderer.

    15.   public boolean getRendersSelf() {

    16.       return true;

    17.   }
    复制代码
    /**
    * Decode the current state of this UIComponent from the request contained in
    * the specified FacesContext, and attempt to convert this state information
    * into an object of the required type for this component.
    */
    // public void decode(FacesContext context) throws IOException {
    // // Not needed.
    // }

    // Called during the Render Response phase
    public void encodeEnd(FacesContext context) throws IOException {
        if(user == null || host == null) {
        try {
             String clientId = getClientId(context);
             parseEmail((String)getValue());
        }catch(Exception e) {
           //System.out.println(e);
        }
       }
        ResponseWriter writer = context.getResponseWriter();

    // Represent this component as HTML
        writer.write("<script language="JavaScript">
    ");
        writer.write("function emailAddress(host, user) {
    ");
        writer.write("document.write("<a href="mailto:");
    ");
        writer.write("document.write(user + "@" + host);
    ");
        writer.write("document.write("">");
    ");
        writer.write("document.write(user + "@" + host);
    ");
        writer.write("document.write("</a>");
    }

    ");
        writer.write("emailAddress("" + host + "", "" + user + "");
    ");
        writer.write("</script>
    ");
       }
    } 自定义组件的标记类:
    1. /*
    2. * $Id: UIOutputEmailTag.java,v 1.1 2003/06/20 16:48:49 jack Exp $
    3. *
    4. */
    5. package net.jackwind.jsf;
    6. import javax.faces.component.UIComponent;
    7. import javax.faces.webapp.UIComponentTag;
    8. import javax.faces.context.FacesContext;
    9. import javax.faces.application.Application;
    10. import javax.faces.el.ValueBinding;
    11. /**
    12. * @author         JACK        (Jun 19, 2003)
    13. * @class         UIOutputEmail
    14. */
    15. public class UIOutputEmailTag extends UIComponentTag {
    16.         private String valueRef;
    17.         private String value;
    18.         public  String getComponentType() {
    19.                 return "UIOutputEmail";
    20.         }
    21.         public void setValue(String value) {
    22.                 this.value = value;
    23.         }
    24.         public String getValue() {
    25.                 return value;
    26.         }
    27.         public String getValueRef() {
    28.                 return valueRef;
    29.         }
    30.         public void setValueRef(String newValueRef) {
    31.                 valueRef = newValueRef;
    32.         }
    33.         /**
    34.          * Return the rendererType property that selects the Renderer to be used
    35.          * for encoding this component, or null to ask the component to render
    36.          * itself directly. Subclasses must override this method to return the
    37.          * appropriate value.
    38.          */
    39.         public String getRendererType() {
    40.                 return null;
    41.         }
    42.          public void setProperties(UIComponent component) {
    43.             super.setProperties(component);
    44.             setStringProperty(component, "value", value);  
    45.             setStringProperty(component, "valueRef", valueRef);   
    46.          }  
    47.        private void setStringProperty(UIComponent component, String attrName, String attrValue) {   
    48.            if(attrValue == null)   
    49.               return;   
    50.            if(isValueReference(attrValue)) {
    51.                 FacesContext context =FacesContext.getCurrentInstance();  
    52.                 Application application =context.getApplication();
    53.                 ValueBinding binding =application.createValueBinding(attrValue);     
    54.                 component.setValueBinding(attrName, binding);
    55.            }  
    56.            else {
    57.                    component.getAttributes().put(attrName, attrValue);  
    58.            }
    59.       }  
    60.       public void release() {  
    61.          super.release();
    62.          value = null;
    63.          valueRef = null;  
    64.       }
    65. }
    复制代码


    三、自定义了邮件地址的验证器:
    /*
    * $Id: EmailAddressValidator.java,v 1.2 2003/06/20 16:48:49 jack Exp $
    *
    */
    package net.jackwind.jsf; import java.util.regex.Matcher;
    import java.util.regex.Pattern;
      import javax.faces.application.FacesMessage;
    import javax.faces.component.UIComponent;
    import javax.faces.component.UIOutput;
    import javax.faces.context.FacesContext;
    import javax.faces.validator.*; /**
    * @author JACK (Jun 16, 2003)
    * @class EmailValidator
    */
    public class EmailAddressValidator implements Validator {  /**
    * The message identifier of the Message to be created if
    * the validation fails.
    */
    public static final String EMAIL_ADDRESS_INVALID ="net.jackwind.jsf.EMAIL_ADDRESS_INVALID";  // Email address pattern. Used by regex.
    public static String PATTERN_EMAIL ="[a-zA-Z0-9][\w\.\-]*@[a-zA-Z0-9][\w\.\-]*\.[a-zA-Z][a-zA-Z\.]*";
    private Pattern pattern = Pattern.compile(PATTERN_EMAIL);  public void validate(FacesContext context, UIComponent component,Object ob) {
       if ((context == null) || (component == null)) {
           throw new NullPointerException();
       }
        if (!(component instanceof UIOutput)) {
          return;
       }

       Object componentValue = ((UIOutput) component).getValue();
       String value = (componentValue == null ? null : componentValue.toString());
       if(value == null)
         return;

        if (!validateEmailAddress(value)) {

         FacesMessage message = new FacesMessage( FacesMessage.SEVERITY_ERROR, "邮件地址错误", "邮件地址错误");
         throw new ValidatorException(message);

        }
    }  private boolean validateEmailAddress(String emailAddress) {
         if(emailAddress == null) return false;
         Matcher matcher = pattern.matcher(emailAddress);
         return matcher.matches();
    } }
    请下载全部源代码学习。

      
      
       
       

         
       

         
       
      



    源码下载:http://file.javaxxz.com/2014/10/10/035436750.zip
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2025-5-8 05:51 , Processed in 0.302442 second(s), 36 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表