views:

25

answers:

0

Below is an action class - I guess similar to what STRUTS uses - which is called from my servlet controller (btw, I am not using STRUTS). I am hung up on a few things.

1) Should I be passing a CommitmentItem BO/DTO/Javabean (whatever it is called) to my CommitmentItemForm instead of the request object inorder to stay MVC compliant?? ciForm.updateCommitment does form validation and if everything is OK it calls my CommitmentItem DAO.

2) Why do I need to pass in the CommitmentListDAO to my CommitmentItemForm?? Can't I just instantiate a CommitmentListDAO in my CommitmentItemForm class??

3) Should the call to my DAO that does the CRUD Update be called from the below action class or is it OK to be called from in the CommitmentItemForm class??

I'm still trying to get my head around Java OO and applying it to Java EE MVC.

package com.foo.me.commitment.controller.action;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.foo.me.commitment.controller.ControllerAction;
import com.foo.me.commitment.controller.form.CommitmentItemForm;
import com.foo.me.commitment.data.CommitmentListDAO;
import com.foo.me.commitment.model.CommitmentItem;

public class UpdateCommitmentItemAction implements ControllerAction {

    public UpdateCommitmentItemAction() {}

    public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            CommitmentListDAO clDAO = new CommitmentListDAO();    
            CommitmentItemForm ciForm = new CommitmentItemForm(clDAO); // <----------------HERE
            CommitmentItem commitmentItem = ciForm.updateCommitment(request); // <----------------HERE

            if (ciForm.isSucces()) {
                    System.out.println("Success");
                    response.sendRedirect("CommitmentList.jsp?message=Update was successfully updated!");
            }
            else {
                    System.out.println("Not Successful");
                    request.setAttribute("form", ciForm);
                    request.setAttribute("commitmentItem", commitmentItem);
                    request.setAttribute("pageName", "Update");
                    RequestDispatcher view = request.getRequestDispatcher("views/commitmentEdit_v.jsp);
                    view.forward(request, response);
            }        
    }

}