⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 artifacttypeconverter.java

📁 JSF例子 JSF标签 用户角色控制 比较全的例子
💻 JAVA
字号:
package org.jia.ptrack.web;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;

import org.jia.ptrack.domain.ArtifactType;

/**
 *
 *  NOTE: This class differs slightly from the book. The core conversion logic
 *        was factored out into separate methods
 *        for easy testing.
 */

public class ArtifactTypeConverter
    implements Converter
{

  public final static String CONVERTER_ID = "jia.ArtifactType";

  public ArtifactTypeConverter()
  {
  }

  public Object getAsObject(FacesContext context, UIComponent component,
                            String value)
  {
    Utils.log(context, "Converting from " + value + " to Object");

    try
    {
      return getStringAsArtifactType(value);
    }
    catch (NumberFormatException ne)
    {
      Utils.log(context, "Can't convert to an ArtifactType; value (" +
                value + ") is not an integer.");
      throw new ConverterException("Can't convert to an ArtifactType; value (" +
                                   value + ") is not an integer.", ne);
    }
    catch (IllegalArgumentException e)
    {
      Utils.log(context, "Can't convert ArtifactType; unknown value: " + value);
      throw new ConverterException(
          "Can't convert ArtifactType; unknown value: " + value, e);
    }
  }

  protected ArtifactType getStringAsArtifactType(String value) throws
      NumberFormatException,
      IllegalArgumentException
  {
    if (value == null)
    {
      return null;
    }
    int artifactValue = Integer.parseInt(value);
    return (ArtifactType) ArtifactType.getEnumManager().getInstance(
        artifactValue);
  }

  public String getAsString(FacesContext context, UIComponent component,
                            Object value)
  {
    Utils.log(context, "Converting from " + value + " to string");

    if (value instanceof ArtifactType)
    {
      return getArtifactTypeAsString(value);
    }
    else
    {
      Utils.log(context, "Incorrect type (" + value.getClass() +
                "; value = " + value +
                "); value must be an ArtifactType instance");
      throw new ConverterException("Incorrect type (" + value.getClass() +
                                   "; value = " + value +
                                   "); value must be an ArtifactType instance");
    }
  }

  protected String getArtifactTypeAsString(Object value)
  {
    if (value == null)
    {
      return null;
    }
    ArtifactType artifact = (ArtifactType) value;
    return String.valueOf(artifact.getValue());
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -