micronaut-views icon indicating copy to clipboard operation
micronaut-views copied to clipboard

Thymeleaf predefined networking objects are not working

Open JeffreyDevloo opened this issue 3 years ago • 3 comments

Predefined networking objects are not working

Coming from https://stackoverflow.com/questions/68305195/micronaut-thymeleaf-request-is-null

Templates that contain #request, #session, ... always give of null for these objects. I've narrowed it down to the EngineContextFactory building a io.micronaut.views.thymeleaf.WebEngineContext instead of the expected org.thymeleaf.context.WebEngineContext.

The reason why the objects cannot be resolved is because the created context does not implement the correct interface. The resolver is unable to cast and retrieve the value.

Steps to Reproduce

  1. Serve a view using
    @View("home")
    @Get("/")
    public HttpResponse<Object> showHome() {
        return HttpResponse.ok();
    }

with the view being

    <!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Home</title>
</head>
<body>
<h3>#request.contextPath</h3>
<span th:utext="${#request.contextPath}"></span>

<h3>#request.requestURI</h3>
<span th:utext="${#request.requestURI}"></span>

<h3>#request.requestURL</h3>
<span th:utext="${#request.requestURL}"></span>
</body>
</html>
  1. Browse to localhost:8080
  2. View is not rendered and stacktrace is put to the console indicating that requestURL cannot be read from value null.

Expected Behaviour

The template should be able to resolve the #request as described by the docs similar to the spring behaviour.

Actual Behaviour

The fetched value is always null and the view is not rendered

Environment Information

  • Operating System: Windows 10
  • Micronaut version: Micronaut 2.57
  • Thymeleaf version: Thymeleaf 3.0.12
  • Micronaut views version: Micronaut views 2.2.1
  • JDK Version: Zulu JDK 11

Notes

I've tried creating my own context which implements the right interface to map those objects but Micronaut does not use the java httpservlet objects.

Workaround

I'm currently using the #ctx.getRequest() to get the NettyHttpRequest instance.

JeffreyDevloo avatar Jul 09 '21 09:07 JeffreyDevloo

Good analysis. Want to send a PR?

graemerocher avatar Jul 09 '21 10:07 graemerocher

I could make a pull request to solve this issue but it would depend on https://micronaut-projects.github.io/micronaut-servlet/1.0.2/guide/index.html as Micronaut does not expose the servlet api by default. An alternative would be to implement a IExpressionObjectFactory which adds some defaults for Micronaut and document them.

@graemerocher what would be the best option?

JeffreyDevloo avatar Jul 09 '21 10:07 JeffreyDevloo

depending on the servlet API is not an option so I guess the IExpressionObjectFactory approach is the way to go

graemerocher avatar Jul 09 '21 10:07 graemerocher

Woo this is still not fixed? I a can't seem to be able to use

th:href="${#httpServletRequest.scheme}+'://'+${#httpServletRequest.serverName}+':'+${#httpServletRequest.serverPort}"

arvera avatar Sep 27 '24 18:09 arvera