tl;dr - I want to set a put-attribute in my tiles definitions based on the individual model passed to a view in tiles
I'm trying to create a page to display a user using spring and tiles. Right now my page gets the model fine, but I want the title of the page to include the username (which it would have to get from the model). I've included some excerpts from my code below:
tiles.xml:
<definition name="baseLayout" template="/WEB-INF/jsp/layout/layout.jsp">
<put-attribute name="title" value="FitterBlog" />
<put-attribute name="header" value="/WEB-INF/jsp/layout/header.jsp" />
<put-attribute name="nav" value="/WEB-INF/jsp/layout/nav.jsp" />
<put-attribute name="body" value="" />
<put-attribute name="footer" value="/WEB-INF/jsp/layout/footer.jsp" />
<put-attribute name="ads" value="/WEB-INF/jsp/layout/ads.jsp" />
<put-attribute name="css-layout" value="/FitterBlog/resources/css/layout.css" />
</definition>
<definition name="user/display" extends="baseLayout">
<put-attribute name="title" value="FitterBlog - ${user.username}" />
<put-attribute name="body" value="/WEB-INF/jsp/user/display.jsp" />
</definition>
As you can see, I've tried using the same syntax as I would in a jsp to display the username. ie. I tried using ${user.username} to display the username, however that doesn't work and I just get the text "${user.username}" displayed in the title instead of the actual username.
display.jsp:
//output the username from the user model, this works fine
${user.username}
UserController.java
@RequestMapping(value="display/**")
public ModelAndView displayUser(@ModelAttribute("user") User user, BindingResult result) {
//TODO
//retrieve user number from the URL
//retrieve user from database
//display user details
user.setUsername("Awesome username!");
return new ModelAndView("user/display", "user", user);
}
as you can see, I'm still in heavy development and currently set the username on the user object myself (instead of getting it from the database), but that's beside the point.
After reading a few different sites (also, thanks for the link Ralph, it gave me a place to start) I have found a solution.
Basically what I needed to do was edit my DTD to use version 2.1 and also to use an expression put-attribute instead of a value put-attribute. Here is the relevant part to the tiles.xml file:
<!DOCTYPE tiles-definitions PUBLIC
"-//Apache Software Foundation//DTD Tiles Configuration 2.1//EN"
"http://tiles.apache.org/dtds/tiles-config_2_1.dtd">
<definition name="user/view" extends="baseLayout">
<put-attribute name="title" expression="FitterBlog | ${user.username}" />
<put-attribute name="body" value="/WEB-INF/jsp/user/view.jsp" />
</definition>
See this part of the tiles documentation: Expression Language support
It looks like you need to enable the EL-Support first.
I know this is not a complete solution, but I hope this hint helps to resolve the problem. -- If it helps, please post the solution you found.
来源:https://stackoverflow.com/questions/8723190/spring-tiles2-put-attribute-from-model