I want to set language value of a gsp page dynamically . Currently I am just doing it using basic hardcoded value . I did find something with JS Onload event described here.
But I wanted to find something that is GSP driven . Is there any way ?
My current code looks like <html lang="en">
I think maybe you are thinking of this in a more complex way than it actuall is.
In grails you have your layouts/main.gsp which is your sitemesh.
The tag <html lang='en' is declared at the very top of this
If you simply edit this page and add the following:
<g:set var="locale" value="${session?.'org.springframework.web.servlet.i18n.SessionLocaleResolver.LOCALE'?:java.util.Locale.UK}"/>
<html lang="${locale?.language?:'en'}" class="no-js">
Then when I visit my site:
localhost:8080/?lang=ja_JP view source shows:
<html lang="ja" class="no-js">
You need to do that for each sitemesh that requires to do this - having a read about this property it seems it doesn't do much for the browser but may help non human things such as search engines.
Related
We are using CAS 5.2.3 which uses an upgraded version of Thymeleaf. Thymeleaf has restricted access to certain request features - '#request.getParameters()' being one. Is there any work around for it? I am getting the following error when trying to access it - "Access to request parameters is forbidden in this context. Note some restrictions apply to variable access. For example, direct access to request parameters is forbidden in preprocessing and unescaped expressions, in TEXT template mode, in fragment insertion specifications and in some specific attribute processors."
Good question. I face this problem a few months before, it is solvable.
After looking through their source code, I found that they are limiting the usage of #request.getParameters() only on specific tag, they didn't forbid to use #request.getParameters() in some situation.
In my use case, I am able to use CData to bypass this checking. Not sure whether it applies to your use case since you didn't provide any code example....
Anyway, the below example want to redirect user to another page, based on the parameter url
Here's an sample code that was broken in CAS 5.2.x, but worked in CAS 5.1.x :
<html>
<head>
<title> Deforestation </title>
</head>
<body th:attr="onload='window.location.href=\''+${#request.getParameter('url')}+'\''">
</body>
</html>
Here's a work around code:
<html>
<head>
<title> Deforestation </title>
</head>
<body>
Logging out. Please wait...
<script th:inline="javascript">
/*<![CDATA[*/
location.href = /*[[( ${#request.getParameter('url')} )]]*/ ;
/*]]>*/
</script>
</body>
</html>
If this didn't solve your problem, please provide your source code so we can have a better look at the problem.
Note: There is a security reason why this stuff is now banned, using this workaround might compromise the security standard, do remember to sanitize the user input if neccesary
Edit:
as per comment, although not elegant, maybe something like the following will work?
<html>
<head>
<title> Data attribute </title>
</head>
<body>
<span id="foobarid"> </span>
<script th:inline="javascript">
/*<![CDATA[*/
$('#foobarid').data('foo-bar',/*[[( ${#request.getParameter('foo') == 'bar'} )]]*/);
/*]]>*/
</script>
</body>
</html>
Apparently one can spend hours, even days on this simple requirement, which Thymeleaf introduced as a poorly dcumented annoyance in later versions. I've spent hours upgrading from an older Thymeleaf version from 5 years ago and got stuck on this very same problem. The lack of documentation didn't either. When I finally took a think break, I've realized the solution to this problem is as simple as using the <c:set> core tag with JSP.
Simply use the th:with tag in some higher level html tag where accessing request parameters is allowed, like this:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
xmlns="http://www.w3.org/1999/xhtml"
th:with="action=${param.action != null ?
param.action[0] : 'none'}">
<head>
...
In this example, the request parameter action is stored in a page context local variable named action (the name can be any unique name that you choose). This variable is now accessible anywhere bellow your high level html tag where you've defined it. It can even be used in following th:xx tags in the same html tag after the declaration. So basically, now you can do this:
<th:block th:switch="${action}">
<title th:case="'edit'" th:text="#{page.contacts.edit}"></title>
<title th:case="'delete'" th:text="#{page.contacts.delete}"></title>
<title th:case="*" th:text="#{page.contacts.add}"></title>
</th:block>
and even call a parameterized fragment by simply referring to your variable as ${action}. In this example I have a navbar template fragment, which accepts three parameters. The last of which, is a request parameter that otherwise, is inaccessible due to the newer Thymeleaf request object access restrictions.
<div id="wrapper">
<div class="nav" th:replace="html/fragments/navbar ::
navbar('html/contact','contact-html', ${action})">
</div>
<div class="content">
...
If you need more request params and more local page context variables, just use a comma to separate the declarations in the th:with tag like this:
th:with="action=${param.action == null ? 'none': param.action[0]},
self=${param.self == null ? 'none': param.self[0]}"
I hope this saves you guys some precious time and voids the frustration of refactoring older Thymeleaf html pages. It definitely beats those horrifically unmanageable CDATA scriptlets inside your pages.
I have created a custom component which is used in xhtml view and it takes the attribute value and print it(like helloworld in below code), in JSF 2.1, using Netbeans8.1 and glassfish4.1. I have created a custom component by extending UIComponentBase overridden family and encode begin and custom component tag class by extending UIComponentELTag overriding componentType and rendererType methods. I have defined a tag library for it test.taglib.xml and registered it in web.xml.
Below is the view code.
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:body>
<h2>JSF Custom Input FacesComponent Example</h2>
<h:form>
<ui:composition
xmlns:custom="http://packt.net/cookbook/components"
xmlns:ui="http://java.sun.com/jsf/facelets">
<custom:testInput helloworld="test component!!"/>
</ui:composition>
</h:form>
</h:body>
</html>
The problem is - it is only displaying the html value and not displaying the custom component attribute value.
The same example works for when I tried for jsp view (with tld file).
I tried using custom component tag directly or as child of ui:composition but no luck.
I put a break point in the custom component and tried to debug, but control is not even going to the break point.
Since control is not going to break point, so does it means my custom component is not getting registered by the servlet? If so them please tell me what I am doing wrong.
I am new to JSF so please tell me if I am missing any point.
I tried the same with JSF2.2.7 but that also exhibits the same problem.
I cannot find the root cause but got my problem solved. I tried the same code with JSF 2.2.7 jar but I used annotations instead.
As mentioned in the problem that it didn't worked with 2.2.7, was because I was using old fashion code, like creating taglib and updating it in deployment descriptor.
Generally Rails main application views/layout omits the <html> tag, focusing on defining what should be inside of it, like the <head> and <body> tags.
What I'm actually trying to do is to define a simple attribute inside of the <html> top level tag, but I don't know where to write it.
Any tips?
It's available in application.html.erb in the views/layout folder
This file in included by default in all your views.
This is no major issue, but I just want to upgrade my understanding of the specifications.
I started using ui:composition as the root element of my JSF pages. When my template starts using tag libraries that are NOT in the client, I get warning messages on the rendered page.
So the Netbeans wizard gives me this:
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns:ui="http://java.sun.com/jsf/facelets"
template="./template.xhtml">
<ui:define name="content">
<h1>A simple page</h1>
<p>
Hi there.
</p>
</ui:define>
And the rendered page shows:
Warning: This page calls for XML namespace declared with prefix p but no taglibrary exists for that namespace.
Warning: This page calls for XML namespace declared with prefix h1 but no taglibrary exists for that namespace.
So I add the xmlns declarations and everything is fine, but is this the way that it is supposed to work? If not is the discontinuity with the JSF specification, XML specification, or just the Mojarra implementation, or none of the above?
but is this the way that it is supposed to work?
Yes. JSF (and in particular Facelets) leverages plain old XML. Both the template client and the template definition are XML documents, so both need to specify their elements' namespaces in accordance with the XML specification. Put differently, the XML parser doesn't know that the document it parses will later be interpreted as a template client definition by JSF, but parses it as "just another document".
It's similar to normal Java code. If you want to use any classes, you must declare by importing their library. XML namespace is just something you need to include to declare the libraries of tags that you are going to use. It's absolutely normal that XML namespace is effective in template clients.
Besides, if you use <ui:composition> as your root element, you should not use normal HTML tags. Instead, you should change them to the equivalent JSF tags. If you need to use HTML tags, use <html> as your root element and the warning will be gone.
In my struts 2.0.12 application I'm trying to use s:datetimepicker
but it does not render.
Firebug error: dojo is not defined
on dojo.require("dojo.widget.DatePicker");
My jsp page
<%#taglib prefix="s" uri="/struts-tags" %>
.
.
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<s:head theme="ajax" debug="true"/>
.
.
</head>
<body>
.
.
<s:datetimepicker name="dataInizioRicerca" label="data inizio ricerca (yyyy-MM-dd)" displayFormat="yyyy-MM-dd"/>
.
.
</body>
What's wrong? Did I miss something?
For use datetimepicker you need to use struts-dojo-tags, this came into de struts-dojo-plugin.jar of struts library, now u have the tags, loaded whit
and put inside head tag
to visualize the date time picker....
... maybe this can help you....
Yes you missed the Ajax header (Ajax struts theme) tag.
This loads the Dojo Javascript files at the start of the page.
Put the following in your HTML head:
<head>
[..other stuff]
<s:head/> <!-- Struts 2 Ajax/Dojo needed for calendar -->
</head>
By the way - the above post talks about struts 2.1.x and not struts 2.0.12 (the one you are using)
I know it's a while since this thread was updated but if someone experiences the problem above and does a search they might end up here and the following observations may be useful.
I got similar issues when using Dojo with Struts 2.1 but that was down to the major changes in how struts 2 works with Dojo. A useful resource in sorting out those issues can be found here:
Toubleshootng guide for migrating from Struts 2.0.x to Struts 2.1.x
If getting Dojo working was still causing problems you could try using the Struts 2 jquery plugin, (do a search for that as I am not allowed to post 2 hyperlinks)
You need to stop using struts dojo tags and use dojo independently. struts -dojo is not supported any more.