Java/Wicket: Compile Basic Hello World with Resources
Asked Answered
I

3

8

I am following this example of a Hello World Wicket application

https://www.ibm.com/developerworks/web/library/wa-aj-wicket/

In particular I placed HelloWorld.html in my source directory next to HelloWorld.java.

My file structure looks like this:

$ tree
.
├── pom.xml
├── src
│   ├── main
│   │   ├── java
│   │   │   └── com
│   │   │       └── example
│   │   │           └── wicket
│   │   │               ├── HelloWorld.html
│   │   │               ├── HelloWorld.java
│   │   │               └── HelloWorldApplication.java
│   │   ├── resources
│   │   └── webapp
│   │       └── WEB-INF
│   │           └── web.xml
│   └── test
│       └── java
└── wicketTest.iml 

However when I compile this to a war file, and load in Jetty, i recieve this error, in the browser:

Unexpected RuntimeException

Last cause: Can not determine Markup. Component is not yet connected to a parent. [Page class = com.example.wicket.HelloWorld, id = 4, render count = 1]

Stacktrace

Root cause:

org.apache.wicket.markup.MarkupNotFoundException: Can not determine Markup. Component is not yet connected to a parent. [Page class = com.example.wicket.HelloWorld, id = 4, render count = 1]
     at org.apache.wicket.Component.getMarkup(Component.java:737)
     at org.apache.wicket.Component.internalRender(Component.java:2344)
     at org.apache.wicket.Component.render(Component.java:2307)
     at org.apache.wicket.Page.renderPage(Page.java:1010) 

When I look in the war file I notice that the html file is missing:

$ tar tvf target/wicketTest-1.0-SNAPSHOT.war
drwxrwxrwx  0 0      0           0 Aug 22 14:50 META-INF/
-rwxrwxrwx  0 0      0         128 Aug 22 14:50 META-INF/MANIFEST.MF
drwxrwxrwx  0 0      0           0 Aug 22 14:50 WEB-INF/
drwxrwxrwx  0 0      0           0 Aug 22 14:50 WEB-INF/classes/
drwxrwxrwx  0 0      0           0 Aug 22 14:50 WEB-INF/classes/com/
drwxrwxrwx  0 0      0           0 Aug 22 14:50 WEB-INF/classes/com/example/
drwxrwxrwx  0 0      0           0 Aug 22 14:50 WEB-INF/classes/com/example/wicket/
drwxrwxrwx  0 0      0           0 Aug 22 14:50 WEB-INF/lib/
-rwxrwxrwx  0 0      0         608 Aug 22 14:50 WEB-INF/classes/com/example/wicket/HelloWorld.class
-rwxrwxrwx  0 0      0         551 Aug 22 14:50 WEB-INF/classes/com/example/wicket/HelloWorldApplication.class
-rwxrwxrwx  0 0      0       25962 Aug 21 16:07 WEB-INF/lib/slf4j-api-1.6.4.jar
-rwxrwxrwx  0 0      0     2126440 Aug 21 16:07 WEB-INF/lib/wicket-core-6.10.0.jar
-rwxrwxrwx  0 0      0       86671 Aug 21 16:07 WEB-INF/lib/wicket-request-6.10.0.jar
-rwxrwxrwx  0 0      0      415858 Aug 21 16:07 WEB-INF/lib/wicket-util-6.10.0.jar
-rwxrwxrwx  0 0      0         690 Aug 22 13:22 WEB-INF/web.xml
drwxrwxrwx  0 0      0           0 Aug 22 14:50 META-INF/maven/
drwxrwxrwx  0 0      0           0 Aug 22 14:50 META-INF/maven/wicketTest/
drwxrwxrwx  0 0      0           0 Aug 22 14:50 META-INF/maven/wicketTest/wicketTest/
-rwxrwxrwx  0 0      0         675 Aug 22 08:52 META-INF/maven/wicketTest/wicketTest/pom.xml
-rwxrwxrwx  0 0      0         112 Aug 22 14:50 META-INF/maven/wicketTest/wicketTest/pom.properties

How do I specify in my POM file to include the html file?

My POM right now is minimal:

<?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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>wicketTest</groupId>
    <artifactId>wicketTest</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <dependencies>
        <dependency>
            <groupId>org.apache.wicket</groupId>
            <artifactId>wicket-core</artifactId>
            <version>6.10.0</version>
        </dependency>
    </dependencies>
</project>
Indecision answered 22/8, 2013 at 21:53 Comment(2)
Why not just use the quickstart generator available from wicket.apache.org/start/quickstart.html? You are then one paste away from a working application. At least you can diff the pom.xml from the quick start with the pom.xml you crafted.Counterproductive
Thanks, that got me the solution as you mentioned. Posted below.Indecision
I
20

The solution, if you want your HTML in the wicket best practice place (with your classes) is to add this to the build section of your pom.

<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>
</build>
</project>
Indecision answered 22/8, 2013 at 23:49 Comment(1)
good explanation. Why did you deactivate filtering for src/main/resources ?Celie
R
1

If using Maven, see David Williams' answer. If using Gradle, see this answer.

Include the following in your build.gradle file:

sourceSets {
    main {
        resources {
            srcDirs += ['src/main/java']
            includes = ["**"]
            // or specifically: includes = ["**/*.html"]
        }
    }
}

This ensures that the HTML files will be added to the WAR file.

Roseleeroselia answered 26/2, 2020 at 14:25 Comment(0)
S
0

You should put you HelloWorld.html file into src/main/webapp folder. This way it will be included in the war file

Spellbind answered 22/8, 2013 at 21:56 Comment(4)
No doubt this would work but the answer from David Williams is better since it follows Wicket best practises.Shroudlaid
Which best practices to follow is always a trade off. Mine answer is considering maven best practices.Spellbind
And not just into src/main/webapp directory, but in src/main/webapp/com/example/wicket.Emf
Embedding the HTML inside your war is good for most applications but one of the awesome things about Wicket is that you can control the layout and even the component composition (see createComponent method) at runtime by simply changing you HTML. To do that you want to store your markup outside of your war file to avoid having to rebuild and redeploy your app each time you make a markup change. We've built a large CMS, pagebloom.com, with Wicket using this method.Bushel

© 2022 - 2024 — McMap. All rights reserved.