What is the JSP equivalent of ASP.NET MVC's partial views?
I'd like to separate some complicated view logic out of a page into a separate page that only handles that logic. How can I render that page as part of my page?
There isn't. JSP is not a fullworthy equivalent of ASP.NET MVC. It's more an equivalent to classic ASP. The Java equivalent of ASP.NET MVC is JSF 2.0 on Facelets.
However, your requirement sounds more like as if you need a simple include page. In JSP you can use the <jsp:include> for this. But it offers nothing more with regard to templating (Facelets is superior in this) and also nothing with regard to component based MVC (there you have JSF for).
Basic example:
main.jsp
<!DOCTYPE html>
<html lang="en">
<head>
<title>Title</title>
</head>
<body>
<h1>Parent page</h1>
<jsp:include page="include.jsp" />
</body>
</html>
include.jsp
<h2>Include page</h2>
Generated HTML result:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Title</title>
</head>
<body>
<h1>Parent page</h1>
<h2>Include page</h2>
</body>
</html>
See also:
What is the Java alternative to ASP.NET/PHP?
Analogues of Java and .NET technologies/frameworks
What is the difference between JSF, JSP and Servlet?
Related
I have a Grails 2.4.x app where ~80% of the pages use a simple.gsp layout, and the other pages are all stragglers that don't use any layout/templating at all. But they can't use simple.gsp because its contents don't apply to them.
I have a need to add a header nav to all of these pages (100%) and would like an elegant solution. Ideally, I could create a new layout, say, awesome-header.gsp that contains the header nav. Then:
For any pages (again, ~20%) that don't use the simple.gsp layout, I would just have them use awesome-header.gsp directly; but then...
I would just somehow reference/import/extend simple.gsp to (somehow) use awesome-header.gsp; which now allows the other ~80% pages to use the new header nav
Let's pretend that this is simple.gsp:
<!DOCTYPE html>
<html>
<head>
<title>
<g:layoutTitle default="Some App" />
</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<!-- Lots of stuff -->
<g:layoutHead />
<!-- Lots of stuff -->
</head>
<body>
<!-- Lots of stuff -->
<div id="page-content">
<g:layoutBody />
</div>
<!-- Lots of stuff -->
</body>
</html>
And let's pretend that this is awesome-header.gsp:
<%# page contentType="text/html;charset=UTF-8" %>
<html>
<head>
<title></title>
</head>
<body>
<script id="awesome-header-bootstrap" src="/awesome-header/awesome-header-bootstrap-1.0.js"><script>
<g:layoutBody />
</body>
</html>
Pretty barebones. All I need this awesome-header.gsp layout to do is include a JS right at the top of the <body> element. For the purpose of this question, this JS script is "magic" and fetches the header nav magically for me.
How can reference/import/extend simple.gsp to use awesome-header.gsp?
I don't want the awesome-header.gsp to override any title or header content (either defined inside simple.gsp or in any of the straggler pages)
Any ideas how I could accomplish this setup?
If I well understand, you want a hierarchy between simple.gsp and awesome-header.gsp. So you may look at this link to help you to do that.
An other solution, maybe easier because there isn't a lot of modifications to do, is to use templates:
Put all your HTML / JS code related to your awesome-header inside a template (let say _awesome-header.gsp, the '_' is important !)
Simply put that line inside your 'simple' layout and inside all others pages which are not connected to your 'simple' layout: <g:render template='awesome-header'/>
I watched the facelet template example and I could see the official tutorial, too, in both cases I can see the tuts make using <ui:composition template="/layout.xhtml"> with template support only; But is there a way not to include template if I want some a very simple facelet for example?
I tried to ignore the template attribute but then I have java.lang.StackOverflowError being thrown :( So the jsf 2.0 makes me to have templates to be included anyway...
So my question is... is there a way writing facelets without templates references?
Thanks
Yes, certainly. Just use the same XHTML composition as the master template /layout.xhtml itself, but then without any <ui:insert>. Instead, just put the desired content straight in there.
For example, /page.xhtml:
<!DOCTYPE html>
<html lang="en"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head>
<title>Some title</title>
</h:head>
<h:body>
<h1>Some heading</h1>
<p>Plain Facelets page without template!</p>
</h:body>
</html>
Open this directly as /page.xhtml in browser.
Can someone tell me if there is a recommended way to add Meta information to MVC3 pages. The kind of information that I would like to add is title, description, keywords.
Thanks,
Gemma
I'd use ViewBag for the title and RenderSection for the rest of the head content. This goes in the Master Layout file (_Layout.cshtml):
<head>
<title>#ViewBag.Title</title>
#RenderSection("head", false);
</head>
In your individual views, you will add:
#{
ViewBag.Title = "My Page Title";
}
#section head {
<meta name="description" content="best site ever">
}
EDIT:
Note that the #section head {...} block is optional. You will not get a compilation error if you omit this block. On views where you want metadata you'd supply it.
I'd add it as a seperate view - that way you can call:
#{Html.RenderAction("Head", "Header");}
from within your various layouts and have the same correct header data rendered.
hth
Usually, in master page, we put a ContentPlaceHolder control, call it TitleContent like this:
<head>
<title>
<asp:ContentPlaceHolder ID="TitleContent" runat="server" />
</title>
</head>
And in children pages:
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Some Title
</asp:Content>
The same thing can be done for Meta Tags, or you could make a full ContentPlaceHolder for HeadContent and fill it with (title/meta...) in each page individually.
You have to put metatags inside the HEAD tag of HTML. For MVC application it depends there you have HEAD. It could be either page (*.cshmtl) of layout (Layout.cshtml). It is quite better to place into layout, since meta info is shared throught the rest of pages.
// *.cshtml
<!DOCTYPE html>
<html>
<head>
<title>#ViewBag.Title</title>
<meta ... />
</html>
</head>
I am new to JSF and Spring. I am trying to use Spring Security with JSF. I have designed an XHTML page as below:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:spring="http://www.springframework.org/tags"
xmlns:form="http://www.springframework.org/tags/form">
<head>
<title>JSF Test</title>
</head>
<body>
<f:view>
<p>This page should be authenticated!</p>
Logout
<div>Test: <spring:url value="/j_spring_security_logout" htmlEscape="true" /></div>
</f:view>
</body>
</html>
I am trying to add a logout link using the spring:url tag. However, firstly this gives an error that href attribute cannot contain <. To troubleshoot this I removed the a tag and used the div to test whether I am able to get the desired value from the spring:url tag. I found that the spring:url tag is not being parsed and appears as it is in the source of the generated page.
Instead of using XHTML page if I use JSP page with taglibs instead of xmlns, everything works fine. I am not able to understand why it is not working with XHTML files.
My Faces Servlet is mapped to .jsf and springSecurityFilterChain is mapped to /* (without the space in between). I tried mapping springSecurityFilterChain to *.jsf and that too doesn't help.
Facelets is a XML based view technology. Nesting tags as an attribute of another tag is invalid XML. All <spring:url> effectively does is prepending the context path, you could also do it yourself:
Logout
The HTML escaping is not relevant since you are not passing any parameters through it.
I've created a simple MVC6 site from the empty site template.
Here's my _Layout:
<!DOCTYPE html>
<body>
#RenderBody()
</body>
And my Index page:
<div>
<p>Hello World From a View!</p>
</div>
The Home controller:
public IActionResult Index()
{
return View();
}
Now here's the problem: Templating via _Layout works as expected if the #RenderBody() isn't enclosed in any tags, example:
<!DOCTYPE html>
#RenderBody()
It also works if it's wrapped in a div, but when it's wrapped in <body tags the page just doesn't load and is in a state similar to an infinite refresh (it just keeps loading the page indefinitely).
Can anyone shed some light on this? Maybe I'm missing some dependency?
EDIT:
A bit of extra info I've forgot to include: It's running on IIS10.
It seems to be a bug with IIS10 and ASP.NET vNext.
When I run it directly through dnx it works flawlessly. But when I run it through IIS10 it just hangs.