How to pass a servlet context parameter in gsp page - css

Can anyone know how to pass the servlet context value in gsp page.
I need to pass the servlet context value customcss to the resource link.
If I print the ${application.customcss} in my gsp page I am getting the value as main.css.
But when i try to pass the same to file link then its not taking the ${application.customcss} value.
<link rel="stylesheet" href="${resource(dir: 'css', file:'${application.customcss}')}" type="text/css">
In my page the link is get not get created properly.Its directly taking the ${application.customcss} as a file.
<link rel="stylesheet" href="/SAM-1.0/css/${application.customcss}" type="text/css">

try:
<link rel="stylesheet" href="${resource(dir: 'css', file: application.customcss)}" type="text/css">
${resource(... is already Groovy code, you don't need to use another block of ${..} inside

Related

<c:set> just assign its value after refreshing the page

I have following variable to be used as a public variable in the application. As you can see it has the main domain of the website as its value. The issue is, once the application is loaded it does not have any value, I need to refresh the page for it to have its correct value.
<c:set var="url" scope="application"
value="http://www.example.com" />
I am using Tiles3 and this line is in baseLayout.jsp file.
Code
<link rel="stylesheet"
href="${url}/resources/mystyle.css">
At first the addresses in the source of the page that are supposed to have the URL value are as following
<link rel="stylesheet"
href="/resources/mystyle.css">
After refreshing the page the address will become as following
<link rel="stylesheet"
href="http://www.example.com/resources/mystyle.css">
in your baseLayout.jsp, use
<link rel="stylesheet" href="resources/style.css" />
Dont write ${url}

How to load CSS for different URL in spring MVC app

I have a problem loading css for different URL.
This is my current structure:
-webapp
|--resources
|---css
|---generalize.css
|---js
|---images
|--WEB-INF
|---views
|---login
|---dashboard
This is my spring configuration to look for the resources:
<mvc:resources mapping="/resources/**" location="/resources/" />
And in the jsp page, I try to load it as:
<link href="<c:url value="resources/css/generalize.css" />" rel="stylesheet" type="text/css" />
If I have an url like localhost:8888/myapp/login or localhost:8888/myapp/dashboard, the css will apply fine. But when I try to map the dashboard page with the url localhost:8888/myapp/user/dashboard, the page loses its css style. Would anyone help to explain how would be the correct way to load the css (or using the resources)?
I have tried to prepend the contextPath as below but it does not work as well.
<link href="<c:url value="${pageContext.request.contextPath}/resources/css/generalize.css" />" rel="stylesheet" type="text/css" />
Simply use context relative path by appending / in the beginning.
<c:url value="/resources/css/generalize.css" />
You don't need to append context path in the beginning.

Why does MVC automatically expand virtual URLs?

In my Layout.cshtml file I have the following line:
<link rel="stylesheet" href="~/Content/bootstrap.css" />
My assumption was that since I did not include Url.Content() call, it would actually just render what I wrote but not expand the virtual URL automatically. This assumption is wrong - the generated HTML does include the correct path to the file, not the virtual path I entered.
If I wrap the <link> in <script>
<script type="text/html">
<link rel="stylesheet" href="~/Content/bootstrap.css" />
</script>
then the URL is not expanded.
Which part of ASP.NET MVC does this automatic parsing of HTML and is there a way to control it?
This was a new feature included in Razor2 and ASP.NET MVC 4 and was designed to make life easier by not having to use Url.Content everywhere.
http://www.davidhayden.me/blog/asp.net-mvc-4-the-new-tilde-slash-feature-in-razor-2
The feature only works inside standard HTML attributes and that's why you don't get it inside your <script> tag or anywhere else.
You could use a simple output write to work around this:
<link rel="stylesheet" href="#("~/Content/bootstrap.css")" />

Implementing CSS as a variable in the site.master page in ASP.NET MVC3

I am implementing a web application using ASP.NET MVC3. In the application I want to add the option of switching between CSS files (for themes) based on clicking some links. I did some research and I came up with the following:
To pass data to site.master, the good solution is to create an abstract controller and inherit that into the other controllers that have site.master as their template, according to this article: Passing Data to View Master Pages
I can pass a viewbag message to the link which controls the css file URL to set the current css file to use, based on the code that I am seeing being passed to scripts at the top of the site.master page:
script src="<%: Url.Content("~/Scripts/jquery-1.5.1.min.js") %>" type="text/javascript"
So I created an abstract controller, ApplicationController, with the following method:
public ApplicationController()
{ViewBag.NewMessage = "../../Content/Site2.css";}
And in the site.master, I included this link:
<link href="<%: (string) ViewBag.NewMessage %>" rel="stylesheet" type="text/css" />
However, that doesn't seem to be working, as it is being interpreted as:
<link href="<%: (string) ViewBag.NewMessage %>" rel="stylesheet" type="text/css" />
And only when I remove the quotation marks:
<link href=<%: (string) ViewBag.NewMessage %> rel="stylesheet" type="text/css" />
is it being interpreted correctly (and the webpage gets rendered with the correct css), except that it violates html standards:
<link href=../../Content/Site2.css rel="stylesheet" type="text/css" />
Any suggestion about how to go tackling this problem or is there a more elegant solution I'm missing? I was going to proceed and implement variables in the ApplicationController that get selected based on links the user clicks at the top of the page, to switch between css styles.
Thank you!
Check to make sure your <head> tag does not have runat="server".
After making this change, be sure to check your script and css tags. This change can break the paths if you use the ~/ to ref app root. To help with this, use the Url.Content(...) helper.

Cannot call external javascript function from SITE.MASTER

I have a Site.Master in my ASP.NET project which defines a HEAD section as follows
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title id="MasterTitle">MyApp</title>
<link rel="icon" href="Content/icon.ico" type="image/x-icon" />
<link href="Content/mycss.css" rel="stylesheet" type="text/css" />
<script src="Content/mycode.js" type="text/javascript"></script>
</head>
In the mycode.js file, I have a function called GetSels();
function GetSels()
{
//do stuff
}
If the GetSels function is defined in Site.Master, GetSels is callable. If it's in mycode.js, it's not.
Every code example I've seen seems to say this should work.
What am I doing wrong?
This should really work perfectly as I have done this multiple times myself.
Check that the code in your external javascript file runs correctly on page load, this is just to make sure that it is indeed being loaded correctly into your document. For example set an alert("It's loaded"); in your external .js file.
have you checked that your reference to mycode.js is correct? if your using a relative path try "~/Content/mycode.js" in your reference.

Resources