java - Populate model with default attributes -
i building website using spring-mvc framework. in template want include 'generic' information. generic in way not specific current controller, used on every page. can generated menu, number of minutes user logged in or current temperature on north pole.
what doing i've made abstract class method populate model default values. controllers extend class , phone call method in every (@requestmapping-ed) method, downside. feels kinda hacky , not flexible.
@component public abstract class abstractbasecontroller { @autowired private someservice someservice; protected void populatemodel(model model) { model.addattribute("value", someservice.getgenericvalue()); } }
@controller public class homecontroller extends abstractbasecontroller { @requestmapping("/") public string index(model model) { populatemodel(model); homecoming "home"; } }
my question how should provide these generic attributes view. there improve way doing now?
(i using spring/spring-mvc 4, javaconfig, thymeleaf thymeleaf-layout-dialect)
okay, wasn't hard. can annotate method @modelattribute
makes available in template.
@component public abstract class abstractbasecontroller { @autowired private someservice someservice; @modelattribute("value") protected string getvalue() { homecoming someservice.getgenericvalue()); } }
mind you, method called. when ${value}
not called in template. not more 'flexible' method described in question.
java spring-mvc
No comments:
Post a Comment