سؤال

عندما أقوم بتحديث الفول:

<bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
  <property name="cache" value="true"/>
  <property name="prefix" value=""/>
  <property name="suffix" value=".vm"/>
  <property name="toolboxConfigLocation" value="tools.xml" />
</bean>

مع Tools.xml Path لأدوات السرعة ، أحصل على:

Caused by: 
java.lang.ClassNotFoundException: org.apache.velocity.tools.view.ToolboxManager

لقد حاولت توصيل الأدوات الإصدار 2 و 1.4 ، ولا يوجد بنية الحزمة هذه. هل فاتني شيء واضح؟ ما هي إصدار أدوات السرعة التي يدعمها مكون الربيع/السرعة؟

هل كانت مفيدة؟

المحلول

الربيع لديه دعم السرعة عفا عليها الزمن بشكل افتراضي. أنا أمتد VelocityView الفصل من الربيع والتجاوز createVelocityContext الطريقة حيث أقوم بتهيئة الأدوات بنفسي. هنا هو كيف ينظر في النهاية.

نصائح أخرى

أنا استخدم أبسط قليلاً من الطريق. لا يمكنني أيضًا إجبار أدوات السرعة على العمل بسبب نقص وثائق التكوين والأمثلة. أنا فقط أحصل على الأدوات ذات السرعة -2.0.

<bean id="velocityViewResolver" 
    class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
    <property name="order" value="1"/>
    <property name="prefix" value="/WEB-INF/vm/"/>
    <property name="suffix" value=".vm"/>

    <property name="exposeSpringMacroHelpers" value="true"/>
    <property name="contentType" value="text/html;charset=UTF-8"/>
    <property name="attributesMap">
        <map>
            <!--Velocity Escape Tool-->
            <entry key="esc"><bean class="org.apache.velocity.tools.generic.EscapeTool"/></entry>
        </map>
    </property>        
</bean>

ثم ، في قالب السرعة ، يمكنك استخدامه كالمعتاد $ esc.html ($ htmlcodevar). هذا الحل بسيط للغاية ، بدون أطنان من الفصول الربيعية والتجاوز.

مع 3.0.5 ، استخدمت فئة مماثلة لما نشره Serg ، مع التعديل الوحيد هو استخدام الفئات المحدثة التي لم يستخدمها Spring (Tail من خلال VelocityToolboxView -> servlettoolboxmanager (المستخدمة في createvelocitycontext لدينا. تم إهماله ، لذلك قمت بتعديل itfelocitytoolcontext في إجابة سيرج ليكون:

private ToolContext getToolContext() throws IllegalStateException, IOException {
  if (toolContext == null) {
    XmlFactoryConfiguration factoryConfiguration = new XmlFactoryConfiguration("Default Tools");
    factoryConfiguration.read(getServletContext().getResourceAsStream(getToolboxConfigLocation()));
    ToolboxFactory factory = factoryConfiguration.createFactory();
    factory.configure(factoryConfiguration);
    toolContext = new ToolContext();
    for (String scope : Scope.values()) {
      toolContext.addToolbox(factory.createToolbox(scope));
    }
  }
  return toolContext;
}

اضطررت أيضًا إلى تغيير الخط الذي أنشأ VelocityContext لاستدعاء هذه الطريقة بشكل واضح.

تبدو الفاصولي الآن مثل:

<bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver"
      p:cache="false"
      p:prefix=""
      p:suffix=".vm"
      p:layoutUrl="templates/main.vm"
      p:toolboxConfigLocation="/WEB-INF/velocity/velocity-toolbox.xml"
      p:viewClass="path.to.overriden.class.VelocityToolsLayoutView"
/>

مستوحاة من إجابات من Scott و Serg ، إليك طريقة أخرى للقيام بذلك لا تتطلب XML: http://squirrel.pl/blog/2012/07/13/spring-velocity-tools-no-xml/

public class MyVelocityToolboxView extends VelocityView {
    @Override
    protected Context createVelocityContext(Map<String, Object> model,
            HttpServletRequest request, HttpServletResponse response) {
        ViewToolContext context = new ViewToolContext(getVelocityEngine(),
                request, response, getServletContext());

        ToolboxFactory factory = new ToolboxFactory();
        factory.configure(ConfigurationUtils.getVelocityView());

        for (String scope : Scope.values()) {
            context.addToolbox(factory.createToolbox(scope));
        }

        if (model != null) {
            for (Map.Entry<String, Object> entry : (Set<Map.Entry<String, Object>>) model
                    .entrySet()) {
                context.put(entry.getKey(), entry.getValue());
            }
        }
        return context;
    }
}

مستوحاة من جميع الإجابات أعلاه ، هذا هو تنفيذ بلدي VelocityLayoutView لأدوات الربيع والسرعة 2.0 ، أضاف بعض التحسن!

public class VelocityToolsView extends VelocityLayoutView {

    private static final String TOOL_MANAGER_KEY = ViewToolManager.class.getName();

    @Override
    protected Context createVelocityContext(
            Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) {
        ServletContext application = getServletContext();

        // use a shared instance of ViewToolManager
        ViewToolManager toolManager = (ViewToolManager)application.getAttribute(TOOL_MANAGER_KEY);
        if(toolManager == null) {
            toolManager = createToolManager(getVelocityEngine(), getToolboxConfigLocation(), application);
            application.setAttribute(TOOL_MANAGER_KEY, toolManager);
        }

        ViewToolContext toolContext = toolManager.createContext(request, response);
        if(model != null) { toolContext.putAll(model); }

        return toolContext;
    }

    private ViewToolManager createToolManager(VelocityEngine velocity, String toolFile, ServletContext application) {
        ViewToolManager toolManager = new ViewToolManager(application, false, false);
        toolManager.setVelocityEngine(velocity);

        // generic & view tools config
        FactoryConfiguration config = ConfigurationUtils.getVelocityView();
        // user defined tools config
        if(toolFile != null) {
            FactoryConfiguration userConfig = ConfigurationUtils.load(application.getRealPath(toolFile));
            config.addConfiguration(userConfig);
        }
        toolManager.configure(config);

        return toolManager;
    }
}

لقد وجدت ذلك هذا الاختلاف على تقنية @Serg عملت بالنسبة لي.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top