I've just started studying Spring, and I'm trying to make a web application. I have some common UI elements such as footer, header, sidebar and also want to add dynamic blocks, i.e. widgets. What widgets to include need to be chosen in Java code, not JSP template.
And that's where I'm stuck. Every controller returns it's own view - main.jsp
which includes some static and dynamic blocks:
<jsp:include page="head.jsp" />
<c:forEach items="${viewList}" var="viewName">
<jsp:include page="${viewName}.jsp" />
</c:forEach>
...
In controler I'm passing view list and object that will be used as widget to model:
...
views.add(moduleOne.getViewName());
views.add(moduleTwo.getViewName());
model.addAllAttributes(moduleOne.getAttr());
model.addAllAttributes(moduleTwo.getAttr());
model.addAttribute(IModule.VIEW_LIST, views); // passing all views that will be included into main.jsp
...
return "main";
But it doesn't work because when I use jsp:include page
model parameters do not pass to includee. Also I can't %@include file=
because in that case I won't be able to use variables and I would need to know which view to pass at compile time.
Of course if there's no solution, I'll have to hardcode all possible widget views in main.jsp
and switch between them:
switch($i) {
case 1:
include file=widget1.jsp;
break;
case 2:
include file=widget2.jsp;
break;
...
}
Which is obviously not flexible.
So, the questions are:
- Is there any way to solve the problem of passing model to include views without using
%@include file=
and make my widgets working? - Is there some better way to implement such widgets structure?
Thank you!