01: /*
02: @COPYRIGHT@
03: */
04: package demo.events.web;
05:
06: import java.util.HashMap;
07: import java.util.Map;
08:
09: import javax.servlet.http.HttpServletRequest;
10: import javax.servlet.http.HttpServletResponse;
11:
12: import org.springframework.context.ApplicationContext;
13: import org.springframework.web.bind.RequestUtils;
14: import org.springframework.web.servlet.ModelAndView;
15: import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
16:
17: import demo.events.EventProcessor;
18: import demo.events.MessageEvent;
19:
20: /**
21: * Web controller for the Events sample application; provides the list of current messages and publishes new messages as
22: * events to the {@link ApplicationContext}, which is distributed by Terracotta for Spring.
23: *
24: * @author Eugene Kuleshov
25: */
26: public class CounterFormController extends MultiActionController {
27:
28: private transient EventProcessor eventProcessor;
29:
30: public void setEventProcessor(EventProcessor eventProcessor) {
31: this .eventProcessor = eventProcessor;
32: }
33:
34: /**
35: * Controller method to handle refresh action
36: */
37: public ModelAndView handleRefresh(HttpServletRequest request,
38: HttpServletResponse response) throws Exception {
39: Map model = new HashMap();
40: model.put("events", eventProcessor.getEvents());
41: return new ModelAndView("index", model);
42: }
43:
44: /**
45: * Controller method to handle message submission action
46: */
47: public ModelAndView handleMessage(HttpServletRequest request,
48: HttpServletResponse response) throws Exception {
49: String message = RequestUtils.getRequiredStringParameter(
50: request, "message");
51: String sender = RequestUtils.getRequiredStringParameter(
52: request, "sender");
53:
54: ApplicationContext ctx = getApplicationContext();
55: ctx.publishEvent(new MessageEvent(sender, message));
56:
57: return new ModelAndView("redirect:index.jsp", null);
58: }
59:
60: }
|