gpt4 book ai didi

java - 如何映射 Springs 中的字节 :select?

转载 作者:行者123 更新时间:2023-12-01 04:45:45 25 4
gpt4 key购买 nike

在我的模型中,我有变量低字节=0;高字节 = 1;

现在 low 和 high 可以映射到 String O1 、 O2 、 O3 中的 3 个值;

例如,如果 low = 0,则可以映射到 O1 ,如果为 1,则可以映射到 O2。对于高也同样如此。

我应该如何设计我的 Controller 来通过 JSP 页面操作这些值。

我有 O1,O2,O3 的枚举

喜欢

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

我想要使用 form:options 的下拉菜单,它将显示这三个低和高的枚举选项。

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

最佳答案

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

你的枚举(我的假设):

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。

希望这有帮助。

关于java - 如何映射 Springs 中的字节 :select?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15880880/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com