简体   繁体   中英

JSP equivalent of ASP.NET MVC's partial views?

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:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM