Sitemesh change decorator on runtime - java

My greetings!
The question is pretty short: is there any way to change the decorator during runtime? For example, I have a dropdown menu with some "decorator styles", so when the user chooses different style, it will change the decorator.
If you have any useful links on this topic, I'd be very grateful.
Found this thread - SiteMesh: Changing the content-type of the response - but still, no help.

I know you can use a meta HTML tag to specify which decorator you want to use with your JSP files. For example in the file login.jsp where I need the login decorator:
<head>
<meta name="decorator" content="login" />
<!-- where "login" is the name of the decorator -->
</head>
So, I never tried yet, but you could probably give the name of the decorator through a POST or a GET parameter, and using it within the meta tag:
<meta name="decorator" content="${decoratorName}" />

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!

Sitemesh custom javascript per page

I am using sitemesh for a spring based site. The problem is that I have some javascript that I want it to run on the onload event of only one specific page using jquery $(function() { ... }) and not on every page.
I have included the jquery.js in the bottom of my decorator, after the body. So, if I try to include a <script> tag in the body of my decorated page the script won't be executed because jquery will be loaded after that! I know that I could include the jquery.js in the header of my decorator so it will be before the custom script in the decorated page however I don't really like that solution since it will contain javascritp in the head of my page.
So I would like to have something like a placeholder in my sitemesh decorator in where the custom from my decorated page will be placed (as you can understand I come from the django world :p). Is this possible ? Do you propose anything else or should I just put my jquery.js in the header and be done with it ?
To answer my question, after some search I found the following solution:
I Added the following to the end of my decorator page (after including jquery.js)
<decorator:getProperty property="page.local_script"></decorator:getProperty>
I also added the following
<content tag="local_script">
<script>
$(function() {
alert("Starting");
});
</script>
</content>
The decorated result page contained the contents of the local_script tag exactly where I wanted them and everything worked fine :)
I don't know why this feature of sitemesh is not properly documented - using this you can have a great templating behaviour (like django).

Java/Scala: understanding Play framework custom tag or helper

I've been using Play 2.0 framework for a couple of days now for a proof of concept application at my job. One of the first things I wanted to check out was the custom tag functionality, since it reminded me of HtmlHelpers in ASP.Net MVC. The thing is that I can't seem to make them work and was wondering if I'm misusing the feature or misunderstanding something.
Here's a simple example of what I want to do: I want to be able to use #script("scriptname.js") anywhere in the templates and have that subsitute the entire tags.
Here's what I got so far:
main.scala.html
#(title: String, scripts: Html = Html(""))(content: Html)
#import tags._
<!DOCTYPE html>
<html>
<head>
<!-- this is how I would like to use the helper/tag -->
#script("jquery.js")
#script("jquery-ui.js")
<!-- let views add their own scripts. this part is working OK -->
#scripts
</head>
<body>
#content
</body>
</html>
I created a subdirectory called "tags" under the app/views directory. There I created my script.scala.html tag/helper file:
#(name: String)
<script src="#routes.Assets.at("javascripts/#name")" type="text/javascript"></script>
The problem I'm having is that whenever I use #script() the output includes the #name parameter in it. For example #script("x.js") actually outputs
<script src="assets/javascripts/#name" type="text/javascript"></script>
What am I doing wrong?
For the record, I did read the documentation and search here, but neither of these links have helped me figure this out:
http://www.playframework.org/documentation/2.0.3/JavaTemplateUseCases
How to define a tag with Play 2.0?
#routes.Assets.at(...) evaluates the Scala expression routes.Assets.at(...) and substitutes the result into your output. There is no recursive evaluation that would allow you to have evaluate an expression textually to get that expression, which seems to be what you're expecting.
What you intend to do is achieved using
#routes.Assets.at("javascripts/" + name)

How can I implement CSS Cache Busting with JSF outputStylesheet?

In JSF page templates I use this code to include a CSS resource:
<h:outputStylesheet library="css" name="mystyles.css" />
The usual way to implement CSS cache busting would be to add a version parameter, like v=123, however this is not supported in outputStyleSheet:
<h:outputStylesheet library="css" name="mystyles.css?v=123" />
will cause a JSF1064 warning and the CSS will not be found.
That's not possible without overridding the StylesheetRenderer (assuming you're on Mojarra). It does indeed not take the query string into account. However, as a (temporary) workaround it's good to know that it is valid to include the CSS using CSS' own #import rule inside <h:outputStyleSheet>.
<h:outputStylesheet target="head">
#import url('css/mystyles.css?v=123')
</h:outputStylesheet>
You might want to post an enhancement request to the Mojarra boys to take this into account in future releases.

Spring MVC: Relative URL problems

I have a controller bound the URL: "/ruleManagement".
Inside my JSP, I have a form that forwards (on submit) to "ruleManagement/save" url. When there are errors with the input fields, I want it to return back the original form View. This is where the problem starts...
Problem 1) Now that the URL is "/ruleManagement/save", my form submit now points to "/ruleManagement/ruleManagement/save".
Problem 2) I tried using spring:url tag to generate the absolute paths for me, which usually works great. But when I put a spring:url tag inside of a tag, the spring:url tag does not get parsed correctly.
<form:form action="<spring:url value='/ruleManagement/save' ...>" method="post">
When I analyze the DOM after the page loads, my form tag looks something like:
<form action='<spring:url value="/ruleManagement/save" />' ... >
If I don't use the spring:url tag, and instead use just "/ruleManagement/save", the url generated excludes my application name in the url, which is also wrong.
How do I generate a consistent URL pattern across all Views regardless of path? If the answer is "using spring:url", how do I get that content inside a form:form tag?
Custom tags in JSP can't be used in attributes of other custom tags, so you need to store intermediate result in a request attribute (using var to redirect output of the tag to the request attribute is a common idiom supported by many tags):
<spring:url var = "action" value='/ruleManagement/save' ... />
<form:form action="${action}" method="post">
I too would love to be able to generate a consistent URL path across all Views! Is this possible with <spring:url .../>.
To answer your second question & tacking on to axtavt's answer, embed the <spring:url ... /> into the form action after adding the property htmlEscape="true"
Example: <form:form action="<spring:url value="/ruleManagement/save" htmlEscape="true" .../>" method="post">

Categories

Resources