1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
|
package ch03.hello;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.MessageResources;
import org.apache.commons.beanutils.PropertyUtils;
/**
* The <strong>Action</strong> class for our "Hello" application.<p>
* This is the "Controller" class in the Struts MVC architecture.
*
* @author Kevin Bedell
*/
public final class HelloAction extends Action {
/**
* Process the specified HTTP request, and create the corresponding HTTP
* response (or forward to another web component that will create it).
* Return an <code>ActionForward</code> instance describing where and how
* control should be forwarded, or <code>null</code> if the response has
* already been completed.
*
* @param mapping The ActionMapping used to select this instance
* @param actionForm The optional ActionForm bean for this request (if any)
* @param request The HTTP request we are processing
* @param response The HTTP response we are creating
*
* @exception Exception if business logic throws an exception
*/
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
/*
* This Action is executed either by calling
*
* /hello.do - when loading the initial page
* - or -
* /hello.do?action=getName - whenever we post the form
*
*/
// If this is first time, go straight to page
String action = request.getParameter("action");
if (action == null) {
return (mapping.findForward("SayHello"));
}
// These "messages" come from the ApplicationResources.properties file
MessageResources messages = getResources(request);
/*
* Validate the request parameters specified by the user
* Note: Basic field validation done in HelloForm.java
* Business logic validation done in HelloAction.java
*/
ActionErrors errors = new ActionErrors();
String person = (String)
PropertyUtils.getSimpleProperty(form, "person");
String badPerson = "Atilla the Hun";
if (person.equals(badPerson)) {
errors.add("person",
new ActionError("ch03.hello.dont.talk.to.atilla", badPerson ));
saveErrors(request, errors);
return (new ActionForward(mapping.getInput()));
}
/*
* Having received and validated the data submitted from the View,
* we now update the model
*/
HelloModel hm = new HelloModel();
hm.setPerson(person);
hm.saveToPersistentStore();
/*
* If there was a choice of View components that depended on the model
* (oe some other) status, we'd make the decisoin here as to which
* to display. In this case, there is only one View component.
*
* We pass data to the View components by setting them as attributes
* in the page, request, session or servlet context. In this case, the
* most appropriate scoping is the "request" context since the data
* will not be nedded after the View is generated.
*
* Constants.HELLO_KEY provides a key accessible by both the
* Controller component (i.e. this class) and the View component
* (i.e. the jsp file we forward to).
*/
request.setAttribute( Constants.HELLO_KEY, hm);
// Remove the Form Bean - don't need to carry values forward
request.removeAttribute(mapping.getAttribute());
// Forward control to the specified success URI
return (mapping.findForward("SayHello"));
}
} |
Partager