1 Reply Latest reply on Jan 7, 2011 11:02 AM by Babu Raj

    JSF 1.2 Custom Component Strange Problem

    Babu Raj Newbie

      1) I have created custom component for search action

               1) A text field

               2) A button

               3) A combo box

      2) We can enter something inside textfield and click on the button will populate data to the combo box .. that is the idea

                <a:searchField id="HelloId" searchImageSource="/icons/search_lens.png"
                      selectItems="#{dateTest.searchedItem }"
                      searchText="#{dateTest.searchText}" action="#{dateTest.handle }" selectedString="#{dateTest.selectedValue }">

                  </a:searchField>

      3) i am currently concentrating to one issue ie; the searchText getter and setter for the bean is not calling after the button click...the page is refreshing ...the call will reach until TAG class...but its not calling the Value Expression

       

      4) the value expression is saving to the Map

       

      The details is shown below. Can anybody explain on which phase(Java class) the getter and setter of the value expression being called...

      Please give me advice if im doing anything wrong..

       

       

       

      package com.application.core.jsf.components.taglib;

       

      import java.util.List;

       

      import javax.el.ExpressionFactory;

      import javax.el.MethodExpression;

      import javax.faces.application.Application;

      import javax.faces.component.UIComponent;

      import javax.faces.context.FacesContext;

      import javax.faces.webapp.UIComponentELTag;

       

      import com.application.core.jsf.components.UISearchField;

      import com.application.core.jsf.components.taglib.util.TagLibUtil;

       

      /**

      *

      * @author baburaj

      *

      */

      public class SearchFieldTag extends UIComponentELTag {

       

          private Boolean immediate;

       

          private String style;

       

          private String styleClass;

       

          private String border;

       

          private String searchText;

       

          private String searchImageSource;

       

          private String selectItems;

       

          private String selectedString;

       

          private String action;

       

          public SearchFieldTag() {

              super();

          }

       

          public String getComponentType() {

              return UISearchField.COMPONENT_TYPE;

          }

       

          public String getRendererType() {

              return null;

          }

       

          private static MethodExpression getMethodExpression(FacesContext context, String attribute, String value, Class<?> dataType) {

              Application application = context.getApplication();

              ExpressionFactory factory = application.getExpressionFactory();

              MethodExpression methodExpression = factory.createMethodExpression(context.getELContext(), value, null, new Class[] {});

              return methodExpression;

          }

       

          protected void setProperties(UIComponent component) {

       

              super.setProperties(component);

              if (selectItems != null) {

                  if (TagLibUtil.isValueReference(selectItems)) {

                      TagLibUtil.setValueExpression(getFacesContext(), "selectItems", selectItems, component, List.class);

                  }

              }

              if (searchText != null) {

                  if (TagLibUtil.isValueReference(searchText)) {

                      TagLibUtil.setValueExpression(getFacesContext(), "searchText", searchText, component, String.class);

                  }

                  else {

                      component.getAttributes().put("searchText", searchText);

                  }

       

              }

              if (selectedString != null) {

                  if (TagLibUtil.isValueReference(selectedString)) {

                      TagLibUtil.setValueExpression(getFacesContext(), "selectedString", selectedString, component, String.class);

                  }

                  else {

                      component.getAttributes().put("selectedString", selectedString);

                  }

              }

              if (action != null) {

                  if (TagLibUtil.isValueReference(action)) {

                      MethodExpression me = getMethodExpression(getFacesContext(), "action", action, null);

                      component.getAttributes().put("actionExpression", me);

       

                  }

              }

              if (searchImageSource != null) {

                  component.getAttributes().put("searchImageSource", searchImageSource);

              }

              if (style != null) {

                  component.getAttributes().put("style", style);

              }

              if (styleClass != null) {

                  component.getAttributes().put("styleClass", styleClass);

              }

              if (border != null) {

                  component.getAttributes().put("border", border);

              }

       

              if (immediate != null) {

                  component.getAttributes().put("immediate", immediate.booleanValue());

              }

          }

       

          public void release() {

              super.release();

              immediate = null;

              searchImageSource = null;

              searchText = null;

              selectedString = null;

              selectItems = null;

          }

       

          public Boolean getImmediate() {

              return immediate;

          }

       

          public void setImmediate(Boolean immediate) {

              this.immediate = immediate;

          }

       

          /**

           * @return the style

           */

          public String getStyle() {

              return style;

          }

       

          /**

           * @param style

           *            the style to set

           */

          public void setStyle(String style) {

              this.style = style;

          }

       

          /**

           * @return the styleClass

           */

          public String getStyleClass() {

              return styleClass;

          }

       

          /**

           * @param styleClass

           *            the styleClass to set

           */

          public void setStyleClass(String styleClass) {

              this.styleClass = styleClass;

          }

       

          /**

           * @return the border

           */

          public String getBorder() {

              return border;

          }

       

          /**

           * @param border

           *            the border to set

           */

          public void setBorder(String border) {

              this.border = border;

          }

       

          /**

           * @return the searchText

           */

          public String getSearchText() {

              return searchText;

          }

       

          /**

           * @param searchText

           *            the searchText to set

           */

          public void setSearchText(String searchText) {

       

              this.searchText = searchText;

          }

       

          /**

           * @return the searchImageSource

           */

          public String getSearchImageSource() {

              return searchImageSource;

          }

       

          /**

           * @param searchImageSource

           *            the searchImageSource to set

           */

          public void setSearchImageSource(String searchImageSource) {

              this.searchImageSource = searchImageSource;

          }

       

          /**

           * @return the selectItems

           */

          public String getSelectItems() {

              return selectItems;

          }

       

          /**

           * @param selectItems

           *            the selectItems to set

           */

          public void setSelectItems(String selectItems) {

              this.selectItems = selectItems;

          }

       

          /**

           * @return the selectedString

           */

          public String getSelectedString() {

              return selectedString;

          }

       

          /**

           * @param selectedString

           *            the selectedString to set

           */

          public void setSelectedString(String selectedString) {

              this.selectedString = selectedString;

          }

       

          /**

           * @return the action

           */

          public String getAction() {

              return action;

          }

       

          /**

           * @param action

           *            the action to set

           */

          public void setAction(String action) {

              this.action = action;

          }

       

      }

       

       

       

       

       

       

      package com.application.core.jsf.components;

       

      import java.io.IOException;

      import java.util.ArrayList;

      import java.util.HashMap;

      import java.util.List;

      import java.util.Map;

      import java.util.Set;

       

      import javax.el.MethodExpression;

      import javax.el.ValueExpression;

      import javax.faces.component.NamingContainer;

      import javax.faces.component.UIInput;

      import javax.faces.context.FacesContext;

      import javax.faces.context.ResponseWriter;

      import javax.faces.model.SelectItem;

       

      /**

      *

      * @author baburaj

      *

      */

      public class UISearchField extends UIInput {

       

          public final static String COMPONENT_TYPE = "UISearchField";

       

          public final static String COMPONEN_FAMILY = "UISearchField";

       

          protected final static String SEARCH_TEXT_KEY = "searchTextKey";

       

          protected final static String SEARCH_SELECT_BOX_KEY = "searchSelectBoxKey";

       

          protected final static String SEARCH_BUTTON_KEY = "searchButtonKey";

       

          private String searchText = "";

       

          private String searchImageSource;

       

          private List<SelectItem> selectItems;

       

          private String selectedString = "";

       

          private String styleClass;

       

          private String style;

       

          private String border;

       

          private MethodExpression actionExpression;

       

          public UISearchField() {

              super();

              setRendererType(null);

              selectItems = new ArrayList<SelectItem>(1);

          }

       

          public String getFamily() {

              return COMPONENT_FAMILY;

          }

       

          // Encoding methods

       

          public void encodeEnd(FacesContext context) throws IOException {

              if (!isRendered()) {

                  return;

              }

       

              ResponseWriter writer = context.getResponseWriter();

              writer.startElement("table", this);

              writer.writeAttribute("cellPadding", 0, "cellPadding");

              writer.writeAttribute("cellSpacing", 0, "cellSpacing");

              if (border != null) {

                  try {

                      Integer.parseInt(border.trim());

                  }

                  catch (NumberFormatException e) {

                      throw new IllegalStateException("border property for component <inputTime> will accepts only integer. border = "

                              + border, e);

       

                  }

                  writer.writeAttribute("border", border.trim(), "border");

              }

       

              writer.startElement("tr", this);

              writer.writeAttribute("align", "left", "align");

              writer.startElement("td", this);

       

              writer.startElement("table", this);

              writer.writeAttribute("cellPadding", 0, "cellPadding");

              writer.writeAttribute("cellSpacing", 0, "cellSpacing");

       

              writer.startElement("tr", this);

              writer.startElement("td", this);

              writer.writeAttribute("align", "left", "align");

       

              writer.startElement("input", this);

              writer.writeAttribute("type", "text", "type");

              writer.writeAttribute("name", getFieldKey(context, SEARCH_TEXT_KEY), null);

              if (styleClass != null) {

                  writer.writeAttribute("class", styleClass, "styleClass");

              }

              if (style != null) {

                  writer.writeAttribute("style", style, "style");

              }

              writer.writeAttribute("value", searchText.trim(), "value");

              writer.endElement("input");

       

              writer.endElement("td");

              writer.startElement("td", this);

              writer.writeAttribute("style", "width: 5px;", "style");

              writer.endElement("td");

       

              writer.startElement("td", this);

              writer.startElement("input", this);

              writer.writeAttribute("type", "image", "type");

              writer.writeAttribute("src", searchImageSource.trim(), "src");

              writer.writeAttribute("name", getFieldKey(context, SEARCH_BUTTON_KEY), null);

              writer.endElement("input");

              writer.endElement("td");

       

              writer.endElement("tr");

       

              writer.endElement("table");

       

              writer.endElement("td");

       

              writer.endElement("tr");

       

              writer.startElement("tr", this);

       

              displaySelectBox(selectedString, context, writer);

       

              writer.endElement("tr");

              writer.endElement("table");

          }

       

          protected void displayOption(SelectItem item, String selectedString, ResponseWriter writer) throws IOException {

              writer.startElement("option", this);

              writer.writeAttribute("value", item.getValue(), null);

              if (selectedString.trim().equals(item.getLabel().trim())) {

                  writer.writeAttribute("selected", "true", null);

              }

              writer.writeText(item.getLabel().trim(), null);

              writer.endElement("option");

          }

       

          private void displaySelectBox(String selectedString, FacesContext context, ResponseWriter writer) throws IOException {

              writer.startElement("td", this);

              writer.writeAttribute("align", "left", "align");

              writer.startElement("select", this);

              writer.writeAttribute("name", getFieldKey(context, SEARCH_SELECT_BOX_KEY), null);

              if (styleClass != null) {

                  writer.writeAttribute("class", styleClass, "styleClass");

              }

              if (style != null) {

                  writer.writeAttribute("style", style, "style");

              }

              for (SelectItem item : selectItems) {

                  displayOption(item, selectedString, writer);

              }

       

              writer.endElement("select");

              writer.endElement("td");

          }

       

          // Decoding methods

       

          public void decode(FacesContext context) {

              Map<String, String> requestParameterMap = context.getExternalContext().getRequestParameterMap();

       

              String searchSelectBoxKey = getFieldKey(context, SEARCH_SELECT_BOX_KEY);

       

              String value = "";

       

              if (requestParameterMap.containsKey(searchSelectBoxKey)) {

                  value = requestParameterMap.get(searchSelectBoxKey);

              }

              activate();

              System.out.println("---------------------------------------------");

              System.out.println(getAttributes().get("rendered"));

              System.out.println(getAttributes().get("searchText"));

              System.out.println(getAttributes().get("searchImageSource"));

              System.out.println(getAttributes().get("actionExpression"));

              System.out.println(getAttributes().get("styleClass"));

              System.out.println(getAttributes().get("style"));

              System.out.println(getAttributes().get("border"));

              System.out.println(getAttributes().get("value"));

              System.out.println(getAttributes().get("selectItems"));

              setSubmittedValue(value);

          }

       

          private void activate() {

              {

                  Set<String> keySet = getAttributes().keySet();

                  for (String s : keySet) {

                      System.out.println("String        => " + s);

                      System.out.println("value        => " + getAttributes().get(s));

                  }

       

              }

       

              Set<String> keySet = bindings.keySet();

              for (String s : keySet) {

                  System.out.println("String        => " + s);

                  if (s.equals("searchText")) {

                      ValueExpression v = bindings.get(s);

                      searchText = v.getValue(getFacesContext().getELContext()).toString();

                      System.out.println("VVVVVVVVVVVVVVVVVVV     " + searchText);

                  }

              }

          }

       

          protected String getFieldKey(FacesContext context, String suffix) {

              return getClientId(context) + NamingContainer.SEPARATOR_CHAR + suffix;

          }

       

          public Object saveState(FacesContext context) {

              Map<String, Object> values = new HashMap<String, Object>();

              values.put("super", super.saveState(context));

              values.put("actionExpression", saveAttachedState(context, actionExpression));

       

              values.put("searchImageSource", searchImageSource);

       

              if (styleClass != null) {

                  values.put("styleClass", styleClass);

              }

              if (style != null) {

                  values.put("style", style);

              }

              if (border != null) {

                  values.put("border", border);

              }

              return values;

          }

       

          @SuppressWarnings("unchecked")

          public void restoreState(FacesContext context, Object state) {

       

              Map<String, Object> values = (Map<String, Object>) state;

              super.restoreState(context, values.get("super"));

              actionExpression = (MethodExpression) restoreAttachedState(context, values.get("actionExpression"));

              actionExpression.invoke(context.getELContext(), null);

       

              searchImageSource = values.get("searchImageSource").toString();

              if (values.get("styleClass") != null) {

                  styleClass = values.get("styleClass").toString();

              }

              if (values.get("style") != null) {

                  style = values.get("style").toString();

              }

              if (values.get("border") != null) {

                  border = values.get("border").toString();

              }

       

          }

       

          public void setRendered(boolean rendered) {

              super.setRendered(rendered);

          }

       

          /**

           * @return the styleClass

           */

          public String getStyleClass() {

              return styleClass;

          }

       

          /**

           * @param styleClass

           *            the styleClass to set

           */

          public void setStyleClass(String styleClass) {

              this.styleClass = styleClass;

          }

       

          /**

           * @return the style

           */

          public String getStyle() {

              return style;

          }

       

          /**

           * @param style

           *            the style to set

           */

          public void setStyle(String style) {

              this.style = style;

          }

       

          /**

           * @return the border

           */

          public String getBorder() {

              return border;

          }

       

          /**

           * @param border

           *            the border to set

           */

          public void setBorder(String border) {

              this.border = border;

          }

       

          /**

           * @return the searchText

           */

          public String getSearchText() {

              return searchText;

          }

       

          /**

           * @param searchText

           *            the searchText to set

           */

          public void setSearchText(String searchText) {

              this.searchText = searchText;

          }

       

          /**

           * @return the selectItems

           */

          public List<SelectItem> getSelectItems() {

              return selectItems;

          }

       

          /**

           * @param selectItems

           *            the selectItems to set

           */

          public void setSelectItems(List<SelectItem> selectItems) {

              this.selectItems = selectItems;

          }

       

          /**

           * @return the searchImageSource

           */

          public String getSearchImageSource() {

              return searchImageSource;

          }

       

          /**

           * @param searchImageSource

           *            the searchImageSource to set

           */

          public void setSearchImageSource(String searchImageSource) {

              this.searchImageSource = "/common" + searchImageSource;

          }

       

          /**

           * @return the selectedString

           */

          public String getSelectedString() {

              return selectedString;

          }

       

          /**

           * @param selectedString

           *            the selectedString to set

           */

          public void setSelectedString(String selectedString) {

              this.selectedString = selectedString;

          }

       

          private static String src(FacesContext context, String imageURI) {

       

              if (imageURI == null) {

                  return "";

              }

       

              String u = context.getApplication().getViewHandler().getResourceURL(context, imageURI);

              return (context.getExternalContext().encodeResourceURL(u));

       

          }

       

          public void setActionExpression(MethodExpression actionExpression) {

              this.actionExpression = actionExpression;

       

          }

       

          public MethodExpression getActionExpression() {

              return actionExpression;

          }

       

      }

       

       

       

       

       

      package com.application.core.jsf.components.taglib.util;

       

      import javax.el.ExpressionFactory;
      import javax.el.ValueExpression;
      import javax.faces.application.Application;
      import javax.faces.component.UIComponent;
      import javax.faces.component.UIInput;
      import javax.faces.context.FacesContext;
      import javax.faces.el.ValueBinding;
      import javax.faces.el.VariableResolver;

       

      /**
      *
      * @author baburaj
      *
      */
      public class TagLibUtil {
         
          /**
           * This is to identify whether the value is binded using EL ( #{bean.value}),
           *
           * @param value
           * @return
           */
          public static boolean isValueReference(String value) {
              if (value == null)
                  throw new NullPointerException();
              int start = value.indexOf("#{");
              return start != -1 && start < value.indexOf('}', start);
          }
         
          /**
           * A general method to set the Value Expression for a component
           * @param context
           * @param attribute
           * @param value
           * @param component
           * @param dataType
           */
          public static void setValueExpression(FacesContext context, String attribute, String value, UIComponent component,
                  Class<?> dataType) {
              Application application = context.getApplication();
              ExpressionFactory factory = application.getExpressionFactory();
              ValueExpression valueExpression = factory.createValueExpression(context.getELContext(), value, dataType);
              component.setValueExpression(attribute, valueExpression);

       

        }
         
      }

        • 1. JSF 1.2 Custom Component Strange Problem
          Babu Raj Newbie

          i digged out the reason,

           

          That is because any user defined attributes, other than jsf core attributes, shall to be taken cared by the user itself. So the setter and the getter should be called by the user inside the component class

           

          Eg:-

           

               Map<String, String> map = context.getExternalContext().getRequestParameterMap();

                 

                  searchText = map.get(getFieldKey(context, SEARCH_TEXT_KEY));

                  if (searchText == null) {

                      searchText = "";

                  }

                  ValueExpression vl = bindings.get("searchText"); // binding is the map object from the  UIComponent.java. All value expressions packaged from the                                                                                              //Tag class will get in this class through this bindings object

                  vl.setValue(context.getELContext(), searchText);//Calling the setter method of the backing bean