November 21, 2014
Hot Topics:

Applying MVC to Web-Based Applications with Generic Views

  • December 2, 2003
  • By Vlad Kofman
  • Send Email »
  • More Articles »

Java Bean Model Layer

In my Web MVC project, the model layer consists of helper beans that hold data. They only have instance fields, getter, and methods. They are located in the 'beans' package and are populated by Action classes. When the controller executes an action method, the action may populate a bean, or do some other operation. In an enterprise project, the action may do a lot more: Go to the RDBMS, mainframe, or any other data source to get data to populate bean, send data to data source, update data, do security checks, and other tasks. Actions are really Controller helper classes, but they also belong to the Model layer because they control the model data state and respond to user requests.

Conclusion

As you can see, MVC is ideally suited for Web-based application development. Once the controller is done, programmers can create many views and corresponding models (actions + beans) as the application use-case and spec requires. And, because the controller can be simply reused in a new application, development time shrinks and can be spent on writing business logic and functionalities represented in the model. Views can be created as a prototype, done entirely in HTML first and then converted to JPS or replaced by another technology altogether. If you have any questions or comments about this or any other of my articles, please e-mail me at vlad_kofman@yahoo.com.

Code Listings

Listing 1

public void doPost(HttpServletRequest req,HttpServletResponse res)
  throws ServletException, IOException{
    res.setContentType("text/html");
    String action = (String) req.getParameter("ACTIONKEY");
    String redirect = (String) req.getParameter("REDIRECTKEY");
    ActionInterface actionInterface = null;
    HttpSession session = req.getSession();

try {
  String classNameStr = action.substring(0, action.indexOf("."));
  String methodName = action.substring(action.indexOf(".") + 1);
  // if action exists - get it, 
  actionInterface = (ActionInterface) actions.get(classNameStr);
  // if null - instantiate it
  if
    (actionInterface == null) {
    className = Class.forName("actions." + classNameStr);
    actionInterface = (ActionInterface) className.newInstance();
    actions.put(classNameStr, actionInterface);
    }

    method = actionInterface.getClass().getMethod(methodName,
                                                  new
    Class[] { o.getClass(), o.getClass()});
    method.invoke(actionInterface, new
    Object[] { req, res });

    } catch (Exception e) {
    session.setAttribute(
    "msg",
    "Problem in MainController Servlet doPost\n" + e);
    System.out.println(
    "Problem in MainController Servlet doPost\n" + e);
    } finally {
    this
    .getServletContext()
    .getRequestDispatcher("/jsp/" + redirect + ".jsp")
    .include(req, res);
    }
  }

Listing 2

public synchronized void viewByZip(Object reqO, Object resO) {

  HttpServletRequest req = (HttpServletRequest) reqO;
  HttpServletResponse res = (HttpServletResponse) resO;
  String zip = (String) req.getParameter("ZIP");
  String data = (String) WeatherData.getData(zip);

  if (data == null)
  data = "Sorry; no weather data is available for zip:" + zip;
  // fill new bean
  WeatherBean wb = new WeatherBean();
  wb.setZip(zip);
  wb.setData(data);
  // put bean in to request user's object
  req.setAttribute("weather", wb);
  }

Listing 3

<BODY>
<% beans.WeatherBean wb =
  (beans.WeatherBean)request.getAttribute("weather"); %>
<P>Weather information for <%=wb.getZip() %></P>
  <%=wb.getData() %>
<P>
<% if (session.getAttribute("msg") == null)
      { session.setAttribute("msg", "& "); } %>
<%=session.getAttribute("msg") %>
<% session.setAttribute("msg", "& "); %>
</P>
  <backTags:BackLink text="<- Back" />
</BODY>

Download source code here.

About the Author

Vlad Kofman is a System Architect currently working on projects under government defense contracts. He also has been involved with enterprise-level projects for major Wall Street firms and the U.S. government. His main interests are object-oriented programming methodologies and design patterns.

References

Java Servlet Technology by Stephanie Bodoff
http://java.sun.com/webservices/docs/1.0/tutorial/doc/Servlets.html

Servlets and JavaServer Pages (JSP) 1.0: A Tutorial
http://www.apl.jhu.edu/~hall/java/Servlet-Tutorial/

Sun Reflection API by Dale Green
http://java.sun.com/docs/books/tutorial/reflect/

WebSphere Studio Application Developer Version 5 Programming Guide
By Ueli Wahli, Ian Brown, Fabio Ferraz, Maik Schumacher, and Henrik Sjostrand
IBM Redbook SG24-6957-00, May 9, 2003
http://www.redbooks.ibm.com/

Core Java 2, Volume II: Advanced Features (5th Edition)
by Cay Horstmann, Gary Cornell
Publisher: Prentice Hall; 5th edition (December 10, 2001)





Page 2 of 2



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Rocket Fuel