站内搜索: 请输入搜索关键词

当前页面: 开发资料首页Java 专题JSF实例学习--JSF Weekly 电子报订阅

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

摘要: JSF实例学习--JSF Weekly 电子报订阅

</td> </tr> <tr> <td height="35" valign="top" class="ArticleTeitle">

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链接,这仅仅为了学习自定义组件。如下如示:



<table width="655" height="47" border="0"> <tr> <td width="381">

/*
* $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
*/

</td> <td width="264"> </td> </tr> </table>

  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(' writer.write("document.write(user + '@' + host);\n");
writer.write("document.write('\">');\n");
writer.write("document.write(user + '@' + host);\n");
writer.write("document.write(\"\");\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();
}

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


</td> </tr> <tr>


↑返回目录
前一篇: 他们在你的网站上做什么?
后一篇: 学习笔记:使用JSF构建数据库驱动的应用程序