Phate
Phate

Reputation: 6612

Can't read an enum type .name() into a jsp: uncaught reference error

This is my java class:

package org.at.network;

public class MyClass {

public static enum Type {
    ROOT(0),RELAY(1),LEAF(2),NULL(3);

    private int value;

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

    public int getValue(){
        return value;
    }
}
 ....

Now in my jsp:

<%@page import="org.at.network.MyClass"%>

var ROOT = <%=MyClass.Type.ROOT.name() %>;

How come it gives me:

Uncaught ReferenceError: ROOT is not defined

?

If I execute that code in a java class it works so it should be a jsp problem...

Upvotes: 1

Views: 310

Answers (2)

A4L
A4L

Reputation: 17595

I think it's a javascript problem...

Probalby the javascript code that is generated looks like this:

var ROOT = ROOT;

so at this point the variable ROOT is undefined.

You probably wanted to save it as string:

so try doing:

var ROOT = '<%=MyClass.Type.ROOT.name() %>';

doing so the js code generated will be

var ROOT = 'ROOT';

Upvotes: 1

Sravan Kumar
Sravan Kumar

Reputation: 253

use getValue() method instead of getName() method

Upvotes: 0

Related Questions