1 25 package org.ofbiz.webapp.ftl; 26 27 import java.util.List ; 28 import javax.servlet.http.HttpServletRequest ; 29 30 import freemarker.core.Environment; 31 import freemarker.ext.beans.BeanModel; 32 import freemarker.template.SimpleScalar; 33 import freemarker.template.TemplateMethodModelEx; 34 import freemarker.template.TemplateModelException; 35 import freemarker.template.TemplateNumberModel; 36 import freemarker.template.TemplateScalarModel; 37 38 45 public class SetRequestAttributeMethod implements TemplateMethodModelEx { 46 47 public static final String module = SetRequestAttributeMethod.class.getName(); 48 49 52 public Object exec(List args) throws TemplateModelException { 53 if (args == null || args.size() != 2) 54 throw new TemplateModelException("Invalid number of arguements"); 55 if (!(args.get(0) instanceof TemplateScalarModel)) 56 throw new TemplateModelException("First argument not an instance of TemplateScalarModel"); 57 if (!(args.get(1) instanceof BeanModel) && !(args.get(1) instanceof TemplateNumberModel) && !(args.get(1) instanceof TemplateScalarModel)) 58 throw new TemplateModelException("Second argument not an instance of BeanModel nor TemplateNumberModel nor TemplateScalarModel"); 59 60 Environment env = Environment.getCurrentEnvironment(); 61 BeanModel req = (BeanModel)env.getVariable("request"); 62 HttpServletRequest request = (HttpServletRequest ) req.getWrappedObject(); 63 64 String name = ((TemplateScalarModel) args.get(0)).getAsString(); 65 Object value = null; 66 if (args.get(1) instanceof TemplateScalarModel) 67 value = ((TemplateScalarModel) args.get(1)).getAsString(); 68 if (args.get(1) instanceof TemplateNumberModel) 69 value = ((TemplateNumberModel) args.get(1)).getAsNumber(); 70 if (args.get(1) instanceof BeanModel) 71 value = ((BeanModel) args.get(1)).getWrappedObject(); 72 73 request.setAttribute(name, value); 74 return new SimpleScalar(""); 75 } 76 77 } 78 | Popular Tags |