question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

can't not cast to java.util.map

See original GitHub issue

the heml is like this:

<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring4-4.dtd">
<html>
<head>
<title>tmpl1</title>

</head>
<body>
    <div th:each="item : ${rows}">
        <h1 th:text="${item.title}"></h1>
    </div>
</body>
</html>

and the controller is like this:

@RequestMapping(value="tmpl1")
    public ModelAndView testTmpl1(){

        List<Integer> ids = new ArrayList<Integer>();
        ids.add(100052);
        ids.add(100052);
        ids.add(100052);
        ids.add(100052);
        ids.add(100052);
        List<Item> itemsByIds = itemService.getItemsByIds(ids);
        ModelAndView modelAndView = new ModelAndView("test/tmpl1");
        modelAndView.addObject("rows",itemsByIds);
        return modelAndView;
    }

    @RequestMapping(value="tmpl2")
    public ModelAndView testTmpl2(){
        List<Integer> ids = new ArrayList<Integer>();
        ids.add(100052);
        ids.add(100052);
        ids.add(100052);
        ids.add(100052);
        ids.add(100052);
        List<Item> itemsByIds = itemService.getItemsByIds(ids);
        ModelAndView modelAndView = new ModelAndView("test/tmpl1");
        modelAndView.addObject("rows",JSON.toJSON(itemsByIds));
        return modelAndView;
    }

!!!when i visited tmpl2 , and then i go to visit tmpl1, it will throw a exception , can’t not cast to java.util.map ,

the JSON library is fastjson

<dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>fastjson</artifactId>
                <!-- <version>1.1.35</version> -->
                <version>1.1.46</version>
            </dependency>

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:12 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
jmiguelsampercommented, Mar 27, 2015

Ok, testTmpl1() should work with th:text=“${item.title}” because the Item class has a getTitle() method.

Try to use th:text=“${item[‘title’]}” (SPel notation for Maps) for testTmpl2() as you don’t have a getTitle() method.

0reactions
dtrunk90commented, Mar 7, 2017

Thanks. That solved the problem.

Read more comments on GitHub >

github_iconTop Results From Across the Web

java.lang.String cannot be cast to java.util.Map - Stack Overflow
String cannot be cast to java.util.Map" . The error only appears after the code is run, not while compiling, nor in NetBeans IDE....
Read more >
How to fix java.lang.classcastexception cannot be cast to in Java
As name suggests ClassCastException in Java comes when we try to type cast an object and object is not of the type we...
Read more >
Jackson: java.util.LinkedHashMap cannot be cast to X
Sometimes, we may encounter java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to X when we try to deserialize JSON or XML ...
Read more >
Error "java.lang.String cannot be cast to java.util.Map" When ...
Error "java.lang.String cannot be cast to java.util.Map" When Uploading a Server Side Extension (SSE) (Doc ID 2418722.1).
Read more >
[Fixed] java.util.HashMap$Values cannot be cast to class java ...
HashMap values returns java.util.Collection and you can not cast Collection to List or ArrayList. It will throw ClassCastException in this scenario.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found