1 16 17 package org.springframework.web.servlet.view.tiles; 18 19 import java.io.File ; 20 import java.io.IOException ; 21 22 import javax.servlet.ServletContext ; 23 import javax.servlet.ServletException ; 24 import javax.servlet.http.HttpServletRequest ; 25 import javax.servlet.http.HttpServletResponse ; 26 27 import org.apache.struts.tiles.ComponentContext; 28 import org.apache.struts.tiles.ControllerSupport; 29 30 import org.springframework.beans.BeansException; 31 import org.springframework.context.ApplicationContext; 32 import org.springframework.context.support.MessageSourceAccessor; 33 import org.springframework.web.context.WebApplicationContext; 34 import org.springframework.web.servlet.support.RequestContextUtils; 35 import org.springframework.web.util.NestedServletException; 36 import org.springframework.web.util.WebUtils; 37 38 53 public abstract class ComponentControllerSupport extends ControllerSupport { 54 55 private WebApplicationContext webApplicationContext; 56 57 private MessageSourceAccessor messageSourceAccessor; 58 59 60 66 public final void perform( 67 ComponentContext componentContext, HttpServletRequest request, 68 HttpServletResponse response, ServletContext servletContext) 69 throws ServletException , IOException { 70 71 try { 72 execute(componentContext, request, response, servletContext); 73 } 74 catch (ServletException ex) { 75 throw ex; 76 } 77 catch (IOException ex) { 78 throw ex; 79 } 80 catch (Throwable ex) { 81 throw new NestedServletException("Execution of component controller failed", ex); 82 } 83 } 84 85 93 public final void execute( 94 ComponentContext componentContext, HttpServletRequest request, 95 HttpServletResponse response, ServletContext servletContext) 96 throws Exception { 97 98 synchronized (this) { 99 if (this.webApplicationContext == null) { 100 this.webApplicationContext = RequestContextUtils.getWebApplicationContext(request, servletContext); 101 this.messageSourceAccessor = new MessageSourceAccessor(this.webApplicationContext); 102 } 103 } 104 doPerform(componentContext, request, response); 105 } 106 107 108 114 protected void initApplicationContext() throws BeansException { 115 } 116 117 120 protected final ApplicationContext getApplicationContext() { 121 return this.webApplicationContext; 122 } 123 124 127 protected final WebApplicationContext getWebApplicationContext() { 128 return this.webApplicationContext; 129 } 130 131 135 protected final MessageSourceAccessor getMessageSourceAccessor() { 136 return this.messageSourceAccessor; 137 } 138 139 142 protected final ServletContext getServletContext() { 143 return this.webApplicationContext.getServletContext(); 144 } 145 146 151 protected final File getTempDir() { 152 return WebUtils.getTempDir(getServletContext()); 153 } 154 155 156 172 protected abstract void doPerform( 173 ComponentContext componentContext, HttpServletRequest request, HttpServletResponse response) 174 throws Exception ; 175 176 } 177 | Popular Tags |