Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Thymeleaf predefined networking objects are not working #241

Open
JeffreyDevloo opened this issue Jul 9, 2021 · 4 comments
Open

Thymeleaf predefined networking objects are not working #241

JeffreyDevloo opened this issue Jul 9, 2021 · 4 comments

Comments

@JeffreyDevloo
Copy link

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.

@graemerocher
Copy link
Contributor

Good analysis. Want to send a PR?

@JeffreyDevloo
Copy link
Author

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?

@graemerocher
Copy link
Contributor

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

@arvera
Copy link

arvera commented Sep 27, 2024

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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants