13

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?

C. Ross
  • 31,137
  • 42
  • 147
  • 238

1 Answers1

20

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:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Can that included jsp be dynamic, and use the context the same way the primary jsp can? – C. Ross Nov 26 '10 at 19:26
  • 2
    You can just use EL in `` like ``. I am not sure what you mean with "context" here, but it shares the same request, session and application scope, if that's what you mean. Only the page scope is different and can be bridged by nesting a `` in the ``. It'll be available by `${param.name}` in EL of included JSP. – BalusC Nov 26 '10 at 19:42