01: package com.mockrunner.example.struts;
02:
03: import javax.servlet.http.HttpServletRequest;
04: import javax.servlet.http.HttpServletResponse;
05:
06: import org.apache.struts.action.Action;
07: import org.apache.struts.action.ActionForm;
08: import org.apache.struts.action.ActionForward;
09: import org.apache.struts.action.ActionMapping;
10: import org.apache.struts.util.MessageResources;
11:
12: /**
13: * This example action takes the name from the {@link GreetingsValidatorForm}
14: * and loads some messages from the current message resources.
15: * It then creates a short greetings message and sets it to
16: * the request. Demonstrates how to deal with messages resources and
17: * validators.
18: */
19: public class GreetingsAction extends Action {
20: public ActionForward execute(ActionMapping mapping,
21: ActionForm form, HttpServletRequest request,
22: HttpServletResponse response) throws Exception {
23: GreetingsValidatorForm greetForm = (GreetingsValidatorForm) form;
24: String name = greetForm.getName();
25: Integer counter = (Integer) request.getSession()
26: .getServletContext().getAttribute("counter");
27: counter = new Integer(counter.intValue() + 1);
28: request.getSession().getServletContext().setAttribute(
29: "counter", counter);
30: MessageResources resources = getResources(request);
31: String helloMessage = resources.getMessage("hello.name", name);
32: String visitorMessage = resources.getMessage("visitor.number",
33: counter);
34: request.setAttribute("greetings", helloMessage + " "
35: + visitorMessage);
36: return mapping.findForward("success");
37: }
38: }
|