jsp中的hashmap输入

发布于 2024-10-17 01:53:18 字数 416 浏览 1 评论 0原文

有人可以告诉我如何在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谎言 2024-10-24 01:53:18

使用 beans 和 jsp 标签的组合是可行的,但要注意 - 你做错了,你想要的也是错误的(而且可能不是你需要的)。我的解决方案效果很好,但我认为您应该扔掉它并更改您的应用程序(除非不可能或时间压力很大)。

  1. 如果您使用纯 jsp(而不是 JSF),那么 HTML 只是一个模板; JSP 不知道“' 之间的区别JSP 中的“煎饼”无法执行任何实际操作(例如执行操作或将值推送到 beans)。您必须创建一个普通的 html 表单,捕获其参数(使用隐式变量“param”)并执行操作。 p>

  2. 这样的“抓即演”并不真正属于JSP。应该在其他地方完成。

  3. 在纯 JSP 中,可以使用 jsp:setProperty 在 java bean 之间以及 java bean 和不同请求属性(如参数或 cookies)之间移动数据。有一个问题:要写入的属性不能是动态的。

  4. 因此,为了实现您的目标,您必须编写一些 Java,它需要一些静态属性(数据和目标映射)并使用它;更重要的是,使用必须发生在 setter 内部。它不可能真正通用,因为在运行时我们不知道映射中键和值的类型(由于擦除)。

  5. 小心搬运:


// MapAppender

package a.b.c;

import java.util.Map;

public class MapAppender {
    private String key;
    private Float value;

    public void setKey(String key) {
        this.key = key;
    }

    public void setValue(Float value) {
        this.value = value;
    }

    public void setMap(Map map) {
        if (key != null && value != null) map.put(key, value);
    }

}

// How to use it on page:

<jsp:useBean id="object"  scope="session" class="a.b.c.ObjectWithMap" />

<jsp:useBean id="appender"  scope="request" class="a.b.c.MapAppender" />
<jsp:setProperty name='appender' property='key' value='${param.string}' />
<jsp:setProperty name='appender' property='value' value='${param.float}' />
<jsp:setProperty name='appender' property='map' value='${object.map}' />

<form>
    <input name='string' value='test' />
    <input name='float' value='3.25' />
    <input type="submit" />
</form>

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).

  1. 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.

  2. Such "catching and acting" does not really belong in JSP. It should be done somewhere else.

  3. 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.

  4. 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).

  5. Handle with care:


// MapAppender

package a.b.c;

import java.util.Map;

public class MapAppender {
    private String key;
    private Float value;

    public void setKey(String key) {
        this.key = key;
    }

    public void setValue(Float value) {
        this.value = value;
    }

    public void setMap(Map map) {
        if (key != null && value != null) map.put(key, value);
    }

}

// How to use it on page:

<jsp:useBean id="object"  scope="session" class="a.b.c.ObjectWithMap" />

<jsp:useBean id="appender"  scope="request" class="a.b.c.MapAppender" />
<jsp:setProperty name='appender' property='key' value='${param.string}' />
<jsp:setProperty name='appender' property='value' value='${param.float}' />
<jsp:setProperty name='appender' property='map' value='${object.map}' />

<form>
    <input name='string' value='test' />
    <input name='float' value='3.25' />
    <input type="submit" />
</form>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文