0

I have the following controller:

public class MyErrorController implements ErrorController {

  @RequestMapping("/error")
  public String handleError(HttpServletRequest request, Model model) {
     model.addAttribute("request", request);

   }
}

And in my template, I can do:

Method: <span th:text="${request.method}">method</span><br/>

And it will give me html of:

Method: POST

Is there a simple way to know all of the properties that the request method has in Thymeleaf? For example, doing something like %{request.toDict} (made up for demonstration purposes). HttpServletRequest methods are listed here, but I'm not sure which can be used as properties and furthermore, how it can be (hopefully) easily shown from the template.

David542
  • 104,438
  • 178
  • 489
  • 842
  • Probably not the best example because you don't need the controller method in this case. You can simply do `#request.whatever` in the HTML: https://stackoverflow.com/questions/41395024/access-httpservletrequest-and-httpservletresponse-in-a-thymeleaf-dialect-process – riddle_me_this Jul 26 '18 at 22:06
  • Otherwise, if you use IntelliJ IDEA, you can do `` and include it in your HTML for completion. – riddle_me_this Jul 26 '18 at 22:08
  • @bphilipnyc sure, but is there a way to just print them all so I can see their values at once? – David542 Jul 26 '18 at 22:08
  • Do you want to print them to a page? Or just for use in your IDE (e.g., tab completion)? – riddle_me_this Jul 26 '18 at 22:10
  • @bphilipnyc I would like to print all values to a page, because a lot of the values are empty. So yes, in html, and not in telliJ. – David542 Jul 26 '18 at 22:12
  • As a note, the whole idea behind an MVC architecture is that you shouldn't smuggle hidden parameters between components like this but rather should list them explicitly in the model. – chrylis -cautiouslyoptimistic- Jul 26 '18 at 22:57
  • Would calling a static method that iterates through `class.getDeclaredMethods()` and its parent objects be sufficient? There isn't a specific utility method with Thymeleaf 3 from what I can tell. Seems a little ugly no matter how you slice it. – riddle_me_this Jul 26 '18 at 23:22
  • https://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html – Johannes Kuhn Jul 27 '18 at 01:36
  • @bphilipnyc yea that would work -- sounds great. If you can add that as an answer on how to do it I can go ahead and accept it. – David542 Jul 27 '18 at 03:38

1 Answers1

2

With the current version of Thymeleaf (3.0.9), there is no specific utility method to print out the properties of an object. So no, there is not a simple way.

However, one way to do this would be to construct your own method and print them using reflection. My running assumption, of course, is that methods available to Java should be available to Thymeleaf as well. You can modify this method if this is not exactly what you're seeking (inspiration).

public static List<Method> toDict(Class aClass) {
    List<Method> methods = new ArrayList<>();
    do {
        Collections.addAll(methods, aClass.getDeclaredMethods()); //using just this would return the declared methods for the class and not any parent classes
        aClass = aClass.getSuperclass();
    } while (aClass != null);
    return methods;
}

Then call the static method in your HTML:

<div th:each="field : ${T(com.somedomain.util.SomeUtil).toDict(#request.class)}">
    <span th:text="${field}" th:remove="tag">[field]</span><br>
</div>

Also be aware of the usual caveats to unit testing and static methods. Lastly, you can look at #vars in case you're looking to access model variables.

No controller method needed.

riddle_me_this
  • 8,575
  • 10
  • 55
  • 80