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

Spring MVC model not forwarded #378

Closed
blaluc opened this issue Sep 22, 2024 · 9 comments
Closed

Spring MVC model not forwarded #378

blaluc opened this issue Sep 22, 2024 · 9 comments

Comments

@blaluc
Copy link
Contributor

blaluc commented Sep 22, 2024

It seems that while forwarding requests, the model instance is not injected from the request scope.

e.g.:

@Controller
@RequestMapping("/second")
public class OtherController {

	@GetMapping
	public String view2(Model model) {
		model.addAttribute("key2", "second value");
		return "forward:/";
	}
@Controller
@RequestMapping("/")
public class TestController {

	@GetMapping
	public String index(Model model) {
		model.addAttribute("key1", "first value");
		return "index";
	}
}

index.jte:

@param String key1
@param String key2

key1: ${key1} <br>
key2: ${key2}

By calling http://localhost:8080/second, the key2 value should be propagated to the template and displayed, but it is not, and the following page is shown instead:

key1: first value
key2:

The same example with thymeleaf works as expected giving the result:

key1: first value
key2: second value
@blaluc
Copy link
Contributor Author

blaluc commented Sep 22, 2024

it seems that can be solved by adding

this.setExposeRequestAttributes(true);

in the JteViewResolver class, i.e.:

public class JteViewResolver extends AbstractTemplateViewResolver {
...
    public JteViewResolver(TemplateEngine templateEngine, final String templateSuffix) {
        this.templateEngine = templateEngine;
        this.setSuffix(templateSuffix);
        this.setViewClass(JteView.class);
        this.setContentType(MediaType.TEXT_HTML_VALUE);
        this.setOrder(Ordered.HIGHEST_PRECEDENCE);
        this.setExposeRequestAttributes(true);
    }
...

@casid
Copy link
Owner

casid commented Sep 27, 2024

@blaluc thanks for sharing. Your suggested fix looks good to me.

However I'm not a heavy spring user and don't know if this has any unintended side effects.

@atomfrede, @tschuehly would this change be okay with you?

@atomfrede
Copy link
Contributor

At first glance it looks okay. Found similar issues with pebble for example. Not sure why it works for thymeleaf out of the box because it looks like the exposeRequestAttributes is not set (at least not explicitly).

@atomfrede
Copy link
Contributor

I have found why it works with thymeleaf. In case it is forwarded, thymeleaf creates an InternalViewResourceView and in there it exposes the model attributes as request attributes (always). So setting it looks fine to me (maybe check if that has any implications on normal view resolving)

@atomfrede
Copy link
Contributor

@blaluc Just curious, whats the use case for the forward (instead e.g. redirect with flash attributes for example), besides being the forward totally server side?

@blaluc
Copy link
Contributor Author

blaluc commented Sep 27, 2024

Hi @atomfrede, the use case I’m working on involves integrating with the htmx library (https://htmx.org/). To handle a full-page reload, it might be useful to forward the model built in a 'partial' controller method (server-side) to another controller method responsible for rendering the full page.

@casid
Copy link
Owner

casid commented Sep 28, 2024

@blaluc did you check if it has any impact on normal view resolving? And, would you be up to submit a Pull Request with your suggested fix?

blaluc pushed a commit to blaluc/jte that referenced this issue Sep 28, 2024
@blaluc
Copy link
Contributor Author

blaluc commented Sep 28, 2024

Done (#383).

To avoid any impact I have added a configuration parameter (the default behavior is the previous one).

Cheers!

casid added a commit that referenced this issue Oct 8, 2024
* Spring MVC model not forwarded #378

* Update jte-spring-boot-starter-3/src/main/resources/META-INF/spring-configuration-metadata.json

Co-authored-by: Frederik Hahne <[email protected]>

---------

Co-authored-by: blasetti <[email protected]>
Co-authored-by: Andreas Hager <[email protected]>
Co-authored-by: Frederik Hahne <[email protected]>
@casid
Copy link
Owner

casid commented Oct 9, 2024

This is now released with jte 3.1.13

@casid casid closed this as completed Oct 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants