El

2 respostas
viniciuspadua

Tenho um enum

public enum Tipo {
	CONSULTA(1), PROCESSO(2), MANUT(3);

	private int value;

	TipoLote(int value) {
		this.value = value;
	}

	public int toInt() {
		return value;
	}

	public static Tipo fromInt(int value) {
		switch (value) {
		case 1:
			return CONSULTA;
		case 2:
			return PROCESSO;
		case 3:
			return MANUT;
		default:
			return null;
		}
	}

	@Override
	public String toString() {
		switch (this) {
		case CONSULTA:
			return "Consulta hsp";
		case SPADT:
			return "Processo X";
		case INTERNACAO:
			return "Manutenção H";
		default:
			return "";
		}
	}

	public String getToString() {
		return toString();
	}

	public int getToInt() {
		return toInt();
	}

	public void setToInt(int value) {
		this.value = value;
	}
}

estou querendo utilizando a EL mostrar o valor do enum.
quando uso: ${tipo} é exibido CONSULTA, PROCESSO, MANUT ao invés do método toString.
como faço para chamar o método toString?

2 Respostas

E

Estranho. Acho que você tem é um arquivo que não compilou e está usando uma versão antiga dele. Peguei seu programa e dei uma ajeitada para ele compilar (obviamente ele não está “correto” de acordo com seu sistema, mas está compilável).

public enum Tipo {  
    CONSULTA(1), PROCESSO(2), MANUT(3), SPADT(4), INTERNACAO(5);  
  
    private int value;  
  
    Tipo(int value) {  
        this.value = value;  
    }  
  
    public int toInt() {  
        return value;  
    }  
  
    public static Tipo fromInt(int value) {  
        switch (value) {  
        case 1:  
            return CONSULTA;  
        case 2:  
            return PROCESSO;  
        case 3:  
            return MANUT;  
        default:  
            return null;  
        }  
    }  
  
    @Override  
    public String toString() {  
        switch (this) {  
        case CONSULTA:  
            return "Consulta hsp";  
        case SPADT:  
            return "Processo X";  
        case INTERNACAO:  
            return "Manutenção H";  
        default:  
            return "";  
        }  
    }  
  
    public String getToString() {  
        return toString();  
    }  
  
    public int getToInt() {  
        return toInt();  
    }  
  
    public void setToInt(int value) {  
        this.value = value;  
    }  

    public static void main(String[] args) {
        for (Tipo t : Tipo.values()) {
            System.out.println (t);
        }
    }
}

Ele imprimiu

Consulta hsp


Processo X
Manutenção H
viniciuspadua

Descobri uma coisa:

Quando faço em um main

Tipo tipo = Tipo.SPADT;
System.out.println(tipo);

funciona difeito!

Entretanto quando chamo em uma jsp

${tipo}
<s:property value="tipo"/>

não funciona com a EL mais funciona com a tag do struts!
agora a duvida fica em pq com EL não funciona!

Criado 2 de setembro de 2009
Ultima resposta 2 de set. de 2009
Respostas 2
Participantes 2