I'm starting with Spring MVC using JSP and Apache Tiles. I've learned that I can define views in a tile definition file like:
<definition name="index" extends="base.definition">
<put-attribute name="body" value="/WEB-INF/jsp/bodies/index.jsp" />
</definition>
However going forward like this, I'll need to repeat this simple pattern for every body template, replacing the two appearances of "index". Isn't there a way to avoid this repetition?
Tiles supports wildcards too. From the documentation here:
http://tiles.apache.org/framework/tutorial/advanced/wildcard.html
<definition name="bank/*" template="/layout.jsp">
<put-attribute name="body" value="/{1}.jsp"/>
</definition>
The {1} now refers to whatever the * matched in the view that was called. However, you may find it simpler to just keep the repetition.
Related
Good Evening,
I am creating a project based on Spring MVC for the back end and Twitter Bootstrap for the front end.
I would like to create a template for recurring my pages (header and footer fixed for all pages) only that I would like to avoid repeating the code in all the pages (as it is not good practice and can lead to mistakes). Is there a way to centralize what? I thought about the jsp (one for the header and the footer) and recall within the various view. Is there a way to do so? Or there better alternatives?
thanks
You can use the JSP... There's include tag, you can use like this:
<%# include file="/views/templates/header.jsp" %>
// Rest of the content
<%# include file="/views/templates/footer.jsp" %>
Or you can use a framework for templates, like Tiles or Freemarker.
Spring MVC with Tiles: http://java.dzone.com/articles/spring-mvc-tiles-3-integration
Using tiles, the template page:
<body>
<div class="container" style="border: #C1C1C1 solid 1px; border-radius:10px;">
<!-- Header -->
<tiles:insertAttribute name="header" />
<!-- Body Page -->
<div class="span-19 last">
<tiles:insertAttribute name="body" />
</div>
<!-- Footer Page -->
<tiles:insertAttribute name="footer" />
</div>
</body>
And you can change dynamically the "body" tiles attribute.
I'm using Spring + Thymeleaf + Tiles combination.
I have a Tiles definition with a string attribute:
<definition name="myDef">
<put-attribute name="myAttr" value="foo"/>
</definition>
In a Thymeleaf template I want to use myAttr in expressions:
<div th:if="${myAttr == 'foo'}">...</div>
How whould I do it?
All I found so far is how to renger Tiles attributes directly to the output (<span tiles:string="myAttr" />), but wasn't able to figure out how to insert its content into a context variable.
Thank you.
I ended up doing a workaround - render Tiles attribute to a <span>, replace Thymeleaf conditions with JavaScript conditions (I'm using Angular), read that <span> value into a JS variable and let the client to do the rest.
I need to perform simple form validation using JSF. Looking at different online examples I see two patterns:
<h:inputText id="name" value="#{bean.name}" label="name">
<f:validateRequired />
</h:inputText>
...or...
<f:inputText id="name" value="#{bean.name}" label="name">
<f:validateRequired />
</f:inputText>
The only difference is the namespace of the inputText tag. Both solutions seem to work, but which one is "more correct" and what makes it a better choice?
There is no such thing f:inputText, inputText is a part of the http://java.sun.com/jsf/html namespace and should use the h prefix,
like this: xmlns:h="http://java.sun.com/jsf/html"
Where have you seen the <f:inputText anyway ?
The only way that you can use <f:inputText is only if you assign the f prefix your self instead of the h , like this: xmlns:f="http://java.sun.com/jsf/html" (but its really not a good idea) better stick to the original h
I have a header,menu and a body to load the jsps. When i click a menu element everytime, the entire tile reloads. But i want to show the users the selected menu item. Since the tiles reloads, i am not able to get the element which was clicked by the user.Please let me resolve this. Thanks
<definition name="main-tiles" template="/WEB-INF/jsp/menu/home.jsp">
<put-attribute name="header" value="/WEB-INF/jsp/menu/header.jsp" />
<put-attribute name="menu" value="/WEB-INF/jsp/menu/menu.jsp" />
<put-attribute name="body" value="" />
</definition>
<definition name="home" extends="main-tiles">
<put-attribute name="body" value="" />
</definition>
<!-- User Tiles -->
<definition name="newUser" extends="main-tiles">
<put-attribute name="body" value="/WEB-INF/jsp/userandorg/user/newUser.jsp" />
</definition>
Tiles don't really 'communicate' with each other.
When a tiles-composed page is fully rendered in your browser, view the html (view source) and you should be hard-pressed to see any tile framework showing through. This means that when your user clicks on a menu, it is a round-trip communication back to your server which runs through your servlet code and then uses the tiles framework to compose the resulting html.
What you describe as the desired behavior is more like frameset or iframe behavior where different sections of your browser pane are literally different requests/responses. You can achieve the same effect with AJAX frameworks which can be a call to your servlet and then dynamically update specific blocks of your html (usually defined as div or span elements) without refreshing the entire page.
But, if you want to merely highlight the clicked menu using your tiles composition, have your servlet identify which menu was clicked and then use some JSTL on your menu.jsp page to identify the menu item link and provide some CSS styling to the menu link. It is still a round-trip (non-AJAX) communication, but it should give you the effect you describe.
I have a jsp with the following (relevant) setup:
<s:url value="/res" var="res_url" />
<link href="${res_url}/less/bootstrap.less" rel="stylesheet/less">
<link href="${res_url}/less/responsive.less" rel="stylesheet/less">
...
Ive noticed a problem with using this technique, in that on the first page load of a new session my res_url variable will have ";jsessionid=xxxxxxxxx" appended. In this case that means the id appears in the middle of my stylesheet URL and therefore the stylesheets are not loaded.
I realize that I'm probably not using the URL tag in the way its intended, and that you can include param tags inside the URL tag to get around this, but I don't like the idea of it and think the way i did it was much cleaner. Is it possible to somehow tell it to ignore the jsessionid? Or is there any other way of doing this?
I don't see the benefit of using Spring's URL tag over the standard JSTL tag. What about
<c:url value="/res/less/bootstrap.less" var="lessBootstrap" />
<link href="${lessBootstrap}" rel="stylesheet/less">
If you want to define the /res/less path in a variable instead of repeating it you may do this like this:
<c:set var="resDir" value="/res/less" scope="request" />
The right way to do it is
<link href="<s:url value="/res/less/bootstrap.less"/>" rel="stylesheet/less">
<link href="<s:url value="/res/less/responsive.less"/>" rel="stylesheet/less">
I don't see what any simpler way to do it.