Getting Tiles' put-list-attribute to work with a Thymeleaf template - tiles

I am trying to get Apache Tiles' put-list-attribute to work with Thymeleaf.
Here is what I tried:
From Tiles config:
<put-list-attribute name="jsFilesList">
<add-attribute value="/js/libs/jquery-1.8.1.js"/>
<add-attribute value="/js/libs/jquery-ui-1.9.0.custom.js"/>
<add-attribute value="/js/libs/bootstrap.js"/>
</put-list-attribute>
From thymleaf template:
<script th:each="jsFile : ${jsFilesList}" th:src="#{${jsFile}}" type="text/javascript" ></script>
Nothing is rendered... It seems the list is empty from the thymeleaf template point of view...
Can anyone please help?

Tiles attributes are not by default available in the request scope. They live in their own tiles scope.
In jsps, to expose a tiles attribute from its scope to the request scope do the following:
<%# taglib prefix="tilesx" uri="http://tiles.apache.org/tags-tiles-extras" %>
<tilesx:useAttribute id="jsFilesList" name="jsFilesList"/>

Related

Mustache cannot resolve MVC view

I'm using the Mustache engine and I have this controller method:
#Controller
public class BetController {
#GetMapping("/bet")
public String show (Model model/*, #RequestParam long id*/){
//Bet bet = this.service.get(id);
return "bet_page"; //returns directly the template
}
}
Which it should return a view with a template called "bet_page":
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Apuestas</title>
</head>
<body>
<h1>Tus apuestas</h1>
<div id="navbar"></div>
<h2>Apuestas realizadas</h2>
{{bets}}
<h2>Próximos partidos</h2>
{{games}}
<div id="match"></div>
<script src="/static/assets/js/jquery.min.js"></script>
<script src="/static/assets/js/navbar.js"></script>
</body>
</html>
However, looks like Spring can't see the template, and IntelliJ throws this warning:
[![image showing warning: cannot resolve MVC view][1]][1]
I look into internet some solutions and none of these helped me to solve the problem.
By the way, here's the configuration that I use and the directory structure:
spring.mustache.suffix="html"
spring.mustache.prefix=classpath:/templates/
#ACCESS INFO
spring.datasource.url=jdbc:mysql://localhost/quidditch\
?useUnicode=true\
&useJDBCCompliantTimezoneShift=true\
&useLegacyDatetimeCode=false\
&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
#DATABASE PROPERTIES
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.jpa.hibernate.ddl-auto= create-drop
[![Directory structure of my project][2]][2]
Any guesses?
EDIT: Looks like something from SQL is affecting in loading the templates (I'm using MySQL with JPA an JDBC)
[1]: https://i.stack.imgur.com/c3fFm.png
[2]: https://i.stack.imgur.com/vzmMv.png

Get erb view from inside layout

I am trying to get the name of the current view being loaded into layout.erb. I am trying to inject a js file and a css file into the layout head. I am building the name for those files based on the name of the erb view:
<link rel="stylesheet" href="/devcss/<%= viewName%>.css">
<script src="/devjs/<%= viewName %>.js" type="text/javascript"></script>
How can I do this from the layout template?
UPDATE:I am not interested in knowing how to inject the script in a specific place in layout, which I think content_for is used for. What I need to knok in the example above is how to determine the viewName variable
Insert the following into application.html.erb after inside head tag
<%= "#{yield(:scripts)}".html_safe if content_for?(:scripts) %>
and in the individual erb, insert similar to the following
<% content_for :scripts do %>
<link rel="stylesheet" href="/devcss/<%= viewName%>.css">
<script src="/devjs/<%= viewName %>.js" type="text/javascript"></script>
<% end %>
This will allow you to insert page specific codes into head and other tags not accessible to your individual erbs

Struts 2 ajax sx:div refresh not working after upgrade

I recently upgraded from Struts 2.0.11 to the latest version (2.3.15) and have found that I can not get an sx:div tag to refresh. This is the code:
<%# taglib prefix="sx" uri="/struts-dojo-tags" %>
<sx:head debug="true" />
<script language="javascript" type="text/javascript">
var controller = {
refresh : function() {}
};
dojo.event.topic.registerPublisher("/refresh", controller, "refresh");
</script>
<s:url id="URLAjaxKaptcha" namespace="%{#attr.kaptchaNamespace}" action="kaptcha"/>
<sx:div id="prosoc_kaptcha" theme="ajax" href="%{URLAjaxKaptcha}"
loadingText="<small>loading …</small>"
errorText="Security code is loading - please wait"
listenTopics="/refresh" cssClass="kaptchaDiv">
</sx:div>
<s:a href="javascript:void(0)" onclick="controller.refresh()">click to get a new image</s:a>.
Before the upgrade, when the user clicked the refresh hyperlink a new kaptcha image was returned to the sx:div. After the upgrade this doesn't work any more.
I am not getting any error messages and I can place an alert in a beforeNotifyTopics and afterNotifyTopics so I know that the refresh function is being called. I just don't get a new kaptcha image in the sx:div.
Does anyone have any ideas?

Ajax using <g:remoteLink> in Grails

Reading through the Grails docs (see here http://grails.org/doc/latest/guide/theWebLayer.html#ajax), I was led to believe that I could use Ajax to update a div using the following syntax:
My view (Ajax/index.gsp)
<!doctype html>
<head>
<meta name="layout" content="main"/>
</head>
<body>
<div id="error"></div>
<div id="message"></div>
<g:remoteLink action="retrievePets" update="message">Ajax magic... Click here</g:remoteLink>
</body>
</html>
My controller (AjaxController):
package genericsite
class AjaxController {
def index() { }
def retrieveMessage() {
render "Weeee! Ajax!"
}
}
However, when I select the link, it just sends me to a page with "Weeee! Ajax!" I know how to do this the typical jQuery way. This is slightly more convenient...
The default "main" layout doesn't include a javascript library by default, so if you want to use remoteLink or any of its associates you'll need to add
<r:require module="jquery"/>
or (if you're on a pre-2.0 version of Grails or not using the resources plugin)
<g:javascript library="jquery"/>
to the <head> section of your GSP.

meta tag in ASP.NET MVC 3

How can I put meta tag to work only for one page. If I want to put it .aspx file, where is right place.
Thanks.
Since you haven't said yet, I'm assuming you're using the Razor engine (the "default" for new MVC3 projects). In that case, you just need to insert a new section into your layout view, and only render that section if you need to insert a meta tag.
For example, working from the stock New ASP.NET MVC 3 Project template, you would edit your Views\Shared\_Layout.cshtml file, and before the closing </head> tag, do something like this:
#this.RenderSection("MetaContent", false)
</head>
Then, in any of your views that you needed to, add this:
#section MetaContent
{
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE8" />
}
If you're still using the ASPX layout engine for some reason, you can accomplish the same thing using the <asp:ContentPlaceHolder> tags in your master page and <asp:Content> tags in your views.
EDIT:
Since you're using the ASP.NET Forms layout engine still, here's the same basic idea as above in aspx syntax:
In your master page, you add the tag:
<asp:ContentPlaceHolder ID="MetaContent" runat="server" />
</head>
And in your .aspx views, you add a new content section (you should already have at least two -- a title and a body):
<asp:Content ID="Meta" ContentPlaceHolderID="MetaContent" runat="server">
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE8" />
</asp:Content>
I was going to suggest exactly what Michael said (+1). Another option would be to put a boolean in the ViewBag, something like:
ViewBag.ForceIE8Mode = true;
For pages that you want to force into IE8 Mode.
and then in your view, wrap the meta tag in a conditional. either
#if(ViewBag.ForceIE8Mode == true) {
<meta... />
}
or
<% if(ViewBag.ForceIE8Mode == true) { %>
<meta... />
<% } %>

Resources