Repository: wicket-site Updated Branches: refs/heads/asf-site d1fa7a5d6 -> 26fcaca36
Minor changes to Tobias' article. Project: http://git-wip-us.apache.org/repos/asf/wicket-site/repo Commit: http://git-wip-us.apache.org/repos/asf/wicket-site/commit/26fcaca3 Tree: http://git-wip-us.apache.org/repos/asf/wicket-site/tree/26fcaca3 Diff: http://git-wip-us.apache.org/repos/asf/wicket-site/diff/26fcaca3 Branch: refs/heads/asf-site Commit: 26fcaca36d205557edfa07dd01cddaa46bab999a Parents: d1fa7a5 Author: Andrea Del Bene <[email protected]> Authored: Fri Feb 24 11:39:18 2017 +0100 Committer: Andrea Del Bene <[email protected]> Committed: Fri Feb 24 11:39:18 2017 +0100 ---------------------------------------------------------------------- content/atom.xml | 2 +- content/learn/examples/servlet3x.html | 101 +++++++++++++++++-------- content/start/quickstart.html | 6 +- learn/examples/servlet3x.md | 115 +++++++++++++++++++++-------- 4 files changed, 158 insertions(+), 66 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/wicket-site/blob/26fcaca3/content/atom.xml ---------------------------------------------------------------------- diff --git a/content/atom.xml b/content/atom.xml index f2dc309..038903d 100644 --- a/content/atom.xml +++ b/content/atom.xml @@ -3,7 +3,7 @@ <title>Apache Wicket</title> <link href="http://wicket.apache.org/atom.xml" rel="self"/> <link href="http://wicket.apache.org/"/> - <updated>2017-02-22T18:12:45+01:00</updated> + <updated>2017-02-24T11:37:16+01:00</updated> <id>http://wicket.apache.org/</id> <author> <name>Apache Wicket</name> http://git-wip-us.apache.org/repos/asf/wicket-site/blob/26fcaca3/content/learn/examples/servlet3x.html ---------------------------------------------------------------------- diff --git a/content/learn/examples/servlet3x.html b/content/learn/examples/servlet3x.html index cb56feb..ab27bfc 100644 --- a/content/learn/examples/servlet3x.html +++ b/content/learn/examples/servlet3x.html @@ -46,7 +46,7 @@ <h1>Servlet 3.x with Spring Framework</h1> </header> <section class="l-one-third right"> - <div id="toc" class="toc"><div id="toc-title"><h2>Table of Contents</h2></div><ul><li class="toc--level-1 toc--section-1"><a href="#creating-the-web-components"><span class="toc-number">1</span> <span class="toc-text">Creating the web components</span></a><ul><li class="toc--level-2 toc--section-2"><a href="#myfilterjava"><span class="toc-number">1.1</span> <span class="toc-text">MyFilter.java</span></a></li><li class="toc--level-2 toc--section-3"><a href="#mycontextloaderlistenerjava"><span class="toc-number">1.2</span> <span class="toc-text">MyContextLoaderListener.java</span></a></li><li class="toc--level-2 toc--section-4"><a href="#myrequestcontextlistenerjava"><span class="toc-number">1.3</span> <span class="toc-text">MyRequestContextListener.java</span></a></li></ul></li><li class="toc--level-1 toc--section-5"><a href="#creating-the-web-application-class"><span class="toc-number">2</span> <span class="toc-text">Creating the web application class</span></a></li><li class="toc--level-1 toc--section-6"><a href="#summary"><span class="toc-number">3</span> <span class="toc-text">Summary</span></a></li></ul></div> + <div id="toc" class="toc"><div id="toc-title"><h2>Table of Contents</h2></div><ul><li class="toc--level-1 toc--section-1"><a href="#creating-the-web-components"><span class="toc-number">1</span> <span class="toc-text">Creating the web components</span></a><ul><li class="toc--level-2 toc--section-2"><a href="#myfilterjava"><span class="toc-number">1.1</span> <span class="toc-text">MyFilter.java</span></a></li><li class="toc--level-2 toc--section-3"><a href="#mycontextloaderlistenerjava"><span class="toc-number">1.2</span> <span class="toc-text">MyContextLoaderListener.java</span></a></li><li class="toc--level-2 toc--section-4"><a href="#myrequestcontextlistenerjava"><span class="toc-number">1.3</span> <span class="toc-text">MyRequestContextListener.java</span></a></li><li class="toc--level-2 toc--section-5"><a href="#mysessionbeanjava"><span class="toc-number">1.4</span> <span class="toc-text">MySessionBean.java</span></a></li></ul></li><li class="toc--level-1 toc--sectio n-6"><a href="#creating-the-web-application-class"><span class="toc-number">2</span> <span class="toc-text">Creating the web application class</span></a></li><li class="toc--level-1 toc--section-7"><a href="#summary"><span class="toc-number">3</span> <span class="toc-text">Summary</span></a></li></ul></div> </section> <section class="l-two-third left"> <p>This example shows you how to configure Wicket to run without any XML definitions. @@ -54,21 +54,26 @@ It requires to use the Servlet 3.x API and a container which is able to load the web component definitions via annotations defined within this standard.</p> <h2 id="creating-the-web-components">Creating the web components</h2> <h3 id="myfilterjava">MyFilter.java</h3> -<p>First you have to configure the filter which was done within the web.xml in previous +<p>First you have to configure the filter which was done within the web.xml in previous versions of the Servlet specification. All initialization parameters are configured also via annotations. The Application class is defined in the init param with the name applicationClassName.</p> -<figure class="highlight"><pre><code class="language-java" data-lang="java"><span class="nd">@WebFilter</span><span class="o">(</span><span class="n">value</span> <span class="o">=</span> <span class="s">"/*"</span><span class="o">,</span> <span class="n">initParams</span> <span class="o">=</span> <span class="o">{</span> +<figure class="highlight"><pre><code class="language-java" data-lang="java"><span class="kn">import</span> <span class="nn">javax.servlet.annotation.WebFilter</span><span class="o">;</span> +<span class="kn">import</span> <span class="nn">javax.servlet.annotation.WebInitParam</span><span class="o">;</span> +<span class="kn">import</span> <span class="nn">org.apache.wicket.protocol.http.WicketFilter</span><span class="o">;</span> +<span class="nd">@WebFilter</span><span class="o">(</span><span class="n">value</span> <span class="o">=</span> <span class="s">"/*"</span><span class="o">,</span> <span class="n">initParams</span> <span class="o">=</span> <span class="o">{</span> <span class="nd">@WebInitParam</span><span class="o">(</span><span class="n">name</span> <span class="o">=</span> <span class="s">"applicationClassName"</span><span class="o">,</span> <span class="n">value</span> <span class="o">=</span> <span class="s">"com.my.package.MyApplication"</span><span class="o">),</span> <span class="nd">@WebInitParam</span><span class="o">(</span><span class="n">name</span> <span class="o">=</span> <span class="s">"configuration"</span><span class="o">,</span> <span class="n">value</span> <span class="o">=</span> <span class="s">"development"</span><span class="o">)</span> <span class="o">})</span> <span class="kd">public</span> <span class="kd">class</span> <span class="nc">MyFilter</span> <span class="kd">extends</span> <span class="n">WicketFilter</span> <span class="o">{}</span></code></pre></figure> <h3 id="mycontextloaderlistenerjava">MyContextLoaderListener.java</h3> <p>Another important class is the ContextLoaderListener which in our case is going to initialize -the Spring Frameworkâs context and effects a package scan for other Spring components. Usually +the Spring Frameworkâs context and effects a package scan for Spring components. Usually those Spring components are initialized with specific annotations like @Component or @Service for -example. You have to place in all your Spring Components in the scanned package or in subpackages -(com.my.package in this case)</p> -<figure class="highlight"><pre><code class="language-java" data-lang="java"><span class="nd">@WebListener</span> +example. You have to place them into the scanned package or in sub packages. (com.my.package in this case)</p> +<figure class="highlight"><pre><code class="language-java" data-lang="java"><span class="kn">import</span> <span class="nn">javax.servlet.annotation.WebListener</span><span class="o">;</span> +<span class="kn">import</span> <span class="nn">org.springframework.web.context.ContextLoaderListener</span><span class="o">;</span> +<span class="kn">import</span> <span class="nn">org.springframework.web.context.support.AnnotationConfigWebApplicationContext</span><span class="o">;</span> +<span class="nd">@WebListener</span> <span class="kd">public</span> <span class="kd">class</span> <span class="nc">MyContextLoaderListener</span> <span class="kd">extends</span> <span class="n">ContextLoaderListener</span> <span class="o">{</span> <span class="kd">private</span> <span class="kd">static</span> <span class="n">AnnotationConfigWebApplicationContext</span> <span class="n">context</span><span class="o">;</span> <span class="kd">static</span> <span class="o">{</span> @@ -79,38 +84,70 @@ example. You have to place in all your Spring Components in the scanned package <span class="kd">public</span> <span class="n">MyContextLoaderListener</span><span class="o">()</span> <span class="o">{</span> <span class="kd">super</span><span class="o">(</span><span class="n">context</span><span class="o">);</span> <span class="o">}</span> - <span class="kd">public</span> <span class="kd">static</span> <span class="n">AnnotationConfigWebApplicationContext</span> <span class="n">getContext</span><span class="o">()</span> <span class="o">{</span> - <span class="k">return</span> <span class="n">context</span><span class="o">;</span> - <span class="o">}</span> - <span class="kd">public</span> <span class="kd">static</span> <span class="kt">void</span> <span class="n">setContext</span><span class="o">(</span> - <span class="n">AnnotationConfigWebApplicationContext</span> <span class="n">context</span><span class="o">)</span> <span class="o">{</span> - <span class="n">MyContextLoaderListener</span><span class="o">.</span><span class="na">context</span> <span class="o">=</span> <span class="n">context</span><span class="o">;</span> - <span class="o">}</span> <span class="o">}</span></code></pre></figure> <h3 id="myrequestcontextlistenerjava">MyRequestContextListener.java</h3> <p>To configure Spring Framework to recognize the request context a RequestContextListener needs to be -defined. This listener enables the web project to define beans with a scope. So if you want a bean -to remains in the session as long as the user is surfing on your web page you can do this by adding the +defined. This listener enables the web project to use beans with a scope. So if you want a bean +to remain in the session as long as the user is surfing on your web page you can do this by adding the annotation @SessionScope together with @Component.</p> -<figure class="highlight"><pre><code class="language-java" data-lang="java"><span class="nd">@WebListener</span> +<figure class="highlight"><pre><code class="language-java" data-lang="java"><span class="kn">import</span> <span class="nn">javax.servlet.annotation.WebListener</span><span class="o">;</span> +<span class="kn">import</span> <span class="nn">org.springframework.web.context.request.RequestContextListener</span><span class="o">;</span> +<span class="nd">@WebListener</span> <span class="kd">public</span> <span class="kd">class</span> <span class="nc">MyRequestContextListener</span> <span class="kd">extends</span> <span class="n">RequestContextListener</span><span class="o">{}</span></code></pre></figure> +<h3 id="mysessionbeanjava">MySessionBean.java</h3> +<p>This is a small example of a session scoped bean within the âcom.my.packageâ package. It can be +autowired by using the @SpringBean annotation in any wicket component like a WebPage or a Panel.</p> +<figure class="highlight"><pre><code class="language-java" data-lang="java"><span class="kn">import</span> <span class="nn">javax.annotation.PostConstruct</span><span class="o">;</span> +<span class="kn">import</span> <span class="nn">javax.servlet.http.HttpSession</span><span class="o">;</span> +<span class="kn">import</span> <span class="nn">org.springframework.beans.factory.annotation.Autowired</span><span class="o">;</span> +<span class="kn">import</span> <span class="nn">org.springframework.stereotype.Component</span><span class="o">;</span> +<span class="kn">import</span> <span class="nn">org.springframework.web.context.annotation.SessionScope</span><span class="o">;</span> +<span class="nd">@Component</span> +<span class="nd">@SessionScope</span> +<span class="kd">public</span> <span class="kd">class</span> <span class="nc">MySessionBean</span> <span class="o">{</span> + <span class="nd">@Autowired</span> + <span class="kd">private</span> <span class="n">HttpSession</span> <span class="n">httpSession</span><span class="o">;</span> + <span class="nd">@PostConstruct</span> + <span class="kd">public</span> <span class="kt">void</span> <span class="n">postConstruct</span><span class="o">()</span> <span class="o">{</span> + <span class="c1">// do something after the bean has been constructed</span> + <span class="o">}</span> + <span class="kd">public</span> <span class="n">HttpSession</span> <span class="n">getHttpSession</span><span class="o">()</span> <span class="o">{</span> + <span class="k">return</span> <span class="n">httpSession</span><span class="o">;</span> + <span class="o">}</span> +<span class="o">}</span></code></pre></figure> +<p>Here you can see the autowiring in the component class:</p> +<figure class="highlight"><pre><code class="language-java" data-lang="java"><span class="kn">import</span> <span class="nn">com.my.package.MySessionBean</span><span class="o">;</span> +<span class="kn">import</span> <span class="nn">org.apache.wicket.spring.injection.annot.SpringBean</span><span class="o">;</span> +<span class="kn">import</span> <span class="nn">org.apache.wicket.markup.html.WebPage</span><span class="o">;</span> +<span class="kd">public</span> <span class="kd">class</span> <span class="nc">HelloWorld</span> <span class="kd">extends</span> <span class="n">WebPage</span> <span class="o">{</span> + <span class="nd">@SpringBean</span> + <span class="kd">private</span> <span class="n">MySessionBean</span> <span class="n">mySessionBean</span><span class="o">;</span> + <span class="kd">public</span> <span class="n">HomePage</span><span class="o">()</span> <span class="o">{</span> + <span class="c1">// do something</span> + <span class="o">}</span> +<span class="o">}</span></code></pre></figure> <h2 id="creating-the-web-application-class">Creating the web application class</h2> <p>Within our application class it is required to hand over the Springâs application context to the -SpringComponentInjector so that beans are also injected into Wicket components - this can be done by using the -@SpringBean annotation. If you are interested in how to implement the home page have a look at the -corresponding example: <a href="helloworld.html">Hello World!</a></p> -<figure class="highlight"><pre><code class="language-java" data-lang="java"><span class="kd">public</span> <span class="kd">class</span> <span class="nc">MyApplication</span> <span class="kd">extends</span> <span class="n">WebApplication</span> <span class="o">{</span> - <span class="nd">@Override</span> - <span class="kd">public</span> <span class="n">Class</span><span class="o"><?</span> <span class="kd">extends</span> <span class="n">Page</span><span class="o">></span> <span class="n">getHomePage</span><span class="o">()</span> <span class="o">{</span> - <span class="k">return</span> <span class="n">HelloWorld</span><span class="o">.</span><span class="na">class</span><span class="o">;</span> - <span class="o">}</span> - <span class="nd">@Override</span> - <span class="kd">protected</span> <span class="kt">void</span> <span class="n">init</span><span class="o">()</span> <span class="o">{</span> - <span class="kd">super</span><span class="o">.</span><span class="na">init</span><span class="o">();</span> - <span class="n">getComponentInstantiationListeners</span><span class="o">().</span><span class="na">add</span><span class="o">(</span> - <span class="k">new</span> <span class="n">SpringComponentInjector</span><span class="o">(</span><span class="k">this</span><span class="o">,</span> - <span class="n">MyContextLoaderListener</span><span class="o">.</span><span class="na">getContext</span><span class="o">()));</span> - <span class="o">}</span> +SpringComponentInjector so that beans are also injected into Wicket components. If you are interested in how to +implement the home page and the corresponding HTML markup have a look at the corresponding +example: <a href="helloworld.html">Hello World!</a></p> +<figure class="highlight"><pre><code class="language-java" data-lang="java"><span class="kn">import</span> <span class="nn">org.apache.wicket.Page</span><span class="o">;</span> +<span class="kn">import</span> <span class="nn">org.apache.wicket.protocol.http.WebApplication</span><span class="o">;</span> +<span class="kn">import</span> <span class="nn">org.apache.wicket.spring.injection.annot.SpringComponentInjector</span><span class="o">;</span> +<span class="kn">import</span> <span class="nn">org.springframework.web.context.support.WebApplicationContextUtils</span><span class="o">;</span> +<span class="kn">import</span> <span class="nn">com.my.package.HelloWorld</span><span class="o">;</span> +<span class="kd">public</span> <span class="kd">class</span> <span class="nc">MyApplication</span> <span class="kd">extends</span> <span class="n">WebApplication</span> <span class="o">{</span> + <span class="nd">@Override</span> + <span class="kd">public</span> <span class="n">Class</span><span class="o"><?</span> <span class="kd">extends</span> <span class="n">Page</span><span class="o">></span> <span class="n">getHomePage</span><span class="o">()</span> <span class="o">{</span> + <span class="k">return</span> <span class="n">HelloWorld</span><span class="o">.</span><span class="na">class</span><span class="o">;</span> + <span class="o">}</span> + <span class="nd">@Override</span> + <span class="kd">protected</span> <span class="kt">void</span> <span class="n">init</span><span class="o">()</span> <span class="o">{</span> + <span class="kd">super</span><span class="o">.</span><span class="na">init</span><span class="o">();</span> + <span class="n">getComponentInstantiationListeners</span><span class="o">().</span><span class="na">add</span><span class="o">(</span><span class="k">new</span> <span class="n">SpringComponentInjector</span><span class="o">(</span><span class="k">this</span><span class="o">,</span> + <span class="n">WebApplicationContextUtils</span><span class="o">.</span><span class="na">getRequiredWebApplicationContext</span><span class="o">(</span> + <span class="n">getServletContext</span><span class="o">())));</span> + <span class="o">}</span> <span class="o">}</span></code></pre></figure> <h2 id="summary">Summary</h2> <p>Since Servlet 3.x and the corresponding changes within the Spring Framework to configure web applications http://git-wip-us.apache.org/repos/asf/wicket-site/blob/26fcaca3/content/start/quickstart.html ---------------------------------------------------------------------- diff --git a/content/start/quickstart.html b/content/start/quickstart.html index 1205fd5..90168e4 100644 --- a/content/start/quickstart.html +++ b/content/start/quickstart.html @@ -153,9 +153,9 @@ generated (e.g. for Eclipse users this would be your workspace folder). Paste the command line into your terminal window and press «enter» to execute the command.</p> <p>You can create your quickstart interactively too with the following Maven command:</p> -<p><code class="highlighter-rouge">shell - mvn archetype:generate -DarchetypeCatalog=http://wicket.apache.org -</code></p> +<div class="highlighter-rouge"><pre class="highlight"><code> mvn archetype:generate -DarchetypeCatalog<span class="o">=</span>http://wicket.apache.org +</code></pre> +</div> <h4 id="result-of-the-maven-command">Result of the Maven command</h4> <p>Executing the Maven command line will result the following directory structure:</p> http://git-wip-us.apache.org/repos/asf/wicket-site/blob/26fcaca3/learn/examples/servlet3x.md ---------------------------------------------------------------------- diff --git a/learn/examples/servlet3x.md b/learn/examples/servlet3x.md index e5791cd..3cd3daf 100644 --- a/learn/examples/servlet3x.md +++ b/learn/examples/servlet3x.md @@ -11,11 +11,15 @@ web component definitions via annotations defined within this standard. ### MyFilter.java ### -First you have to configure the filter which was done within the web.xml in previous +First you have to configure the filter which was done within the web.xml in previous versions of the Servlet specification. All initialization parameters are configured also via annotations. The Application class is defined in the init param with the name applicationClassName. {% highlight java %} +import javax.servlet.annotation.WebFilter; +import javax.servlet.annotation.WebInitParam; +import org.apache.wicket.protocol.http.WicketFilter; + @WebFilter(value = "/*", initParams = { @WebInitParam(name = "applicationClassName", value = "com.my.package.MyApplication"), @@ -26,13 +30,16 @@ public class MyFilter extends WicketFilter {} ### MyContextLoaderListener.java ### Another important class is the ContextLoaderListener which in our case is going to initialize -the Spring Framework's context and effects a package scan for other Spring components. Usually +the Spring Framework's context and effects a package scan for Spring components. Usually those Spring components are initialized with specific annotations like @Component or @Service for -example. You have to place in all your Spring Components in the scanned package or in subpackages -(com.my.package in this case) +example. You have to place them into the scanned package or in sub packages. (com.my.package in this case) {% highlight java %} +import javax.servlet.annotation.WebListener; +import org.springframework.web.context.ContextLoaderListener; +import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; + @WebListener public class MyContextLoaderListener extends ContextLoaderListener { @@ -47,52 +54,100 @@ public class MyContextLoaderListener extends ContextLoaderListener { public MyContextLoaderListener() { super(context); } - - public static AnnotationConfigWebApplicationContext getContext() { - return context; - } - - public static void setContext( - AnnotationConfigWebApplicationContext context) { - MyContextLoaderListener.context = context; - } } {% endhighlight %} ### MyRequestContextListener.java ### To configure Spring Framework to recognize the request context a RequestContextListener needs to be -defined. This listener enables the web project to define beans with a scope. So if you want a bean -to remains in the session as long as the user is surfing on your web page you can do this by adding the +defined. This listener enables the web project to use beans with a scope. So if you want a bean +to remain in the session as long as the user is surfing on your web page you can do this by adding the annotation @SessionScope together with @Component. {% highlight java %} +import javax.servlet.annotation.WebListener; +import org.springframework.web.context.request.RequestContextListener; + @WebListener public class MyRequestContextListener extends RequestContextListener{} {% endhighlight %} +### MySessionBean.java ### + +This is a small example of a session scoped bean within the "com.my.package" package. It can be +autowired by using the @SpringBean annotation in any wicket component like a WebPage or a Panel. + +{% highlight java %} +import javax.annotation.PostConstruct; +import javax.servlet.http.HttpSession; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; +import org.springframework.web.context.annotation.SessionScope; + +@Component +@SessionScope +public class MySessionBean { + + @Autowired + private HttpSession httpSession; + + @PostConstruct + public void postConstruct() { + // do something after the bean has been constructed + } + + public HttpSession getHttpSession() { + return httpSession; + } +} +{% endhighlight %} + +Here you can see the autowiring in the component class: + +{% highlight java %} +import com.my.package.MySessionBean; +import org.apache.wicket.spring.injection.annot.SpringBean; +import org.apache.wicket.markup.html.WebPage; + +public class HelloWorld extends WebPage { + + @SpringBean + private MySessionBean mySessionBean; + + public HomePage() { + // do something + } +} +{% endhighlight %} + ## Creating the web application class Within our application class it is required to hand over the Spring's application context to the -SpringComponentInjector so that beans are also injected into Wicket components - this can be done by using the -@SpringBean annotation. If you are interested in how to implement the home page have a look at the -corresponding example: [Hello World!](helloworld.html) +SpringComponentInjector so that beans are also injected into Wicket components. If you are interested in how to +implement the home page and the corresponding HTML markup have a look at the corresponding +example: [Hello World!](helloworld.html) {% highlight java %} +import org.apache.wicket.Page; +import org.apache.wicket.protocol.http.WebApplication; +import org.apache.wicket.spring.injection.annot.SpringComponentInjector; +import org.springframework.web.context.support.WebApplicationContextUtils; +import com.my.package.HelloWorld; + public class MyApplication extends WebApplication { - @Override - public Class<? extends Page> getHomePage() { - return HelloWorld.class; - } - - @Override - protected void init() { - super.init(); - getComponentInstantiationListeners().add( - new SpringComponentInjector(this, - MyContextLoaderListener.getContext())); - } + @Override + public Class<? extends Page> getHomePage() { + return HelloWorld.class; + } + + @Override + protected void init() { + super.init(); + getComponentInstantiationListeners().add(new SpringComponentInjector(this, + WebApplicationContextUtils.getRequiredWebApplicationContext( + getServletContext()))); + } } {% endhighlight %}
