English 中文(简体)
如何使用 Hashmap 的jstl 来获取jsp 中的值, 而不重复?
原标题:how to get values in jsp using jstl from hashmap without iteration?

我是一个新人 我有一张大麻地图 被宣布为

 HashMap map = new HashMap();

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

这张地图在我这样的模特和观视窗中通过

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.

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

请你帮助我解决这个问题

提前感谢

最佳回答

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

你的代码里有一个假设 是从我这边推断出来的

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

是错的,应该是错的

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

您不需要通过地图值循环 。

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

或者你可以像

return new ModelAndView("purchaseProduct", map);

当您在 JSP 中这样做时, 您只需要访问地图值 。

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

希望这能帮到你

欢呼声。

问题回答

我认为一个更正确的答复仍然使用地图, 但仍如建议的那样使用地图, 但将其添加到模型映射中, 在输入控制器方法时声明, 并且只返回带有视图名称的字符串( 我总是用此方式在弹簧控制器中引用视图 ) :

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

这样您就可以在 Jsp 中使用${map. code} 。

更有趣的是,如果需要,您可以将复杂的数据结构包装在地图中,然后在列表中

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

这样,在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>                          




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...