2013-04-08 61 views
1

在我的模型我有变量 字节低= 0; 字节高= 1;如何映射弹簧形式的字节:select?

现在低和高可以映射到字符串O1,O2,O3中的3个值;

例如,如果low = 0,它可以映射到O1,如果是1,它将映射到O2。 这对高也是一样。

我应该如何设计我的控制器来通过JSP页面操纵这些值。

我有一个像

enum MyEnum { 
O1(0),O2(1),O3(2) so on... 
} 

为O1,O2,O3

枚举我用形式要下拉:选项,它会显示这三个枚举选项低以及高。

这里唯一的问题是我已阅读How do I set the selected value in a Spring MVC form:select from the controller?,但我无法弄清楚我的字节值如何创建地图。我想填充这些值。

回答

0

首先,我认为你应该在你的模型中使用枚举而不是字节。您始终可以从枚举中获取字节值。还要向模型类添加方法以返回枚举的字节值或字符串值。然后使用此字符串值作为您的选择输入框。

你枚举(我的假设):

public enum MyEnum { 
    O1 (0), 
    O2 (1), 
    O3 (2); 

    private final Byte byteVal;  

    private MyEnum(Byte val) { 
     byteVal = val; 
    } 

    public Byte getByteVal(){ 
     return byteVal; 
    } 

} 

你的模型(我的假设):

public class MyModel{ 
    MyEnum high; //instead of Byte high 
    MyEnum low;//instead of Bye low 
    .... 
    //This method would return byte to be compatible with your backend as it is right now 
    public Byte getHigh(){ 
     return this.high.getByteVal(); 
    } 
    //This method would allow you to use the string representation for your front end 
    public Byte getHighString(){ 
     return this.high.name(); 
    } 
} 

现在在你的JSP使用model.highString而不是model.high您的选择框。

希望这会有所帮助。

+0

这里的高和低是字节,根据它们的值,它们将指向O1或O2等。 – Chetan 2013-04-08 14:14:37

+0

如果low = 0那么它应该指向O1 – Chetan 2013-04-08 14:14:55

+0

高和低只是字节。你碰巧把它们和一个枚举关联起来。但更好地利用枚举将直接使用枚举值作为高和低。 – MickJ 2013-04-08 14:20:09