1. Create a simple composite view (JSP file) used on each page:
Code: Select all
------------------------
HEADER
------------------------
{CONTENT}
------------------------
FOOTER
------------------------3. Front controller loads JSP for the current module along with the overall layout JSP (header+footer) the combines the two and displays it.
In Pseudo code I'm just thinking extremely basic along the lines of this extremely hackish messy code (which doesn't actually work):
Code: Select all
/**
* The FrontController which all web requests communicate to.
* The front controller delegates requests to individual action controllers.
*/
package org.w3style.controllers;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class FrontController extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
processRequest(req, resp);
}
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
processRequest(req, resp);
}
public void processRequest(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String view = null;
try {
String module = "Home"; //Would come from URL
String action = "Index"; //Would come from URL
//Get the class for the action controller
Class actionClass = Class.forName("org.w3style.controllers." + module + "Actions");
//Create a new instance of the action controller
ActionController ac = actionClass.newInstance();
//Execute the action in it
actionClass.getDeclaredMethod("execute" + action,
new Class["HttpServletRequest", "HttpServletResponse"]).invoke(ac, null);
//Construct a path to the view for this action
view = "/view/" + module + "/" + action + ".jsp";
} catch (Exception e) {
//dump some error report
}
//Store the path to the view so layout.jsp can include it
req.setAttribute("includeFile", view);
ServletContext app = getServletContext();
RequestDispatcher disp;
//Dispatch to main template file, includes the header + footer etc
disp = app.getRequestDispatcher("/view/_layout/layout.jsp");
disp.forward(req, resp);
}
}
http://java.sun.com/blueprints/corej2ee ... oller.html
http://java.sun.com/blueprints/patterns ... eView.html
http://java.sun.com/blueprints/guidelin ... tier5.html
Does anyone have any links to easier to follow tutorials which use code to demonstrate the principles rather than a whole load of "waffle" about advanced Java stuff?