New project with both examples included and a home page that contains a link for each.

This commit is contained in:
diego 2016-08-31 04:29:24 +02:00
parent c1667fe024
commit 0c1ad4f7c3
16 changed files with 570 additions and 71 deletions

View File

@ -19,90 +19,82 @@ import org.eclipse.jetty.webapp.WebAppContext;
* Separate startup class for people that want to run the examples directly. Use parameter * Separate startup class for people that want to run the examples directly. Use parameter
* -Dcom.sun.management.jmxremote to startup JMX (and e.g. connect with jconsole). * -Dcom.sun.management.jmxremote to startup JMX (and e.g. connect with jconsole).
*/ */
public class Start public class Start {
{ /**
/** * Main function, starts the jetty server.
* Main function, starts the jetty server. *
* * @param args
* @param args */
*/ public static void main(String[] args) {
public static void main(String[] args) System.setProperty("wicket.configuration", "development");
{
System.setProperty("wicket.configuration", "development");
Server server = new Server(); Server server = new Server();
HttpConfiguration http_config = new HttpConfiguration(); HttpConfiguration http_config = new HttpConfiguration();
http_config.setSecureScheme("https"); http_config.setSecureScheme("https");
http_config.setSecurePort(8443); http_config.setSecurePort(8443);
http_config.setOutputBufferSize(32768); http_config.setOutputBufferSize(32768);
ServerConnector http = new ServerConnector(server, new HttpConnectionFactory(http_config)); ServerConnector http = new ServerConnector(server, new HttpConnectionFactory(http_config));
http.setPort(8080); http.setPort(8080);
http.setIdleTimeout(1000 * 60 * 60); http.setIdleTimeout(1000 * 60 * 60);
server.addConnector(http); server.addConnector(http);
Resource keystore = Resource.newClassPathResource("/keystore"); Resource keystore = Resource.newClassPathResource("/keystore");
if (keystore != null && keystore.exists()) if (keystore != null && keystore.exists()) {
{ // if a keystore for a SSL certificate is available, start a SSL
// if a keystore for a SSL certificate is available, start a SSL // connector on port 8443.
// connector on port 8443. // By default, the quickstart comes with a Apache Wicket Quickstart
// By default, the quickstart comes with a Apache Wicket Quickstart // Certificate that expires about half way september 2021. Do not
// Certificate that expires about half way september 2021. Do not // use this certificate anywhere important as the passwords are
// use this certificate anywhere important as the passwords are // available in the source.
// available in the source.
SslContextFactory sslContextFactory = new SslContextFactory(); SslContextFactory sslContextFactory = new SslContextFactory();
sslContextFactory.setKeyStoreResource(keystore); sslContextFactory.setKeyStoreResource(keystore);
sslContextFactory.setKeyStorePassword("wicket"); sslContextFactory.setKeyStorePassword("wicket");
sslContextFactory.setKeyManagerPassword("wicket"); sslContextFactory.setKeyManagerPassword("wicket");
HttpConfiguration https_config = new HttpConfiguration(http_config); HttpConfiguration https_config = new HttpConfiguration(http_config);
https_config.addCustomizer(new SecureRequestCustomizer()); https_config.addCustomizer(new SecureRequestCustomizer());
ServerConnector https = new ServerConnector(server, new SslConnectionFactory( ServerConnector https = new ServerConnector(server, new SslConnectionFactory(sslContextFactory, "http/1.1"), new HttpConnectionFactory(https_config));
sslContextFactory, "http/1.1"), new HttpConnectionFactory(https_config)); https.setPort(8443);
https.setPort(8443); https.setIdleTimeout(500000);
https.setIdleTimeout(500000);
server.addConnector(https); server.addConnector(https);
System.out.println("SSL access to the examples has been enabled on port 8443"); System.out.println("SSL access to the examples has been enabled on port 8443");
System.out System.out.println("You can access the application using SSL on https://localhost:8443");
.println("You can access the application using SSL on https://localhost:8443"); System.out.println();
System.out.println(); }
}
WebAppContext bb = new WebAppContext(); WebAppContext bb = new WebAppContext();
bb.setServer(server); bb.setServer(server);
bb.setContextPath("/"); bb.setContextPath("/");
bb.setWar("src/main/webapp"); bb.setWar("src/main/webapp");
// uncomment the next two lines if you want to start Jetty with WebSocket (JSR-356) support // uncomment the next two lines if you want to start Jetty with WebSocket (JSR-356) support
// you need org.apache.wicket:wicket-native-websocket-javax in the classpath! // you need org.apache.wicket:wicket-native-websocket-javax in the classpath!
// ServerContainer serverContainer = WebSocketServerContainerInitializer.configureContext(bb); // ServerContainer serverContainer = WebSocketServerContainerInitializer.configureContext(bb);
// serverContainer.addEndpoint(new WicketServerEndpointConfig()); // serverContainer.addEndpoint(new WicketServerEndpointConfig());
// uncomment next line if you want to test with JSESSIONID encoded in the urls // uncomment next line if you want to test with JSESSIONID encoded in the urls
// ((AbstractSessionManager) // ((AbstractSessionManager)
// bb.getSessionHandler().getSessionManager()).setUsingCookies(false); // bb.getSessionHandler().getSessionManager()).setUsingCookies(false);
server.setHandler(bb); server.setHandler(bb);
MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer(); MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
MBeanContainer mBeanContainer = new MBeanContainer(mBeanServer); MBeanContainer mBeanContainer = new MBeanContainer(mBeanServer);
server.addEventListener(mBeanContainer); server.addEventListener(mBeanContainer);
server.addBean(mBeanContainer); server.addBean(mBeanContainer);
try try {
{ server.start();
server.start(); server.join();
server.join(); } catch (Exception e) {
} e.printStackTrace();
catch (Exception e) System.exit(100);
{ }
e.printStackTrace(); }
System.exit(100);
}
}
} }

View File

@ -0,0 +1,136 @@
<?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>
<spring-web.version>4.1.1.RELEASE</spring-web.version>
<javax.servlet-api.version>3.1.0</javax.servlet-api.version>
<wicket-spring.version>8.0.0-M1</wicket-spring.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>
<wtp.version>none</wtp.version>
</properties>
<dependencies>
<!-- WICKET DEPENDENCIES -->
<dependency>
<groupId>org.apache.wicket</groupId>
<artifactId>wicket-core</artifactId>
<version>${wicket.version}</version>
</dependency>
<!-- SPRING DEPENDENCIES -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring-web.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${javax.servlet-api.version}</version>
</dependency>
<dependency>
<groupId>org.apache.wicket</groupId>
<artifactId>wicket-spring</artifactId>
<version>${wicket-spring.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>
<finalName>WicketIntro</finalName>
<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>
<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>
<configuration>
<systemProperties>
<systemProperty>
<name>maven.project.build.directory.test-classes</name>
<value>${project.build.directory}/test-classes</value>
</systemProperty>
</systemProperties>
<jettyXml>${project.basedir}/src/test/jetty/jetty.xml,${project.basedir}/src/test/jetty/jetty-ssl.xml,${project.basedir}/src/test/jetty/jetty-http.xml,${project.basedir}/src/test/jetty/jetty-https.xml</jettyXml>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>Apache Nexus</id>
<url>https://repository.apache.org/content/repositories/snapshots/</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
</project>

View File

@ -0,0 +1,10 @@
package com.baeldung.wicket.examples;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan
public class ApplicationConfiguration {
}

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,26 @@
package com.baeldung.wicket.examples;
import javax.servlet.FilterRegistration;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import org.apache.wicket.protocol.http.WicketFilter;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
public class WebAppInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext container) throws ServletException {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
container.addListener(new ContextLoaderListener(context));
context.register(ApplicationConfiguration.class);
FilterRegistration filter = container.addFilter("ExamplesApplication", WicketFilter.class);
filter.setInitParameter("applicationClassName", ExamplesApplication.class.getName());
filter.setInitParameter(WicketFilter.FILTER_MAPPING_PARAM, "/*");
filter.addMappingForUrlPatterns(null, false, "/*");
}
}

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,38 @@
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd">
<!-- ============================================================= -->
<!-- Configure the Jetty Server instance with an ID "Server" -->
<!-- by adding a HTTP connector. -->
<!-- This configuration must be used in conjunction with jetty.xml -->
<!-- ============================================================= -->
<Configure id="Server" class="org.eclipse.jetty.server.Server">
<!-- =========================================================== -->
<!-- Add a HTTP Connector. -->
<!-- Configure an o.e.j.server.ServerConnector with a single -->
<!-- HttpConnectionFactory instance using the common httpConfig -->
<!-- instance defined in jetty.xml -->
<!-- -->
<!-- Consult the javadoc of o.e.j.server.ServerConnector and -->
<!-- o.e.j.server.HttpConnectionFactory for all configuration -->
<!-- that may be set here. -->
<!-- =========================================================== -->
<Call name="addConnector">
<Arg>
<New class="org.eclipse.jetty.server.ServerConnector">
<Arg name="server"><Ref refid="Server" /></Arg>
<Arg name="factories">
<Array type="org.eclipse.jetty.server.ConnectionFactory">
<Item>
<New class="org.eclipse.jetty.server.HttpConnectionFactory">
<Arg name="config"><Ref refid="httpConfig" /></Arg>
</New>
</Item>
</Array>
</Arg>
<Set name="host"><Property name="jetty.host" /></Set>
<Set name="port"><Property name="jetty.port" default="8080" /></Set>
<Set name="idleTimeout"><Property name="http.timeout" default="30000"/></Set>
</New>
</Arg>
</Call>
</Configure>

View File

@ -0,0 +1,45 @@
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd">
<!-- ============================================================= -->
<!-- Configure a HTTPS connector. -->
<!-- This configuration must be used in conjunction with jetty.xml -->
<!-- and jetty-ssl.xml. -->
<!-- ============================================================= -->
<Configure id="Server" class="org.eclipse.jetty.server.Server">
<!-- =========================================================== -->
<!-- Add a HTTPS Connector. -->
<!-- Configure an o.e.j.server.ServerConnector with connection -->
<!-- factories for TLS (aka SSL) and HTTP to provide HTTPS. -->
<!-- All accepted TLS connections are wired to a HTTP connection.-->
<!-- -->
<!-- Consult the javadoc of o.e.j.server.ServerConnector, -->
<!-- o.e.j.server.SslConnectionFactory and -->
<!-- o.e.j.server.HttpConnectionFactory for all configuration -->
<!-- that may be set here. -->
<!-- =========================================================== -->
<Call id="httpsConnector" name="addConnector">
<Arg>
<New class="org.eclipse.jetty.server.ServerConnector">
<Arg name="server"><Ref refid="Server" /></Arg>
<Arg name="factories">
<Array type="org.eclipse.jetty.server.ConnectionFactory">
<Item>
<New class="org.eclipse.jetty.server.SslConnectionFactory">
<Arg name="next">http/1.1</Arg>
<Arg name="sslContextFactory"><Ref refid="sslContextFactory"/></Arg>
</New>
</Item>
<Item>
<New class="org.eclipse.jetty.server.HttpConnectionFactory">
<Arg name="config"><Ref refid="sslHttpConfig"/></Arg>
</New>
</Item>
</Array>
</Arg>
<Set name="host"><Property name="jetty.host" /></Set>
<Set name="port"><Property name="jetty.https.port" default="8443" /></Set>
<Set name="idleTimeout">30000</Set>
</New>
</Arg>
</Call>
</Configure>

View File

@ -0,0 +1,36 @@
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd">
<!-- ============================================================= -->
<!-- Configure a TLS (SSL) Context Factory                         -->
<!-- This configuration must be used in conjunction with jetty.xml -->
<!-- and either jetty-https.xml or jetty-spdy.xml (but not both)   -->
<!-- ============================================================= -->
<Configure id="sslContextFactory" class="org.eclipse.jetty.util.ssl.SslContextFactory">
<Set name="KeyStorePath"><Property name="maven.project.build.directory.test-classes" default="." />/<Property name="jetty.keystore" default="keystore"/></Set>
<Set name="KeyStorePassword"><Property name="jetty.keystore.password" default="wicket"/></Set>
<Set name="KeyManagerPassword"><Property name="jetty.keymanager.password" default="wicket"/></Set>
<Set name="EndpointIdentificationAlgorithm"></Set>
<Set name="ExcludeCipherSuites">
<Array type="String">
<Item>SSL_RSA_WITH_DES_CBC_SHA</Item>
<Item>SSL_DHE_RSA_WITH_DES_CBC_SHA</Item>
<Item>SSL_DHE_DSS_WITH_DES_CBC_SHA</Item>
<Item>SSL_RSA_EXPORT_WITH_RC4_40_MD5</Item>
<Item>SSL_RSA_EXPORT_WITH_DES40_CBC_SHA</Item>
<Item>SSL_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA</Item>
<Item>SSL_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA</Item>
</Array>
</Set>
<!-- =========================================================== -->
<!-- Create a TLS specific HttpConfiguration based on the -->
<!-- common HttpConfiguration defined in jetty.xml -->
<!-- Add a SecureRequestCustomizer to extract certificate and -->
<!-- session information -->
<!-- =========================================================== -->
<New id="sslHttpConfig" class="org.eclipse.jetty.server.HttpConfiguration">
<Arg><Ref refid="httpConfig"/></Arg>
<Call name="addCustomizer">
<Arg><New class="org.eclipse.jetty.server.SecureRequestCustomizer"/></Arg>
</Call>
</New>
</Configure>

View File

@ -0,0 +1,23 @@
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd">
<!-- ============================================================= -->
<!-- Configure a HTTP connector.                                  -->
<!-- ============================================================= -->
<Configure>
<New id="httpConfig" class="org.eclipse.jetty.server.HttpConfiguration">
<Set name="secureScheme">https</Set>
<Set name="securePort">
<Property name="jetty.secure.port" default="8443" />
</Set>
<Set name="outputBufferSize">32768</Set>
<Set name="requestHeaderSize">8192</Set>
<Set name="responseHeaderSize">8192</Set>
<Set name="sendServerVersion">true</Set>
<Set name="sendDateHeader">false</Set>
<Set name="headerCacheSize">512</Set>
<!-- Uncomment to enable handling of X-Forwarded- style headers <Call name="addCustomizer"> -->
<!-- <Arg><New class="org.eclipse.jetty.server.ForwardedRequestCustomizer"/></Arg> -->
<!-- </Call> -->
</New>
</Configure>

Binary file not shown.