Spring形式的Hashmap

时间:2013-10-23 11:19:14

标签: java jsp spring-mvc hashmap spring-el

我使用以下输入标记,其中entry是myMap的条目。

<form:input path="myMap[${entry.key}].parameters[${counter}].value" />

和以下对象

Map<Long, MyViewModel> myMap;

public class MyViewModel {
private List<My2ViewModel> parameters;
...

如果entry.key的值是一个整数,那么一切都可以正常工作。但如果它很长(例如5000000000)就崩溃了

org.springframework.expression.spel.SpelParseException: EL1035E:(pos 14): The value '5000000000' cannot be parsed as an int
at org.springframework.expression.spel.ast.Literal.getIntLiteral(Literal.java:79)
at org.springframework.expression.spel.standard.InternalSpelExpressionParser.maybeEatLiteral(InternalSpelExpressionParser.java:688)
at org.springframework.expression.spel.standard.InternalSpelExpressionParser.eatStartNode(InternalSpelExpressionParser.java:429)
at org.springframework.expression.spel.standard.InternalSpelExpressionParser.eatPrimaryExpression(InternalSpelExpressionParser.java:282)
at org.springframework.expression.spel.standard.InternalSpelExpressionParser.eatUnaryExpression(InternalSpelExpressionParser.java:275)
...
Caused by: java.lang.NumberFormatException: For input string: "5000000000"

当我添加L(Stephen的建议)时,我得到了另一个错误。

<form:input path="myMap[${entry.key}L].parameters[${counter}].value" />

java.lang.IllegalArgumentException: The field type is required
at org.springframework.util.Assert.notNull(Assert.java:112)
at org.springframework.webflow.mvc.view.ConvertingPropertyEditorAdapter.<init>(ConvertingPropertyEditorAdapter.java:48)
at org.springframework.webflow.mvc.view.BindingModel.findSpringConvertingPropertyEditor(BindingModel.java:277)
at org.springframework.webflow.mvc.view.BindingModel.findEditor(BindingModel.java:183)
at org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:125)
at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getBindStatus(AbstractDataBoundFormElementTag.java:179)
at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getPropertyPath(AbstractDataBoundFormElementTag.java:199)
at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getName(AbstractDataBoundFormElementTag.java:165)
at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.autogenerateId(AbstractDataBoundFormElementTag.java:152)
at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.resolveId(AbstractDataBoundFormElementTag.java:143)
at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.writeDefaultAttributes(AbstractDataBoundFormElementTag.java:127)
at org.springframework.web.servlet.tags.form.AbstractHtmlElementTag.writeDefaultAttributes(AbstractHtmlElementTag.java:421)
at org.springframework.web.servlet.tags.form.InputTag.writeTagContent(InputTag.java:142)
...

1 个答案:

答案 0 :(得分:2)

数字5,000,000,000大于Java int的最大可能值。现在看起来你真的想要一个long值,但遗憾的是Spring文档说明了这一点:

  

“支持的文字表达式的类型是字符串,日期,数值(int,real和hex),boolean和null。”

没有提及long

参考:Spring Expression Language - 特别是第6.5.1节。


使用long文字的5000000000L的Java语法可能会“幸运” - 但我怀疑它会起作用。