• Wicket Hello World Example


    A simple hello world example in Wicket, show the basic structure of Wicket web application.

    Tools and technologies used in this article

    • Apache Wicket 1.4.17
    • Eclipse 3.6
    • Maven 3.0.3
    • JDK 1.6.0.13

    1. Directory Structure

    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 :

    wicket directory structure

    Follow below steps to create the entire directory structure.

    2. Maven Quick Start

    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.

    3. Wicket Dependency

    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>
    

    Wicket need SLF4J !

    You have to include the slf4j logging implementation, otherwise Wicket will be failed to start.

    Wicket need resource filter

    Remember to add the resource filter, Wicket puts all files in same package folder, if you didn’t define the resource filter to include everything “<include>*</include>” , “html”, “properties” or other resources files may failed to copy to the correct target folder.

    4. Wicket Applications

    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>
            
        </head>
        <body>
    	  <h1>
            <span wicket:id="message">message will be replace later</span>
    	  </h1>
        </body>
    </html>
    

    5. Wicket Filters

    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>
    

    6. Build it

    All files are ready, build it with Maven.

    mvn eclipse:eclipse -Dwtpversion=2.0
    

    Import it into Eclipse and start the project.

    7. Test It

    Visit http://localhost:8080/WicketExamples/ , see figure :
    Wicket hello world

    Done.

  • 相关阅读:
    R_Studio(关联)使用apriori函数简单查看数据存在多少条关联规则,并按支持度降序排序输出
    Unity3D_(游戏)2D坦克大战 像素版
    R_Studio(cart算法决策树)对book3.csv数据用测试集进行测试并评估模型
    R_Studio(决策树算法)鸢尾花卉数据集Iris是一类多重变量分析的数据集【精】
    R_针对churn数据用id3、cart、C4.5和C5.0创建决策树模型进行判断哪种模型更合适
    R_Studio(教师经济信息)逻辑回归分析的方法和技巧
    JavaWeb_Get和Post方法传输数据区别
    JavaWeb_响应和请求数据包
    JavaWeb_使用dom4j解析、生成XML文件
    JavaWeb_ XML文件
  • 原文地址:https://www.cnblogs.com/ghgyj/p/4765403.html
Copyright © 2020-2023  润新知