JSF2自定义组件编程系列 第五部分

 在写这一章的时候,没有想到遇到很多的困难。现在简单的说一下:
1.添加taglib.xml文件里面的namespace看上去很美,但是带来了很大的困扰—EL表达式失效。这是我和另一位程序员在java.net上的帖子。
http://www.java.net/forum/topic/glassfish/glassfish-webtier/el-composite-component-taglib-jsf20
目前我的解决方案是绕过这个问题,只采用标准namespace,也就是http://java.sun.com/jsf/composite/tag_folder_path。同时我简单搜索了一下PrimeFaces的源代码,发现并没有采用Composite Component的方式实现,因为没有找到任何xhtml文件。

2.<<The Complete Reference-Java Server Faces2.0>>一书的问题
a.第11章333页,
public class loginPanel extends UINamingContainer
这行代码根本是错的,正确的应该是
public class loginPanel extends UIInput implements UINamingContainer
b.该书没有提供例子演示如何将一个拥有Backing class的Composite Component打包进一个jar包中,因此书中提供的代码片段不可信
c.xhtml中cc的别名和默认cc同名,混淆读者的概念

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html"
xmlns:cc="http://java.sun.com/jsf/composite">
<cc:interface/>
<cc:implementation>
  <h:panelGrid columns="3">
  <h:outputLabel for="#{cc.clientId}:userid" value="Userid:" />
  <h:inputText required="true"
    requiredMessage="Userid is required" id="userid" />
  <h:message for="#{cc.clientId}:userid" />
  <h:outputLabel for="#{cc.clientId}:password" value="Password:" />
  <h:inputSecret required="true"
    requiredMessage="Password is required" id="password" />
  <h:message for="#{cc.clientId}:password" />
  <h:outputText value="On Login, Go To:"
    rendered="#{! empty cc.facets.loginOutcomeChoiceList}"/>
  <h:commandButton id="loginButton" value="Login" />
  <h:messages for="#{cc.clientId}" />
  </h:panelGrid>
</cc:implementation>
</html>

上面的代码中,xmlns:cc="http://java.sun.com/jsf/composite 是容易误导人的,一般建议使用xmlns:composite而不是xmlns:cc。因为在后面#{cc.}的语法中,cc是预定义的Java对象,代 表这个Composite Component的顶层对象NamingContainer

不少问题直到看了<<Core JSF>>第三版,才明白。

好,现在开始。
首先实现htmlinput2.xhtml代码:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:composite="http://java.sun.com/jsf/composite">
  <composite:interface componentType="HtmlInput2">
    <composite:editableValueHolder name="inputField" target="in"/>
    <composite:valueHolder name="outputField" target="out"/>
  </composite:interface>
  <composite:implementation>
    <h:inputText id="in" required="true"/>
    <h:commandButton id="clickButton" value="Click Me!"/>
    <h:outputText id="out"/>
  </composite:implementation>
</html>

  composite:interface指定了componentType,JSF runtime用它在在faces-config.xml文件中查找到HtmleInput2类,该类实现了NamingContainer接口,继承了 UIInput类。JSF runtime会创建该类,将之作为Customized component的顶层对象。
  <component>
    <component-type>HtmlInput2</component-type>
    <component-class>com.freebird.component.HtmlInput2</component-class>
  </component>
  如果不指定componentType,JSF runtime会根据xhtml文件的名称htmlinput2去查找htmlinput2.java,如果找到,并且该类也实现了 NamingContainer接口,继承了UIInput类,则创建该类,将之作为Customized component的顶层对象。
  如果还找不到,JSF runtime会创建NamingContainer接口的默认实现类作为顶层对象。
  什么时候我们应该指定compnentType呢?当你想有一些java代码后台帮助tag处理一些行为的时候。
  其他的部分很简单,建议参考<<Core JSF>>第三版第九章。

现在来看一下HtmleInput2类的实现:
package com.freebird.component;

import javax.faces.component.NamingContainer;
import javax.faces.component.UIOutput;
import javax.faces.component.UIInput;
import javax.faces.event.ActionEvent;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.io.FileWriter;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.File;
import javax.faces.component.FacesComponent;
import javax.faces.event.ActionListener;
import javax.faces.convert.ConverterException;
import javax.faces.context.FacesContext;
import java.io.IOException;
import javax.faces.component.html.HtmlInputText;
import javax.faces.component.html.HtmlOutputText;
import javax.enterprise.context.ApplicationScoped;
import java.util.Map;

/**
  * Describe class HtmlInput2 here.
  *
  *
  * Created: Sat Jan 1 16:08:53 2011
  *
  * @author <a href="mailto:chenshu@csdesktop">chenshu</a>
  * @version 1.0
  /
public class HtmlInput2 extends UIInput implements NamingContainer{

  public HtmlInput2(){
    getLogger().info("HtmlInput2 constructor");
  }

  @Override
  public String getFamily() {
    return "javax.faces.NamingContainer";
  }

  private Logger getLogger(){
    return Logger.getLogger(HtmlInput2.class.getName());
  }

  public void print(ActionEvent event){
    getLogger().info("enter print method");
  }

  public void encodeBegin(FacesContext context) throws IOException {
    Map requestMap = context.getExternalContext().getRequestParameterMap();
    String clientId = getClientId(context);
    getLogger().info("clientId:"+clientId);
    String inputValue = (String)requestMap.get(clientId+":in");
    HtmlInputText input = (HtmlInputText) findComponent("in");
    if(null == input){
      getLogger().info("can't find input component instance");
      super.encodeBegin(context);
      return;
    }
    HtmlOutputText output = (HtmlOutputText)findComponent("out");
    if(null == output){
      getLogger().info("can't find output component instance");
      output.setValue("null");
      super.encodeBegin(context);
      return;
    }
    getLogger().info("input value is:"+inputValue);
    output.setValue(inputValue);
    super.encodeBegin(context);
  }
}

所有代码就这么多,当用户在页面上输入数据后,点击按钮,将会显示刚才输入的数据。具体例子我马上上传到我的空间里。

 

未完,待续



原文链接: http://blog.csdn.net/sheismylife/article/details/6121124

转载于:https://my.oschina.net/chen106106/blog/45723

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JSF(JavaServer Faces)和Spring MVC 是两种常用的Java Web应用程序框架,它们都基于MVC(Model-View-Controller)设计模式,但有一些区别。 下面是JSF和Spring MVC的一些主要区别: 1. 技术栈:JSF是Java EE(现在是Jakarta EE)规范的一部分,它是在Java EE容器中运行的。而Spring MVC是Spring Framework的一部分,它可以独立于Java EE容器运行。 2. 组件化 vs 注解驱动:JSF采用组件化编程模型,提供了丰富的UI组件库,并使用XML配置来定义页面和组件之间的关系。而Spring MVC则更加注重注解驱动的开发风格,通过注解将请求映射到处理方法,并使用模板引擎来渲染视图。 3. 配置方式:JSF通常使用XML配置文件定义页面、组件和导航规则等信息,这些配置文件位于WEB-INF目录下。而Spring MVC通过注解和配置类来定义请求映射、视图解析器、拦截器等配置信息,通常不需要使用XML配置文件。 4. 依赖注入:Spring MVC是Spring Framework的一部分,因此天然支持依赖注入(DI)和控制反转(IoC)。开发人员可以使用Spring的DI机制来管理和注入组件、服务和其他依赖项。而JSF并没有内置的依赖注入机制,但可以与其他框架(如Spring)集成来实现依赖注入。 5. 社区和生态系统:Spring MVC拥有庞大的开发者社区和丰富的生态系统,提供了大量的第三方库和工具支持。而JSF的社区相对较小,但仍然有一些活跃的开发者和项目。 选择使用JSF还是Spring MVC取决于你的需求、项目规模和个人偏好。如果你需要一个在Java EE容器中运行的框架,并且更喜欢组件化编程模型,那么JSF可能更适合你。如果你更倾向于注解驱动的开发风格、灵活的配置方式以及强大的依赖注入功能,那么Spring MVC可能更适合你。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值