I’m thinking of implementing Front Controller in my J2EE application. Could you please suggest the same with few links (with source code examples) & any standards to follow?
Best regards
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
To start, create a
Servletwhich listens on a certainurl-pattern, e.g./pages/*. Implement theservice()method to lookup the action associated with the request method (GET,POST, etc) and pathinfo (the URL part after the servlet’surl-pattern).Basic example:
The
Actioninterface should represent an unit of work. You can implement it to do the necessary business logic:The
ActionFactoryshould maintain the classes implementingActionin sort ofMap<String, Action>where theStringkey represents less or more a combination of the request method and pathinfo. You could then get anActionas follows:The
Viewshould represent the request scoped context which theActioncan work with. In thenavigate()you could forward the request to a JSP for display:That should get you started (note that I left all obvious checks such as null pointers away to make the examples less cluttered, that’s up to you now).
There is however more to take account with in the whole story, such as validation, conversion, event handling, input value mappings, localization, dependency injection, etcetera. That’s all with all quite a work. The more decent MVC frameworks takes most of this all into account, such as Sun JSF, Apache Struts, Spring MVC, Stripes, etcetera. If you have never done any of them, then I strongly recommend to do so before homegrowing one, otherwise you would end up with a waste of time.