java 如何在没有迭代的情况下使用来自hashmap的jstl获取jsp中的值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10701564/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 02:10:08  来源:igfitidea点击:

how to get values in jsp using jstl from hashmap without iteration?

javamodel-view-controllerjspspring-mvcjstl

提问by anto

i am a newbie i have a hash map declared as

我是新手我有一个哈希映射声明为

 HashMap map = new HashMap();

    map.put("code", code);
    map.put("url", evaluationTSRCode);

and iam passing this map in my modelandview contoller like this

我正在像这样在我的模型和视图控制器中传递这张地图

return new ModelAndView("purchaseProduct","map","map"); 

how to access the values code,evaluationTSRCode in jsp using jstl without using iteration i tries like this but i could not get the values.

如何在不使用迭代的情况下使用jstl 访问jsp 中的值代码、evaluationTSRCode 我尝试这样,但我无法获取值。

<c:choose>
<c:when test="(${map.code} != null && ${map.code}=!"" )" >
<td>You can purchase<a href='<c:url value="${map.url}"/>'>Directly</a></td>
</c:when>
<c:otherwise>
td>You can purchase Directly</td>
</c:otherwise>
</c:choose>

please help me in solving this issue

请帮我解决这个问题

Thanks in advance

提前致谢

回答by Japan Trivedi

The way you are accessing the map values in JSP is totally wrong.

您在 JSP 中访问地图值的方式是完全错误的。

There is one assumption from my side in your code

你的代码中有一个来自我的假设

return new ModelAndView("purchaseProduct","map","map");

is wrong it should be

应该是错的

return new ModelAndView("purchaseProduct","map",map);

You don't need to iterate through the map values.

您不需要遍历地图值。

Eg. To access the value of code. ${map.code}in jsp anywhere would be sufficient.

例如。访问代码的值。${map.code}在jsp中任何地方就足够了。

Or you can use like

或者你可以使用像

return new ModelAndView("purchaseProduct", map);

and when you do this in JSP you just need to access the map value by its name only.

当您在 JSP 中执行此操作时,您只需要通过其名称访问映射值。

Eg. To access the value of code. ${code}in jsp anywhere would be sufficient.

例如。访问代码的值。${code}在jsp中任何地方就足够了。

Hope this helps you.

希望这对你有帮助。

Cheers.

干杯。

回答by Stefano Scarpanti

I think an answer a bit more correct is still using map as suggested, but adding it to the ModelMap, declared in input of the controller method and returning only the string with the view name (I always use this way to refer views in spring controller):

我认为一个更正确的答案是仍然按照建议使用地图,但将其添加到 ModelMap,在控制器方法的输入中声明并仅返回带有视图名称的字符串(我总是使用这种方式来引用 spring 控制器中的视图):

public String themethod(ModelMap model) {
  ...
  model.addAttribute("map",map);
  return "purchaseProduct";

In this way you can use ${map.code} in your jsp.

这样你就可以在你的jsp 中使用${map.code}。

More interestingly, if needed, you could package complex data structure in a map and then in a list

更有趣的是,如果需要,您可以将复杂的数据结构打包在一个映射中,然后在一个列表中

  List<Map<String, Object>> lcli = new ArrayList<Map<String, Object>>();
  for(Cliente c:cli) {
        Map <String,Object>map  = new HashMap<String, Object>();
        int nord    = userDao.nord(c);
        map.put("cli", c);
        map.put("nord", nord);

        lcli.add(map);
    }
    mo.addAttribute("lcli", lcli);

In this way in the jsp you could iterate each element of the list, tagged lcli, that it is a map, from which pick up its keys/values as said, as below

通过这种方式,在 jsp 中,您可以迭代标记为 lcli 的列表中的每个元素,它是一个映射,从中获取其键/值,如下所示

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
...
<c:forEach var="itemcli" items="${lcli}">
        <a href="<c:url value='edit-client.html?id=${ itemcli.cli.id }'/>" >   ${itemcli.cli.nome}  </a>            
        ${itemcli.cli.citta}
</c:forEach>