Module io.jstach.opt.spring.example
Class HelloController
java.lang.Object
io.jstach.opt.spring.example.hello.HelloController
Example hello world controller using different ways to use JStachio for web
development.
- Author:
- agentgt
-
Field Summary
Modifier and TypeFieldDescriptionfinal JStachio
Although not needed You can also wire in JStachio directlyfinal Template<HelloModel>
(Optional) Spring will inject this template as the templates are either component scanned or loaded by the ServiceLoader into Spring's context. -
Constructor Summary
ConstructorDescriptionHelloController
(JStachio jstachio, Template<HelloModel> view) Placate JDK 18 Javadoc -
Method Summary
Modifier and TypeMethodDescriptionentity()
Here we use aResponseEntity
which allows use to set status codes with our model to be rendered.hello()
Here we use JStachio runtime to resolve the renderer (in this case we are calling them Views) via Springs Http Message Converter.mvc()
Here we could useJStacheInterfaces
to make our model implementJStachioModelView
to support the traditional servlet MVC approach.Here we use the generated code directly and return aTemplateModel
which is analogous toModelAndView
.wired()
Here we use the wired renderer.
-
Field Details
-
view
(Optional) Spring will inject this template as the templates are either component scanned or loaded by the ServiceLoader into Spring's context. This is usually not needed as just returning the models is good enough. -
jstachio
Although not needed You can also wire in JStachio directly
-
-
Constructor Details
-
HelloController
Placate JDK 18 Javadoc- Parameters:
jstachio
- spring powered jstachioview
- wired in template
-
-
Method Details
-
hello
Here we use JStachio runtime to resolve the renderer (in this case we are calling them Views) via Springs Http Message Converter.- Returns:
- the model that will be used to find the correct view and then rendered using that view
- See Also:
- API Note
- Notice that the method has to be annotated with
@ResponseBody
.
-
templateModel
Here we use the generated code directly and return aTemplateModel
which is analogous toModelAndView
.- Returns:
- the template model pair that already has the template found.
- See Also:
- API Note
- Notice that the method has to be annotated with
@ResponseBody
.
-
entity
Here we use aResponseEntity
which allows use to set status codes with our model to be rendered.- Returns:
- a response entity.
- See Also:
-
mvc
Here we could useJStacheInterfaces
to make our model implementJStachioModelView
to support the traditional servlet MVC approach. The model will use the static jstachio singleton that will be the spring one.This approach has pros and cons. It makes your models slightly coupled to Spring MVC but allows you to return different views if say you had to redirect on some inputs (
RedirectView
).- Returns:
- the model and view that will be used as View (see
HelloModelAndView
). - See Also:
- API Note
- Notice that the return type is
View
.
-
wired
Here we use the wired renderer.- Returns:
- template model pair derived from
Template.model(Object)
.
-