Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to populate a map in JSP?

Tags:

java

jsp

jstl

For example

<jsp:useBean id="total" class="java.util.LinkedHashMap"/>
// need somehow do something like this: total.put('key', 'value');

But without using scriptlets (it's obvious but a little bit ugly)

like image 835
Eugene Retunsky Avatar asked Feb 18 '13 18:02

Eugene Retunsky


People also ask

How do you insert elements in a map Java?

Inserting Elements Into a Java Map To add elements to a Map you call its put() method. Here are a few examples: Map<String, String> map = new HashMap<>(); map. put("key1", "element 1"); map.

Can we create object of map in Java?

The classic Java way is to pass the value Map as an argument to the constructor of Person and let person read the properties from the map. This way you can have multiple ways for constructing a Person object. Either by passing arguments directly, or passing the map.

What is <%@ in JSP?

The page directive is used to provide instructions to the container that pertain to the current JSP page. You may code the page directives anywhere in your JSP page. By convention, page directives are coded at the top of the JSP page. Following is the basic syntax of page directive − <%@ page attribute = "value" %>


1 Answers

You can use JSTL <c:set> for this.

<jsp:useBean id="total" class="java.util.LinkedHashMap"/>
<c:set target="${total}" property="key" value="value" />
like image 122
BalusC Avatar answered Sep 28 '22 00:09

BalusC