BAEL-226 - Source code for wicket

This commit is contained in:
slavisa-baeldung
2016-09-17 13:07:39 +02:00
parent b8eb2a799a
commit cf53bc6585
14 changed files with 73 additions and 74 deletions

4
wicket/README.md Normal file
View File

@@ -0,0 +1,4 @@
From the same directory where pom.xml is, execute the following command to run the project:
mvn jetty:run

106
wicket/pom.xml Normal file
View File

@@ -0,0 +1,106 @@
<?xml version="1.0" encoding="UTF-8"?>
<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.baeldung.wicket.examples</groupId>
<artifactId>wicket-intro</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>WicketIntro</name>
<properties>
<wicket.version>7.4.0</wicket.version>
<jetty9.version>9.2.13.v20150730</jetty9.version>
<log4j.version>2.5</log4j.version>
<junit.version>4.12</junit.version>
<maven-compiler-plugin.version>3.5.1</maven-compiler-plugin.version>
<maven-war-plugin.version>2.6</maven-war-plugin.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- WICKET DEPENDENCIES -->
<dependency>
<groupId>org.apache.wicket</groupId>
<artifactId>wicket-core</artifactId>
<version>${wicket.version}</version>
</dependency>
<!-- JUNIT DEPENDENCY FOR TESTING -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<!-- JETTY DEPENDENCIES FOR TESTING -->
<dependency>
<groupId>org.eclipse.jetty.aggregate</groupId>
<artifactId>jetty-all</artifactId>
<version>${jetty9.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<filtering>false</filtering>
<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>
<testResources>
<testResource>
<filtering>false</filtering>
<directory>src/test/resources</directory>
</testResource>
<testResource>
<filtering>false</filtering>
<directory>src/test/java</directory>
<includes>
<include>**</include>
</includes>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</testResource>
</testResources>
<plugins>
<plugin>
<inherited>true</inherited>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
<showWarnings>true</showWarnings>
<showDeprecation>true</showDeprecation>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>${maven-war-plugin.version}</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>${jetty9.version}</version>
</plugin>
</plugins>
</build>
</project>

View File

@@ -0,0 +1,52 @@
<!DOCTYPE html>
<html xmlns:wicket="http://wicket.apache.org">
<head>
<meta charset="utf-8" />
<title>Wicket Intro Examples</title>
<style type="text/css" media="screen">
<!--
body {
margin: 0px
}
#horizon {
text-align: center;
position: absolute;
top: 50%;
left: 0px;
width: 100%;
height: 1px;
overflow: visible;
visibility: visible;
display: block
}
#content {
font-family: Verdana, Geneva, Arial, sans-serif;
margin-left: -250px;
position: absolute;
top: -35px;
left: 50%;
width: 500px;
height: 70px;
visibility: visible
}
-->
</style>
</head>
<body>
<div id="horizon">
<div id="content">
<div>
<h3>Wicket Introduction Examples:</h3>
<wicket:link>
<a href="helloworld/HelloWorld.html">Hello World!</a>
<br />
<br />
<a href="cafeaddress/CafeAddress.html">Cafes</a>
</wicket:link>
</div>
</div>
</div>
</body>
</html>

View File

@@ -0,0 +1,9 @@
package com.baeldung.wicket.examples;
import org.apache.wicket.markup.html.WebPage;
public class Examples extends WebPage {
private static final long serialVersionUID = 1L;
}

View File

@@ -0,0 +1,27 @@
package com.baeldung.wicket.examples;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.protocol.http.WebApplication;
import com.baeldung.wicket.examples.cafeaddress.CafeAddress;
import com.baeldung.wicket.examples.helloworld.HelloWorld;
public class ExamplesApplication extends WebApplication {
/**
* @see org.apache.wicket.Application#getHomePage()
*/
@Override
public Class<? extends WebPage> getHomePage() {
return Examples.class;
}
/**
* @see org.apache.wicket.Application#init()
*/
@Override
public void init() {
super.init();
mountPage("/examples/helloworld", HelloWorld.class);
mountPage("/examples/cafes", CafeAddress.class);
}
}

View File

@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html xmlns:wicket="http://wicket.apache.org">
<head>
<meta charset="utf-8" />
<title>Cafes</title>
</head>
<body>
<div style="width: 800px; margin: 0 auto;">
<select wicket:id="cafes"></select>
<p>
Address: <span wicket:id="address">address</span>
</p>
</div>
</body>
</html>

View File

@@ -0,0 +1,72 @@
package com.baeldung.wicket.examples.cafeaddress;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.request.mapper.parameter.PageParameters;
public class CafeAddress extends WebPage {
private static final long serialVersionUID = 1L;
String selectedCafe;
Address address;
Map<String, Address> cafeNamesAndAddresses = new HashMap<>();
public CafeAddress(final PageParameters parameters) {
super(parameters);
initCafes();
ArrayList<String> cafeNames = new ArrayList<>(this.cafeNamesAndAddresses.keySet());
this.selectedCafe = cafeNames.get(0);
this.address = new Address(this.cafeNamesAndAddresses.get(this.selectedCafe).getAddress());
final Label addressLabel = new Label("address", new PropertyModel<String>(this.address, "address"));
addressLabel.setOutputMarkupId(true);
final DropDownChoice<String> cafeDropdown = new DropDownChoice<>("cafes", new PropertyModel<String>(this, "selectedCafe"), cafeNames);
cafeDropdown.add(new AjaxFormComponentUpdatingBehavior("onchange") {
private static final long serialVersionUID = 1L;
@Override
protected void onUpdate(AjaxRequestTarget target) {
String name = (String) cafeDropdown.getDefaultModel().getObject();
address.setAddress(cafeNamesAndAddresses.get(name).getAddress());
target.add(addressLabel);
}
});
add(addressLabel);
add(cafeDropdown);
}
private void initCafes() {
this.cafeNamesAndAddresses.put("Linda's Cafe", new Address("35 Bower St."));
this.cafeNamesAndAddresses.put("Old Tree", new Address("2 Edgware Rd."));
}
class Address implements Serializable {
private String sAddress = "";
public Address(String address) {
this.sAddress = address;
}
public String getAddress() {
return this.sAddress;
}
public void setAddress(String address) {
this.sAddress = address;
}
}
}

View File

@@ -0,0 +1,5 @@
<html>
<body>
<span wicket:id="hello"></span>
</body>
</html>

View File

@@ -0,0 +1,13 @@
package com.baeldung.wicket.examples.helloworld;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
public class HelloWorld extends WebPage {
private static final long serialVersionUID = 1L;
public HelloWorld() {
add(new Label("hello", "Hello World!"));
}
}

View File

@@ -0,0 +1,23 @@
package com.baeldung.wicket.examples;
import org.apache.wicket.util.tester.WicketTester;
import org.junit.Before;
import org.junit.Test;
public class TestHomePage {
private WicketTester tester;
@Before
public void setUp() {
tester = new WicketTester(new ExamplesApplication());
}
@Test
public void whenPageInvoked_thanRenderedOK() {
//start and render the test page
tester.startPage(Examples.class);
//assert rendered page class
tester.assertRenderedPage(Examples.class);
}
}

View File

@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://www.oracle.com/webfolder/technetwork/jsc/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>CafeAddress</display-name>
<!--
There are three means to configure Wickets configuration mode and they
are tested in the order given.
1) A system property: -Dwicket.configuration
2) servlet specific <init-param>
3) context specific <context-param>
The value might be either "development" (reloading when templates change) or
"deployment". If no configuration is found, "development" is the default. -->
<filter>
<filter-name>wicket.examples</filter-name>
<filter-class>org.apache.wicket.protocol.http.WicketFilter</filter-class>
<init-param>
<param-name>applicationClassName</param-name>
<param-value>com.baeldung.wicket.examples.ExamplesApplication</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>wicket.examples</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>