原文地址: http://www.mkyong.com/wicket/wicket-hello-world-example-with-maven-tutorial/
A simple hello world example in Wicket, show the basic structure of Wicket web application.
Tools and technologies used in this article
See the final directory structure of this Wicket hello world web application. In Wicket, you need to put all files “.html” and “.java” in a same package directory.
See figure below :
Follow below steps to create the entire directory structure.
Create a simple web application via Maven.
mvn archetype:generate -DgroupId=com.mkyong.core -DartifactId=WicketExamples -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
Now, it will create all the standard web folder structure.
Add Wicket dependency in your Maven pom.xml
file.
File : pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.mkyong.core</groupId> <artifactId>WicketExamples</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>WicketExamples</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>org.apache.wicket</groupId> <artifactId>wicket</artifactId> <version>1.4.17</version> </dependency> <!-- slf4j-log4j --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.5.6</version> </dependency> </dependencies> <build> <finalName>WicketExamples</finalName> <resources> <resource> <directory>src/main/resources</directory> </resource> <resource> <filtering>false</filtering> <directory>src/main/java</directory> <includes> <include>*</include> </includes> <excludes> <exclude>**/*.java</exclude> </excludes> </resource> </resources> <plugins> <plugin> <inherited>true</inherited> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.6</source> <target>1.6</target> <optimise>true</optimise> <debug>true</debug> </configuration> </plugin> </plugins> </build> </project>
In Wicket, most stuffs are work by convention, you don’t need to configure it. In this case, WebApplication
returns a “Hello.class” as the default page, when Wicket see this “Hello.class“, it know the mark up “html” page should be “Hello.html“, and it should be able to find at the same package directory. This is why Wicket need you to put both “html” and “java” class together.
File : MyApplication.java – The main application entrance.
package com.mkyong; import org.apache.wicket.Page; import org.apache.wicket.protocol.http.WebApplication; import com.mkyong.hello.Hello; public class MyApplication extends WebApplication { @Override public Class<? extends Page> getHomePage() { return Hello.class; //return default page } }
File : Hello.java
package com.mkyong.hello; import org.apache.wicket.PageParameters; import org.apache.wicket.markup.html.basic.Label; import org.apache.wicket.markup.html.WebPage; public class Hello extends WebPage { private static final long serialVersionUID = 1L; public Hello(final PageParameters parameters) { add(new Label("message", "Hello World, Wicket")); } }
File : Hello.html
<html> <head> <title>Wicket Hello World</title> </head> <body> <h1> <span wicket:id="message">message will be replace later</span> </h1> </body> </html>
To make Wicket works, you need to register Wicket filters in your web.xml
file.
File : web.xml
<?xml version="1.0" encoding="ISO-8859-1"?> <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4"> <display-name>Wicket Web Application</display-name> <filter> <filter-name>wicket.wicketTest</filter-name> <filter-class>org.apache.wicket.protocol.http.WicketFilter</filter-class> <init-param> <param-name>applicationClassName</param-name> <param-value>com.mkyong.MyApplication</param-value> </init-param> </filter> <filter-mapping> <filter-name>wicket.wicketTest</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
All files are ready, build it with Maven.
mvn eclipse:eclipse -Dwtpversion=2.0
Import it into Eclipse and start the project.
Visit http://localhost:8080/WicketExamples/ , see figure :
Done.