<%@ page contentType="text/html; charset=gb2312"%> JSF实例学习--JSF Weekly 电子报订阅
网站公告:   ◆北天JAVA技术网热情为java爱好者服务,本网内容包括JAVA(JSP、servlet、EJB、webservice、j2ee、javabean、应用服务器、JavaScript),数据库(MYSQL、SQL Server、Sybase、Oracle、DB2、数据库综合知识),设计研究(设计模式、Struts、Spring、Hibernate、设计框架、设计综合知识),WEB2.0新技术(主要介绍AJAX),以及各种技术的入门、实例、例子等等,欢迎各位多来坐坐!◆  诚邀各位JAVA爱好者加盟!◆  本网站内容丰富,更新快,保证每周20篇以上!  
加入收藏
设为首页
联系站长
承接项目
  相关资源:网站首页 | 免费培训学院 | 技术论坛 | JAVA聊天室 | 作家专栏 | 开发工具 | 认证考试 | 会员俱乐部
  JAVA技术初学者园地 | jsp与servlet | javascript | Java源代码 | EJB | web service | 应用服务器 | JAVA综合知识
  设计研究设计模式 | 设计框架 | Struts | Spring | Hibernate | 开源项目 | 面向对象设计 | 设计综合知识
  数 据 库MYSQL | SQL Server | Sybase | Oracle | DB2 | Informix | Access | 数据库综合知识
  其他资源:AJAX新技术 | 网站开发 | ERP软件 | OA办公软件 | 商业智能BI | 开发综合知识 | 承接项目 | 项目试用

 
 
JSF实例学习--JSF Weekly 电子报订阅
     发布者: 发布时间:2006-10-26
   JSF Weekly 电子报订阅。这是网上来的一个JSF入门实例,原来它是jsf1.0版本的,我将其改为了jsf1.1版。






这个例子主要有三个特点:
一、中英文界面且资源包使用类,如中文资源包:

/*
 * $Id: Resources_zh.java,v 1.2 2003/06/17 01:14:03 jack Exp $
 * 
 */
package net.jackwind.jsf;

// Chinese lang. resource bundle.

public class Resources_zh extends java.util.ListResourceBundle {
	static final Object[][] contents = new String[][] { 
		{ "locale", "请选择您的语言" },
		{ "title", "JSF 演示:  电子报订阅系统 " },
		{ "prompt", "您正在订阅" },
		{ "newsletter", "电子报" },
		{ "email", "电子信箱" },	
		{ "password", "密码" },	
		{ "confirmPassword", "确认密码" },	
		{ "format", "电子邮件格式" },	
		{ "donotuse", "请不要寄给我更多广告信息" },
		{ "submit", "订阅" },
		{ "url-welcome", "首页" },
		{ "url-subscribe", "订阅" },
		{ "passwords-not-match", "密码与重覆密码不相配。 " }, 
		{ "go-back", "退回" },
		{ "thank-you", "您的订阅申请已被提交,感谢支持。" }, 
		{ "details", "订阅详细信息" }
	};
	
	public Object[][] getContents() {
		return contents;
	}
}
二、定义了一个组件,它将用户提交的邮件地址输出时添加了一个email链接,这仅仅为了学习自定义组件。如下如示:




/*
* $Id: UIOutputEmail.java,v 1.1 2003/06/20 16:48:49 jack Exp $
*
*/
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
*/

  public class UIOutputEmail extends UIOutput {
private String host;
private String user;

private void parseEmail(String email) {
   StringTokenizer st = new StringTokenizer(email, "@");
i   f (st.countTokens() != 2)
     throw new IllegalArgumentException("Invalid email address: " + email);
   user = st.nextToken();
   host = st.nextToken();
}

// This method indicates whether this component renders itself
// or delegates rendering to a renderer.
public boolean getRendersSelf() {
    return true;
}

/**
* 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'>\n");
   writer.write("function emailAddress(host, user) { \n");
   writer.write("document.write('<a href=\"mailto:');\n");
   writer.write("document.write(user + '@' + host);\n");
   writer.write("document.write('\">');\n");
   writer.write("document.write(user + '@' + host);\n");
   writer.write("document.write(\"</a>\");\n}\n\n");
   writer.write("emailAddress('" + host + "', '" + user + "');\n");
   writer.write("</script>\n");
  }
}

自定义组件的标记类:

/*
 * $Id: UIOutputEmailTag.java,v 1.1 2003/06/20 16:48:49 jack Exp $
 * 
 */
package net.jackwind.jsf;

import javax.faces.component.UIComponent;
import javax.faces.webapp.UIComponentTag;
import javax.faces.context.FacesContext;
import javax.faces.application.Application;
import javax.faces.el.ValueBinding;

/**
 * @author 	JACK	(Jun 19, 2003)
 * @class 	UIOutputEmail
 */
public class UIOutputEmailTag extends UIComponentTag {

	private String valueRef;
	private String value;



	public  String getComponentType() {
		return "UIOutputEmail";
	}

	public void setValue(String value) {
		this.value = value;
	}

	public String getValue() {
		return value;
	}

	public String getValueRef() {
		return valueRef;
	}

	public void setValueRef(String newValueRef) {
		valueRef = newValueRef;
	}

	/**
	 * Return the rendererType property that selects the Renderer to be used 
	 * for encoding this component, or null to ask the component to render 
	 * itself directly. Subclasses must override this method to return the 
	 * appropriate value. 
	 */
	public String getRendererType() {
		return null;
	}

	 public void setProperties(UIComponent component) { 
            super.setProperties(component); 
            setStringProperty(component, "value", value);  
            setStringProperty(component, "valueRef", valueRef);   
         }  

       private void setStringProperty(UIComponent component, String attrName, String attrValue) {    
           if(attrValue == null)   
              return;    
           if(isValueReference(attrValue)) { 
                FacesContext context =FacesContext.getCurrentInstance();  
                Application application =context.getApplication(); 
                ValueBinding binding =application.createValueBinding(attrValue);     
                component.setValueBinding(attrName, binding);
           }  
           else { 
                   component.getAttributes().put(attrName, attrValue);  
           } 
      }  

      public void release() {  
         super.release(); 
         value = null;
         valueRef = null;  
      }

}


三、自定义了邮件地址的验证器:
/*
* $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();
}

}
请下载全部源代码学习。

下载源代码

 

(转载文章请保留出处:北天JAVA技术网(www.java114.com))
 
更多精彩文章:
他们在你的网站上做什么?
Cookie手册
用JAVA写一个日志类程序
审查Java代码的十一种常见错误
JAVA基础:谨慎使用Date和Time类
简单级联菜单(javascript实现)
 
最近评论:
        
你曾悄悄的来过!
wow gold,wow gold,wow gold,ffxi gil max(6347)
        
冰封的往事!
wow power leveling,wow gold,wow power leveling,wow gold max(9012)
        
冰封的往事!
wow power leveling,wow gold,WoW Gold,wow gold max(6635)
        
标 题:   
内 容:   
 
                                  
 
免责声明:该文章由网友发表,如果对您造成侵权,请联系站长

首页 - 承接项目 - 网站地图 - 联系我们 -
版权所有北天JAVA技术工作室 ICP证号:粤ICP备06079815号