Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Good JSP code structure for header and footer

Tags:

java

html

jsp

I currently have following code structure of my JSP pages:

MyPage.jsp

<jsp:include page="header.jsp"/>
Specific page content
<jsp:include page="footer.jsp"/>

However, this means that the header and footer code do not have a correct HTML structure. For example, this is simplified header and footer code:

header.jsp

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>${param.pageTitle}</title>
    </head>
    <body>
        <div class="container" style="margin-top: 80px;">

footer.jsp

        </div>
    </body>
</html>

As a result, IDE gives a warning about "missing start tag" / "missing end tag". I don't feel good about disabling the warning entirely since it may find legitimate issues with HTML structure.

Is there a cleaner way to structure the JSP code so that I can still reuse header and footer code in some good way?

like image 765
Petr Dvořák Avatar asked May 28 '16 13:05

Petr Dvořák


People also ask

What is the correct html structure for header and footer?

However, this means that the header and footer code do not have a correct HTML structure. For example, this is simplified header and footer code: <!DOCTYPE html> <html lang="en"> <head> <title>$ {param.pageTitle}</title> </head> <body> <div class="container" style="margin-top: 80px;">

How to include header and footer in main JSP page?

So the correct approach should be to include two separate jsp pages as header and footer in your main jsp page without distorting it. That is your MyPage.jsp should be :- This is the proper structure of a page. Won't that lead to what he was trying to avoid?

What are the different types of code in JSP?

A JSP page mainly comprises the following types of code. HTML code is used for displaying the HTML output like text field, check box, option menu where user can give requested data to the web application and also for generating other static content of the web page. HTML codes can be embedded easily in the JSP page.

Which attributes does the <footer> tag support in HTML?

The <footer> tag also supports the Global Attributes in HTML. The <footer> tag also supports the Event Attributes in HTML. Most browsers will display the <footer> element with the following default values:


1 Answers

Might be this is helpful. Please have a look.

\home.jsp               // Base Page
\parts\meta.jsp         // To hold page meta information. Useful when working with SEO
\parts\header.jsp       // Resources CSS, images, 
\parts\footer.jsp       // Resources JS

Remember

Use <%@include> because it is static include and <jsp:include> is dynamic include. When you use <jsp:include> the file will be included at runtime. When you use <%@include> file will be included at compile time.

So here is code snippet.

1) home.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<%@ include file="parts/meta.jsp" %>  
<title>Home Page</title>
<%@ include file="parts/header.jsp" %>  
</head>
<body>
    <div class="view">
        <div class="pages">
            <jsp:include page="parts/page-body.jsp"></jsp:include>
        </div>
    </div>
    <%@ include file="parts/footer.jsp" %>  
</body>
</html>

2) meta.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<meta charset="utf-8">
<meta name="viewport"
    content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no, minimal-ui">
<meta name="mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">

3) header.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<link href="http://fonts.googleapis.com/css?family=Roboto:400,300,500,700" rel="stylesheet" type="text/css">
<link rel="stylesheet" href="css/my-page.css">
<link rel="stylesheet" href="css/my-icon.css">
<link rel="icon" href="img/icon.png">

4) footer.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<script type="text/javascript" src="js/my-app.js"></script>
<script type="text/javascript" src="js/my-app-service.js"></script>

Thanks :)

like image 185
Suraj Shingade Avatar answered Nov 08 '22 10:11

Suraj Shingade