Modified: velocity/site/production/engine/1.7/webapps.html URL: http://svn.apache.org/viewvc/velocity/site/production/engine/1.7/webapps.html?rev=1834366&r1=1834365&r2=1834366&view=diff ============================================================================== --- velocity/site/production/engine/1.7/webapps.html (original) +++ velocity/site/production/engine/1.7/webapps.html Mon Jun 25 18:46:53 2018 @@ -267,14 +267,14 @@ webapp.resource.loader.path=/WEB-INF/myt <h3 id="changing-object-state-dont">Changing Object State - Don't!<a class="headerlink" href="#changing-object-state-dont" title="Permanent link">¶</a></h3> <p>Velocity provides the ability to call any method of an object acting as a reference. This can be useful when displaying information into the page but is dangerous when object or application state is modified.</p> <p>For example, the following code safely calls the size() method of a list and displays the result.</p> -<div class="codehilite"><pre><span class="x">There are </span><span class="p">$</span><span class="nv">users</span><span class="p">.</span><span class="nv">size</span><span class="p">()</span><span class="x"> currently logged in.</span> +<div class="codehilite"><pre>There are $users.size() currently logged in. </pre></div> <p>An example of an unsafe operation concerns a financial web page with an object in the context that calculates data year by year. The method calculateNextYear() calculates data for the next year and advances an internal counter:</p> -<div class="codehilite"><pre><span class="x">2005 data: </span><span class="p">$</span><span class="nv">table</span><span class="p">.</span><span class="nv">data</span><span class="x"></span> -<span class="p">$</span><span class="nv">table</span><span class="p">.</span><span class="nv">calculateNextYear</span><span class="p">()</span><span class="x"></span> -<span class="x">2006 data: </span><span class="p">$</span><span class="nv">table</span><span class="p">.</span><span class="nv">data</span><span class="x"></span> +<div class="codehilite"><pre>2005 data: $table.data +$table.calculateNextYear() +2006 data: $table.data </pre></div>
Modified: velocity/site/production/engine/2.0/developer-guide.html URL: http://svn.apache.org/viewvc/velocity/site/production/engine/2.0/developer-guide.html?rev=1834366&r1=1834365&r2=1834366&view=diff ============================================================================== --- velocity/site/production/engine/2.0/developer-guide.html (original) +++ velocity/site/production/engine/2.0/developer-guide.html Mon Jun 25 18:46:53 2018 @@ -524,15 +524,15 @@ template.merge( context2, writer ); <h3 id="objects-created-in-the-template">Objects Created in the Template<a class="headerlink" href="#objects-created-in-the-template" title="Permanent link">¶</a></h3> <p>There are two common situations where the Java code must deal with objects created at runtime in the template:</p> <p>When a template author calls a method of an object placed into the context by Java code.</p> -<div class="codehilite"><pre><span class="cp">#</span><span class="nf">set</span><span class="p">($</span><span class="nv">myarr</span> <span class="o">=</span> <span class="o">[</span><span class="s2">"a"</span><span class="p">,</span><span class="s2">"b"</span><span class="p">,</span><span class="s2">"c"</span><span class="p">]</span> <span class="p">)</span><span class="x"></span> -<span class="p">$</span><span class="nv">foo</span><span class="p">.</span><span class="nv">bar</span><span class="p">(</span> <span class="p">$</span><span class="nv">myarr</span> <span class="p">)</span><span class="x"></span> +<div class="codehilite"><pre>#set($myarr = ["a","b","c"] ) +$foo.bar( $myarr ) </pre></div> <p>When a template adds objects to the context, the Java code can access those objects after the merge process is complete.</p> -<div class="codehilite"><pre><span class="cp">#</span><span class="nf">set</span><span class="p">($</span><span class="nv">myarr</span> <span class="o">=</span> <span class="o">[</span><span class="s2">"a"</span><span class="p">,</span><span class="s2">"b"</span><span class="p">,</span><span class="s2">"c"</span><span class="p">]</span> <span class="p">)</span><span class="x"></span> -<span class="cp">#</span><span class="nf">set</span><span class="p">(</span> <span class="p">$</span><span class="nv">foo</span> <span class="o">=</span> <span class="m">1</span> <span class="p">)</span><span class="x"></span> -<span class="cp">#</span><span class="nf">set</span><span class="p">(</span> <span class="p">$</span><span class="nv">bar</span> <span class="o">=</span> <span class="s2">"bar"</span><span class="p">)</span><span class="x"></span> +<div class="codehilite"><pre>#set($myarr = ["a","b","c"] ) +#set( $foo = 1 ) +#set( $bar = "bar") </pre></div> @@ -660,7 +660,7 @@ template.merge( context2, writer ); <p>where the template we used, testtemplate.vm, is</p> -<div class="codehilite"><pre><span class="x">Hi! This </span><span class="p">$</span><span class="nv">name</span><span class="x"> from the </span><span class="p">$</span><span class="nv">project</span><span class="x"> project.</span> +<div class="codehilite"><pre>Hi! This $name from the $project project. </pre></div> @@ -1698,18 +1698,18 @@ vc.put("root", root.getRootEle <p>Velocity can be integrated into the Java Scripting Language Framework (as defined by the <a href="https://www.jcp.org/en/jsr/detail?id=223">JSR-223 API</a>).</p> <p>This section is a brief illustration of how to use Velocity Scripting framework through the JSR-223 API.</p> <p>Hello World example:</p> -<div class="codehilite"><pre><span class="x">// get script manager, create a new Velocity script engine factory and get an engine from it</span> -<span class="x">ScriptEngineManager manager = new ScriptEngineManager();</span> -<span class="x">manager.registerEngineName("velocity", new VelocityScriptEngineFactory());</span> -<span class="x">ScriptEngine engine = manager.getEngineByName("velocity");</span> +<div class="codehilite"><pre>// get script manager, create a new Velocity script engine factory and get an engine from it +ScriptEngineManager manager = new ScriptEngineManager(); +manager.registerEngineName("velocity", new VelocityScriptEngineFactory()); +ScriptEngine engine = manager.getEngineByName("velocity"); -<span class="x">System.setProperty(VelocityScriptEngine.VELOCITY_PROPERTIES, "path/to/velocity.properties");</span> -<span class="x">String script = "Hello </span><span class="p">$</span><span class="nv">world</span><span class="x">";</span> -<span class="x">Writer writer = new StringWriter();</span> -<span class="x">engine.getContext().setWriter(writer);</span> -<span class="x">Object result = engine.eval(script);</span> -<span class="x">System.out.println(writer);</span> +System.setProperty(VelocityScriptEngine.VELOCITY_PROPERTIES, "path/to/velocity.properties"); +String script = "Hello $world"; +Writer writer = new StringWriter(); +engine.getContext().setWriter(writer); +Object result = engine.eval(script); +System.out.println(writer); </pre></div>