1 16 17 package org.apache.velocity.tools.struts; 18 19 import java.util.Locale ; 20 import javax.servlet.http.HttpServletRequest ; 21 import javax.servlet.ServletContext ; 22 import org.apache.struts.util.MessageResources; 23 import org.apache.velocity.app.Velocity; 24 import org.apache.velocity.tools.view.context.ViewContext; 25 import org.apache.velocity.tools.view.tools.ViewTool; 26 27 34 public abstract class MessageResourcesTool implements ViewTool 35 { 36 37 protected ServletContext application; 38 protected HttpServletRequest request; 39 protected Locale locale; 40 protected MessageResources resources; 41 42 43 49 public void init(Object obj) 50 { 51 if (!(obj instanceof ViewContext)) 52 { 53 throw new IllegalArgumentException ("Tool can only be initialized with a ViewContext"); 54 } 55 56 ViewContext context = (ViewContext)obj; 57 this.request = context.getRequest(); 58 this.application = context.getServletContext(); 59 this.resources = 60 StrutsUtils.getMessageResources(request, application); 61 this.locale = 62 StrutsUtils.getLocale(request, request.getSession(false)); 63 } 64 65 66 71 protected MessageResources getResources(String bundle) 72 { 73 if (bundle == null) 74 { 75 if (resources == null) 76 { 77 Velocity.error("MessageResourcesTool: Message resources are not available."); 78 } 79 return resources; 80 } 81 82 MessageResources res = 83 StrutsUtils.getMessageResources(request, application, bundle); 84 if (res == null) 85 { 86 Velocity.error("MessageResourcesTool: MessageResources bundle '" 87 + bundle + "' is not available."); 88 } 89 return res; 90 } 91 92 } 93 | Popular Tags |