How should I template my site in Eclipse
I'm开发者_StackOverflow社区 using Eclipse for the first time, wondering which way to go about templating it? I know a little about tiles and jsp's, zero about databases.
the site:
- static header, nav, sidebar, and footer
- a few different content jsp's
- main question is --> I have one content section with one layout but 100's of varying jsp's...how should I go about this?
thanks
I'm not sure how Struts and databases are related to this, but basically, <jsp:include>
is the best what you can get in JSP.
Basic kickoff example of /WEB-INF/template.jsp
<!DOCTYPE html>
<html lang="en">
<head>
<title>${title}</title>
<link rel="stylesheet" href="style.css" />
<script src="script.js"></script>
</head>
<body>
<div id="header">
header
</div>
<div id="menu">
menu
</div>
<div id="content">
<jsp:include page="/WEB-INF/${view}.jsp" />
</div>
<div id="footer">
footer
</div>
</body>
</html>
And the controller Servlet:
@WebServlet(urlPatterns={"/pages/*"})
public class Controller extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String view = request.getPathInfo().substring(1);
String title = titles.get(view); // Imagine that it's a map or something.
request.setAttribute("view", view);
request.setAttribute("title", title);
request.getRequestDispatcher("/WEB-INF/template.jsp").forward(request, response);
}
}
Invoke it by
http://localhost:8080/contextname/pages/foo
and provide a /WEB-INF/foo.jsp
file which should represent the content. E.g.
/WEB-INF/foo.jsp
<h1>This is Foo!</h1>
<p>Lorem ipsum</p>
精彩评论