sitemesh + struts2 How to include external files in standard.jsp - struts2

My company has 20+ web applications. They all use Struts2 and Sitemesh to decorate the pages.
Every application has its own version of header.html and footer.html which are included in standard.jsp (using <%# include file=filename%>).
For look and feel consistency across applications I want to have the header and footer files outside of the web application.
However I can't figure out how to do that!
My decorator.xml looks like this
<decorators defaultdir="/layout">
<decorator name="standard-layout" page="standard.jsp">
<pattern>/*</pattern>
</decorator>
</decorators>
My include directive <%# include file="/common/header.html%>, gets prefix with the defaultdir value, like this /layout/common/header.html, and I get a file not found error.
The same thing happens if I use an absolute path.
Also, the same thing happens if I change decorators.xml like this.
<decorators>
<decorator name="standard-layout" page="/layout/standard.jsp">
<pattern>/*</pattern>
</decorator>
</decorators>
Help will be appreciated!
Update: I was able to solve this using JSTL
<c:import var="commomHeader" url="/resources/html/common_header.html"/>
${commomHeader}

Related

Difference Between Script Rendering Code in Razor

When I create an MVC project that uses Razor, the following lines are generated:
#Scripts.Render("~/bundles/jquery")
#RenderSection("scripts", false)
What is the difference between these lines ? Is it the case that #Scripts.Render generates a <scripts> section and then #RenderSection("scripts", false) renders it ?
The first one renders out a bundle, which is a group of related Javascript files. For instance, you might want to bundle jQuery and jQuery UI together. Bundles also get the benefit of bundling and minification when a solution is compiled in release mode. ref: http://www.asp.net/mvc/tutorials/mvc-4/bundling-and-minification
#RenderSection indicates that a page that uses the layout in question can inject markup in a particular spot in the layout. Sections are in effect a placeholder (and work much like the ContentPlaceHolder server control from web forms if you are familiar with that). That reference is probably right before the closing body tag, where it is believed by some to be the best spot to put scripts. You could have a section called scripts, or head, or footer, it is completely arbitrary and sections don't necessarily have anything to do with scripts at all. ref: http://weblogs.asp.net/scottgu/asp-net-mvc-3-layouts-and-sections-with-razor
These are completely different things.
The #Scripts.Render("~/bundles/jquery") is rendering a group of scripts for you, creating all the <script> tags. in the BundleConfig.cs file you are able to create bundles of scripts and css files.
One of its advantages allow you to group multiple files that are commonly used together. Instead of including each of these files explicitly you can include all of them using explicit Scripts.Render(groupName).
You can read more about Bundling and Minification here.
The #RenderSection("scripts", false) is about rendering a section in the view.
A section allow you to specify a region of content within a layout. It expects one parameter which is the name of the section. If you don’t provide that, an exception will be thrown.
Here is a good article explaining about Layouts, RenderBody, RenderSection and RenderPage in ASP.NET MVC.
The first parameter to the “RenderSection()” helper method specifies
the name of the section we want to render at that location in the
layout template. The second parameter is optional, and allows us to
define whether the section we are rendering is required or not. If a
section is “required”, then Razor will throw an error at runtime if
that section is not implemented within a view template that is based
on the layout file (which can make it easier to track down content
errors). If a section is not required, then its presence within a
view template is optional, and the above RenderSection() code will
render nothing at runtime if it isn’t defined.

Asp MVC 3: Dynamically resolve relative resources in views

I have a javascript application that runs in a view (index.cshtml).
Problem:
The problem is all relative paths are relative to the current url, which would be ok in a simple html webapp but not in asp mvc. The js-app shouldn't have to bother whether it's served in a normal html file or via a asp mvc page.
I.e. http://www.domain.com/<controller>/<action>/ contains a script test.js. This script loads an external xml file searching relative to it ie. "data/data.xml". The resulting url reads http://www.domain.com/<controller>/<action>/data/data.xml. This isn't found.
Question:
Is there a way to route static files (images,..., maybe even js files) to the content folder like "~/Content/controller/action/<pathToFile>/"?
Any help appreciated!
Lg
warappa
PS: I know about Url.Content() but that doesn't fit here.
The solution doesn't require mapping - just a simple html tag in the header:
<base href="#(Request.Url.GetComponents(UriComponents.SchemeAndServer, UriFormat.Unescaped) +
Url.Content("~/content/controller/action/"))" />
Lg
warappa
EDIT
Some browsers need an absolute url - sample updated.
In you can use absolute URL addresses to access you static resources:
$('img').attr('src', '/Content/Pictures/picture1.png');
or
<script src="/Scripts/script.js"></script>
This way you will allways get the same resources relative to the page base address, no matter if you load the script in a /{Controller}/{Action}/{View}, {Area}/{Controller}/{Action}/{View}, a custom route or even in a static script html page.
Or perhaps what you're looking for is the use of css files, since CSS's url('<path>') resolves the addresses relative to the CSS file's location. You would just need to import the one CSS file that had all the resource (image?) file paths. Then the scripts could reference the distinct class names, thus not being location aware at all. This is what libraries like jQuery UI do. But again this would require a fixed folder structure relative to the CSS document.

Grails layouts not applied on a 404 UrlMapping

In a Grails 1.3.1 app, I want 404 errors to cause the render the main index action of the "list" controller. This controller uses a conventional layout file in the layouts directory (views/layouts/list.gsp), which then uses the included snippet for that action (views/list/index.gsp).
When this action is accessed normally (http://localhost/list/index), then both the layout file and the snippet are applied, and it looks correct. However, when accessed via a 404 rule in the UrlMapping class -- "404"(controller: "list", action: "index") -- the layout file isn't used, and only the snippet is displayed.
Does anyone know why this happens, and if there's any way to get the conventional layout to display for a 404 (or other) error mapping?
I know a while back this was a bug in the version of SiteMesh Grails was using. There is a work around where you can wrap your error pages in:
<g:applyLayout name="main">
</g:applyLayout>
Instead of using the usual:
<meta name="layout" content="main" />
Another thing to look for is the sitemesh.xml configuration file. Sitemesh is turned on/off depending on the content-type of the response, and this file declares the content-types values that site-mesh will process. In my case, an entry for text/html;charset=UTF-8 was not enough for responses with type text/html to be processed.
Are you sure the layout isn't applied? I'm using Grails 1.3.2 and I thought the layout wasn't applied, however it actually was the lack of a model and security tags in the layout after a 404.
If your layout content is derived from such things being available, try a "Hello world" first to see if it shows up.

Understanding how VS parses CSS in .ascx docs

I have a number of documents (in an MVC app) that exist solely to be called by:
<% Html.RenderPartial("showLoginStatus"); %>
While editing the file in VS (08) the CSS class selectors used within this page all toss 'class not defined' warnings since the stylesheet isn't referenced in this file but in the 'parent'.
The page renders correctly when called - how do I clue VS into the fact that a definition exists?
thx
Looks like VS does not understand site relative paths, as described in this article.
So, to answer your question, either change the CSS URLs to be fully qualified, or live with the error...

Avoid "The class or CssClass value is not defined" Warnings in ASP.NET MVC ASCX Partial Views (UserControls)

I wondered how I can stop those annoying compiler warnings "The class or CssClass value is not defined" coming from my ASP.NET MVC partial views/ASCX user controls. The CSS is correctly defined, but the reference to the CSS file is obviously on the master page only. My partial views contain lots of CSS class references on div's etc. and so I get massive amounts of warnings which I shouldn't see.
How can I solve this?
Thank you !
Include this in your partial view:
<% if (false) { %>
<link rel="stylesheet" type="text/css" ...
<% } %>
This will make intellisense happy, and excludes the stylesheet when the page is rendered, so that it is not included twice.
One way is to turn HTML Syntax Checking off (Tools->Options->Text editor->HTML->Show Errors->In CSS).
I use the Firefox Tidy plug in, which gives better advice, IMHO.
This is not a flaw in ASP.Net MVC, and I don't think it's going to be it's going to be fixed in the next version. This is a 'limitation' (notice how i avoid the word flaw) in asp.net (not just mvc) that prevents it from accessing header information that's included in the master page. So you don't have access to javascript/css in the content pages/usercontrols.
The code provided by Robert Harvey is a hack solution that we've been using to overcome this.
It works by using the enclosing the include file in an if block that's always false. So the compiler sees the css file but the runtime doesn't.

Resources