[SOLVED] [Java/JSP] I need an Action controller example :(
Posted: Fri Jul 06, 2007 10:04 am
I'm struggling to find a simple example of how to do the following with Servlets/JSP:
1. Create a simple composite view (JSP file) used on each page:
2. Create a FrontController which reads "module" and "action" arguments from the request then loads an action controller for that module and runs the relevant action in it.
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):
I've been searching for ages for decent tutorials on how to handle action controllers in Java, and also how to deal with a composite view but I just keep coming back to these articles which I don't really understand because my experience with servlets amounts to just a few days.
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?
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?