header and footer and freemarker - java

My website has a consistant header and footer that I want on all pages.
What is the best way to do this?
The header will have some dynamic data also, based on the current view.
P.S Does freemarker have any sort of master page functionality? Where I can create a base template, then have other templates build upon the base?
Basically I want to design a template that has header and footer + a place holder for the main content area.
THEN, all other pages will inherit the main template (with header + footer), and then inject the action's render output into the templates main content area.

Define a macro like this in an import library:
<#macro page title>
<html><head><title>${title?html}</title></head>
<body>
<!-- header section -->
<#nested/>
<!-- footer section -->
</body></html>
</#macro>
and use the following template for all your pages:
<#import "common.ftl" as c/>
<#c.page title="custom page title">
<!-- custom page content -->
</#c.page>

You can find the spring-specific part in the spring reference and the freemarker part in the freemarker online docs
(It doesn't seem like Freemarker supports master pages, but through recursive use of include you can achieve a high level of code reuse)

Have a look at SiteMesh. I believe you will find it useful.

Related

Freemarker - call external java function with page number as parameter

I have Freemarker templates for generating pdf documents, that can have multiple pages based on variables content. I need generate unique code on each page, the generating logic takes page number as parameter. I have working utils class, callable from template used for dates formatting etc.
Ideally I would like to be able call my utils class like that, with something that would provide current page number on each page (simplified example):
<h1>Utils result: ${utils.generateUniqueNumber(pageNumber)}</h1>
I will probably need to place this code into header/footer, to achieve have it on each page.
Currently I have working page counter in footer defined using css:
<head>
<title>RESEA - Selection Notice OWCMS</title>
<style type="text/css">
.page-counter:before {
content: "Page "counter(page)" of "counter(pages)
}
</style>
</head>
Then used in footer , works as expected.
I'm not sure if actual paging is driven by Freemarker or CSS, so maybe I need to make CSS call my utils function.
Any advise, or relevant study source is welcome!

Is there a way to display thymeleaf tags on an angular JS partial?

Currently, I have my main page rendered by the thymeleaf resolver server-side, so tags such as
<span th:text="#{userId}"></span>
works properly. However, when I load a partial page using angular's ng-route and routeProvider like
$routeProvider.
when('/c1000', {
templateUrl: 'pages/C1000.html'
});
and in my C1000.html, I have a tag such as
<span th:text="#{userId}"></span>
It does not display. I am assuming it's because the main page is resolved by thymeleaf's resolver while the partial is loaded as an ordinary HTML file. I have implemented internationalization so the text for #{userId} changes language based on the properties file. What I want is for the partial page to also display #{userId} similar to how my main page displays it. Is there a way to do this?

sitemesh layout

The Stripes web framework includes a layout engine which provides the following feature:
Layout Definition File - layout.jsp
<s:layout-definition>
Header
<div><s:layout-component name="body"/></div>
Footer
</s:layout-definition>
Page Using layout.jsp
<s:layout-render name="/layout.jsp">
<s:layout-component name="body">
Hello, reusable layout
</s:layout-component>
</s:layout-render>
Generated Content
Header
<div>Hello, reusable layout</div>
Footer
In the layout definition I specify the position of a component and the page defines the content of this component. I'm looking for an equivalent feature in Sitemesh, which I'm using in a Grails application.
It seems that Sitemesh will only only allow one to posititon (or decorate) the entire page body. Is there any way I can position more fine-grained content elements as above?
If you need more fine-grained content elements, take a look at Sitemesh Content Blocks

spring mvc - easiest way to check on what page you are and change style of the menu items

I'm working on a spring mvc project. I need to change the style of my menu items when I'm on a particular page. The css is done. But i still need a way to check on which page I am.
What is the easiest way to do this. All solutions are appreciated.
,
thank you
You can integrate Apache Tiles into your Spring MVC project (exmple here) and pass path to css needed in tiles.xml.
Alternatively you can send this path to your JSP page in JavaBean, but it is less declarative and requires accurate manipulating of beans.
Try this short cut.
Set the style's class name in the ModelAndView as a variable. In the JSP files directly use the variable as the style's class name.
In the controller
modelAndView.add("styleVariableName","styleToBeApplied")
In the JSP
<div class="${styleVariableName}">
styleToBeApplied should be a css class and you can repeat this for every controller action.

JSF runtime pagename passing

I have an include page which is a navigation menu. When i click on those menu i want to refresh the content area of layout with a certain page. How can i pass the page name into a JSF page using include tag
I dont want to switch to facelets and also i tried using $ and calling the backing bean method. It works but no css or richfaces components renders properly.
Thanks
Raj
I am not sure about the RichFaces part, but you can just use EL in <jsp:include> as well.
<jsp:include page="/WEB-INF/#{bean.pagename}.jsp" />
If bean.getPagename() returns for example home, then this will include /WEB-INF/home.jsp. You also need to ensure that the JSF/HTML contents of home.jsp is wrapped by a <f:subview> with an unique ID.
As to the CSS trouble, just ensure that the generated HTML validates and that the CSS imports in the <link> tags are all valid.

Categories

Resources