How to Set a HashMap from jsp to Action

2020-07-29 17:46发布

I have a HashMap in my Action class:

private Map<String, String> ids = new HashMap<String, String>();

In jsp I'm trying to set this hashmap like this:

<input type="text" name="ids[0].key" value="key">
<input type="text" name="ids[0]" value="value">

But when after submit, when I iterate over the map in the action like this:

if(ids!=null){
    for(Map.Entry<String, String> entry : ids.entrySet()){
        system.out.println(entry.getKey()+"-"+entry.getValue());
    }
}

I only get "0-value" instead of "key-value"

How Can I do what I want? Can someone help me with this?

2条回答
等我变得足够好
2楼-- · 2020-07-29 17:59

Trying to set values in a HashMap in a JSP file is a very bad idea. To stick with best practice and lead a happy life, you should revisit your design. You can post the data to the server side (All input values from JSP) and then get the values from request to store in a HashMap collection as per your requirement would be a better option.

查看更多
等我变得足够好
3楼-- · 2020-07-29 18:06

You have keys of type String and therefore should be mapped like strings. For example

<input type="text" name="ids['0']" value="%{value}">

Then you will get key '0' and value that was provided by OGNL.

About indexed property names and createIfNull setting you can find in the docs Advanced Type Conversion.

查看更多
登录 后发表回答