Java 使用 JSTL/EL 将整数值转换为字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3576131/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert integer value to string using JSTL/EL
提问by geffchang
How do I correct this statement:
我如何更正此声明:
${model.myHashtable[model.data.id]}.
myHashtable is defined as
myHashtable 定义为
Hashtable<String, String>
But, ${model.data.id}
returns an int
.
但是,${model.data.id}
返回一个int
.
I tried to do something like
我试着做类似的事情
${model.myHashtable['model.data.id']}
But it does not work. Any other ideas, aside from changing the type of id
to String
?
但它不起作用。除了更改id
to的类型之外,还有其他想法String
吗?
采纳答案by BalusC
Set it as body of <c:set>
. It will implicitly be converted to String
.
将其设置为<c:set>
. 它将隐式转换为String
.
<c:set var="idAsString">${model.data.id}</c:set>
<c:out value="${model.myHashtable[idAsString]}" />
回答by u1776024
you can use org.apache.commons.collections4.map.CaseInsensitiveMap both String "1" and int 1 are the same key;
您可以使用 org.apache.commons.collections4.map.CaseInsensitiveMap String "1" 和 int 1 是相同的键;
回答by Chaojun Zhong
${''.concat(model.data.id)} works for me,you may try it.
${''.concat(model.data.id)} 对我有用,你可以试试。
回答by Do Nhu Vy
In JSP file:
在 JSP 文件中:
<%@ taglib prefix = "fmt" uri = "http://java.sun.com/jsp/jstl/fmt" %>
<!-- lines of code... -->
<fmt:parseNumber var = "id_value" type = "number" value = "${model.data.id}" />
<c:out value = "${id_value}" />