本文介绍了向JSF 2.0 UIInput组件添加自定义属性(HTML5)支持的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图编写一个渲染器来处理< h:inputText> 上的占位符零件。
阅读,这似乎是正确的。这是我的自定义渲染器

  public class InputRenderer extends com.sun.faces.renderkit.html_basic.TextRenderer {

@覆盖
公共无效encodeBegin(FacesContext的上下文中,的UIComponent组件)
抛出IOException异常{
的System.out.println( 渲染: + component.getClientId());

字符串占位符=(String)component.getAttributes()。get(placeholder);
if(placeholder!= null){
ResponseWriter writer = context.getResponseWriter();
writer.writeAttribute(placeholder,placeholder,placeholder);
}

super.encodeBegin(context,component);



$ b @Override
public void decode(FacesContext context,UIComponent component){
super.decode(context,component) ;

$ b @Override
public void encodeEnd(FacesContext context,UIComponent component)
throws IOException {
super.encodeEnd(context,component);
}
}

此渲染器在faces config中注册为 p>

 < render-kit> 
<渲染器>
< component-family> javax.faces.Input< / component-family>
< renderer-type> javax.faces.Text< / renderer-type>
< renderer-class> com.example.renderer.InputRenderer< / renderer-class>
< /渲染器>
< / render-kit>

这样可以很好的记录,没有问题。

我的意图是处理占位符属性,插入它,然后将处理委托给super。我上面的代码不起作用,因为我在错误的地方插入了属性。它必须在 writer.startElement('input')执行后插入。但是,startElement必须发生在super的 encodeBegin()方法中的某处。那么如何插入自定义属性(在这种情况下为'placeholder'),然后继续执行流程?

注意:上面的代码确实添加了一个 placeholder 属性,但不是我想要的输入组件,它将它写入Input的父级(因为我试图在组件本身实际写入流,它将该属性应用到当前组件)

解决方案

这是我的方式。我添加了占位符和数据主题属性。如果你想添加更多的属性,你只需要将它的名字添加到属性数组中即可。



pre $ import javax.faces.component.UIComponent ;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;

import com.sun.faces.renderkit.html_basic.TextRenderer;

公共类InputRender延伸TextRenderer {

@覆盖
保护无效getEndTextToRender(FacesContext的上下文中,
的UIComponent组件,
字符串CurrentValue的)
抛出java.io.IOException {

String [] attributes = {placeholder,data-theme};

ResponseWriter writer = context.getResponseWriter();

for(String attribute:attributes)
{
String value =(String)component.getAttributes()。get(attribute);
if(value!= null){
writer.writeAttribute(attribute,value,attribute);
}
}

super.getEndTextToRender(context,component,currentValue);



$ b $ / code>

您应该添加这个到faces-config.xml文件。

 < render-kit> 
<渲染器>
< component-family> javax.faces.Input< / component-family>
< renderer-type> javax.faces.Text< / renderer-type>
< renderer-class> your.package.InputRenderer< / renderer-class>
< /渲染器>
< / render-kit>


I am trying to write a renderer which would process the placeholder attribute on an <h:inputText> component.I headed to this path after reading JSF 2.0 strips out needed HTML5 attributes and it seems correct. Here's my custom renderer

public class InputRenderer extends com.sun.faces.renderkit.html_basic.TextRenderer{

    @Override
    public void encodeBegin(FacesContext context, UIComponent component)
    throws IOException {
        System.out.println("Rendering :"+component.getClientId());

        String placeholder = (String)component.getAttributes().get("placeholder");
        if(placeholder != null) {
            ResponseWriter writer = context.getResponseWriter();
            writer.writeAttribute("placeholder", placeholder, "placeholder");
        }

        super.encodeBegin(context, component);

    }


    @Override
    public void decode(FacesContext context, UIComponent component) {
        super.decode(context, component);
    }

    @Override
    public void encodeEnd(FacesContext context, UIComponent component)
    throws IOException {
        super.encodeEnd(context, component);
    }
}

And this renderer is registered in faces config as

 <render-kit>
    <renderer>
        <component-family>javax.faces.Input</component-family>
        <renderer-type>javax.faces.Text</renderer-type>
        <renderer-class>com.example.renderer.InputRenderer</renderer-class>
    </renderer>
</render-kit>

This gets registered fine, no issues there.

My intention is to process the placeholder attribute, insert it, and then delegate the processing to super. My above code doesn't work because I'm inserting the attribute at a wrong place. It must be inserted after writer.startElement('input') has executed. However, the startElement must be happening somewhere in the super's encodeBegin() method. So how do I insert a custom attribute ('placeholder' in this case) and then continue the execution flow?

NB: The above code does add a placeholder attribute but not to the input component that I intend to, It writes it to the parent of the Input (since I'm trying to write an attribute before the component itself is actually written in the stream, it applies the attribute to the current component)

解决方案

This is my way. I added placeholder and data-theme attributes. If you want to add more attributes, you should just add its name to attributes array.

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;

import com.sun.faces.renderkit.html_basic.TextRenderer;

public class InputRender extends TextRenderer {

    @Override
    protected void getEndTextToRender(FacesContext context,
            UIComponent component,
            String currentValue)
     throws java.io.IOException{

        String [] attributes = {"placeholder","data-theme"};

        ResponseWriter writer = context.getResponseWriter();

        for(String attribute : attributes)
        {
            String value = (String)component.getAttributes().get(attribute);
            if(value != null) {
                writer.writeAttribute(attribute, value, attribute);
            }
        }

        super.getEndTextToRender(context, component, currentValue);

    }

}

You should add this to faces-config.xml file.

 <render-kit>
    <renderer>
        <component-family>javax.faces.Input</component-family>
        <renderer-type>javax.faces.Text</renderer-type>
        <renderer-class>your.package.InputRenderer</renderer-class>
    </renderer>
</render-kit>

这篇关于向JSF 2.0 UIInput组件添加自定义属性(HTML5)支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 15:32