casid / jte

Secure and speedy templates for Java and Kotlin.
https://jte.gg
Apache License 2.0
748 stars 56 forks source link

Spring Boot starter integration JteModelAndView and @model proposal #274

Closed stianl closed 11 months ago

stianl commented 12 months ago

I just looked at the README.md for jte-spring-boot-starter-3 and - taking a little inspiration from .NET Razor templates - I wondered if it'd be possible to simplify the usage even further?

@import com.example.demo.DemoModel

@param DemoModel model

Hello ${model.text}!

I know that @import is not needed if specifying fully qualified class name, so I can write just @param com.example.demo.DemoModel model. But what if the model name is always model? Could we just do @model com.example.demo.DemoModel ?

@GetMapping("/") 
public String view(Model model, HttpServletResponse response) {
    model.addAttribute("model", new DemoModel("Hello World"));
    return "demo";
}

What if we could leave out the name of the model here too - just always let it be "model"? I can return ModelAndView from a Spring controller, but there's no constructor taking a view name and model. I guess there could be one (that'd just set the model name to "model")? Like JteModelAndView? 🤔 Something like this?

@GetMapping("/") 
public ModelAndView view(Model model, HttpServletResponse response) {
    return new JteModelAndView("demo", new DemoModel("Hello World"));
}

Or, maybe something like @JteController (or @Controller + @Jte ?) that would render the view with the same name as the method - and use the return value as "model"?

atomfrede commented 12 months ago

The thing is, you can have multiple parameters inside the modelmap. So yes, if you have only one "model parameter" it would make it a little simpler, but you would end up having two different approaches when having multiple model parameters.

casid commented 11 months ago

Since there's been relatively low support for this, and I personally think we should keep the amount of magic as low as possible, I'm closing this now as not planned.