1 16 17 package org.springframework.web.servlet.view.velocity; 18 19 import org.springframework.web.servlet.view.AbstractTemplateViewResolver; 20 import org.springframework.web.servlet.view.AbstractUrlBasedView; 21 22 45 public class VelocityViewResolver extends AbstractTemplateViewResolver { 46 47 private String velocityFormatterAttribute; 48 49 private String dateToolAttribute; 50 51 private String numberToolAttribute; 52 53 private String toolboxConfigLocation; 54 55 56 61 public VelocityViewResolver() { 62 setViewClass(requiredViewClass()); 63 } 64 65 69 protected Class requiredViewClass() { 70 return VelocityView.class; 71 } 72 73 80 public void setVelocityFormatterAttribute(String velocityFormatterAttribute) { 81 this.velocityFormatterAttribute = velocityFormatterAttribute; 82 } 83 84 90 public void setDateToolAttribute(String dateToolAttribute) { 91 this.dateToolAttribute = dateToolAttribute; 92 } 93 94 100 public void setNumberToolAttribute(String numberToolAttribute) { 101 this.numberToolAttribute = numberToolAttribute; 102 } 103 104 117 public void setToolboxConfigLocation(String toolboxConfigLocation) { 118 this.toolboxConfigLocation = toolboxConfigLocation; 119 } 120 121 122 protected void initApplicationContext() { 123 super.initApplicationContext(); 124 125 if (this.toolboxConfigLocation != null) { 126 if (VelocityView.class.equals(getViewClass())) { 127 logger.info("Using VelocityToolboxView instead of default VelocityView " + 128 "due to specified toolboxConfigLocation"); 129 setViewClass(VelocityToolboxView.class); 130 } 131 else if (!VelocityToolboxView.class.isAssignableFrom(getViewClass())) { 132 throw new IllegalArgumentException ( 133 "Given view class [" + getViewClass().getName() + 134 "] is not of type [" + VelocityToolboxView.class.getName() + 135 "], which it needs to be in case of a specified toolboxConfigLocation"); 136 } 137 } 138 } 139 140 141 protected AbstractUrlBasedView buildView(String viewName) throws Exception { 142 VelocityView view = (VelocityView) super.buildView(viewName); 143 view.setVelocityFormatterAttribute(this.velocityFormatterAttribute); 144 view.setDateToolAttribute(this.dateToolAttribute); 145 view.setNumberToolAttribute(this.numberToolAttribute); 146 if (this.toolboxConfigLocation != null) { 147 ((VelocityToolboxView) view).setToolboxConfigLocation(this.toolboxConfigLocation); 148 } 149 return view; 150 } 151 152 } 153 | Popular Tags |