jsp中的hashmap输入
有人可以告诉我如何在jsp中设置hashmap值吗?
对象是
汽车:
private Map<String, Float> myHashMap = new HashMap<String, Float>();
在jsp中我想做这样的事情
<input type='hidden' name='myobject.myHashmap["setvalue string"]' value='my value for string' />
<input type='hidden' name='myobject.myHashmap["setvalue float"]' value='my value for float' />
can someone tell me how to set a hashmap value in jsp?
The object is
car with:
private Map<String, Float> myHashMap = new HashMap<String, Float>();
in the jsp i want do soemthing like this
<input type='hidden' name='myobject.myHashmap["setvalue string"]' value='my value for string' />
<input type='hidden' name='myobject.myHashmap["setvalue float"]' value='my value for float' />
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 beans 和 jsp 标签的组合是可行的,但要注意 - 你做错了,你想要的也是错误的(而且可能不是你需要的)。我的解决方案效果很好,但我认为您应该扔掉它并更改您的应用程序(除非不可能或时间压力很大)。
如果您使用纯 jsp(而不是 JSF),那么 HTML 只是一个模板; JSP 不知道“' 之间的区别JSP 中的“煎饼”无法执行任何实际操作(例如执行操作或将值推送到 beans)。您必须创建一个普通的 html 表单,捕获其参数(使用隐式变量“param”)并执行操作。 p>
这样的“抓即演”并不真正属于JSP。应该在其他地方完成。
在纯 JSP 中,可以使用 jsp:setProperty 在 java bean 之间以及 java bean 和不同请求属性(如参数或 cookies)之间移动数据。有一个问题:要写入的属性不能是动态的。
因此,为了实现您的目标,您必须编写一些 Java,它需要一些静态属性(数据和目标映射)并使用它;更重要的是,使用必须发生在 setter 内部。它不可能真正通用,因为在运行时我们不知道映射中键和值的类型(由于擦除)。
小心搬运:
// MapAppender
It is doable, using a combination of beans and jsp tags, but beware - you are doing it wrong and what you want is wrong (and probably not what you need). My solution works well, but I think you should throw it away and change your app anyway (unless it's impossible or there is a great pressure of time).
If you are using plain jsp (as opposed to JSF) then HTML is just a template; JSP does not know the difference between "<input name='x'>' and 'pancakes'. Forms in JSP can't do anything real (like execute actions or push values to beans). You have to create a vanilla html form, catch its params (using implicit variable 'param') and act.
Such "catching and acting" does not really belong in JSP. It should be done somewhere else.
In pure JSP there is a possibility of moving data between java beans and between java beans and different request properties (like parameters or cookies), using jsp:setProperty. There is a catch: properties to be written can't be dynamic.
Therefore, to achieve your goal, you MUST write a bit of Java, that takes some static properties (data and the target map) and uses it; what's more, the usage must happen inside a setter. It cannot be really universal, because at runtime we do not know the types of keys and values in the map (due to erasure).
Handle with care:
// MapAppender