Skip to content

Setting up your Webapp

Michael Delamere edited this page Mar 13, 2017 · 29 revisions

For the purpose of this wiki we will be setting up a simple hello-world project and then adding new bits and pieces in subsequent sections. By the end of the tutorial we should have a small, simple CRUD application and hopefully you will have learnt how easy it is to use geeMVC.

If you have not set up the JDK or Maven yet, please follow the instructions on the prerequisites page before starting with this guide.

A good standard way to set up a web-application is to use Maven for building, packaging and resolving dependencies. We will therefore start by creating a simple, typical Maven directory structure.

Basic Maven webapp directory structure

In order to have a more simplistic view of the directory structure we created a normal none-java project here. Before moving on, if you are using eclipse and did the same, it is highly recommended to convert the project into a Maven-Java one.

Below you will find the 2 only files currently existing in the project. Just copy&paste them for now and add them to the locations shown in the screenshot above.

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>

	<name>geeMVC Example Webapp</name>
	<groupId>com.geemvc.example</groupId>
	<artifactId>hello-world-webapp</artifactId>
	<version>1.0-SNAPSHOT</version>
	<packaging>war</packaging>

	<properties>
		<maven.compiler.source>1.8</maven.compiler.source>
		<maven.compiler.target>1.8</maven.compiler.target>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<dependencies>
		<!-- geeMVC Framework -->
		<dependency>
			<groupId>com.geetools.geemvc</groupId>
			<artifactId>geemvc</artifactId>
			<version>0.9.1</version>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>3.1.0</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>javax.servlet.jsp</groupId>
			<artifactId>jsp-api</artifactId>
			<version>2.1</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jstl</artifactId>
			<version>1.2</version>
		</dependency>
	</dependencies>

	<!-- Embedded Jetty Webserver. This will allow us to test our hello-world 
		application without having to install a webserver first. In order to start 
		Jetty, simply execute "mvn jetty:run" on the command-line. Then open up http://localhost:8080/hello/world 
		in your browser. -->
	<build>
		<plugins>
			<plugin>
				<groupId>org.eclipse.jetty</groupId>
				<artifactId>jetty-maven-plugin</artifactId>
				<version>9.3.9.v20160517</version>
				<configuration>
					<scanIntervalSeconds>10</scanIntervalSeconds>
					<webAppConfig>
						<contextPath>/</contextPath>
					</webAppConfig>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

web.xml:

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
		 http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
	version="3.1">

	<display-name>Hello World</display-name>
	<description>
      geeMVC hello-world example webapp.
    </description>

	<servlet-mapping>
		<!-- Let the container handle favicons. -->
		<servlet-name>default</servlet-name>
		<url-pattern>*.ico</url-pattern>
	</servlet-mapping>

	<servlet>
		<!-- The main entry point to geeMVC. -->
		<servlet-name>geeMVC-Servlet</servlet-name>
		<servlet-class>com.geemvc.DispatcherServlet</servlet-class>
		<!-- Tell geeMVC in which folder JSPs or templates reside, relative to 
			the WEB-INF directory. -->
		<init-param>
			<param-name>view-prefix</param-name>
			<param-value>/jsp/pages</param-value>
		</init-param>
		<!-- Specifies the ending of JSP or template files. -->
		<init-param>
			<param-name>view-suffix</param-name>
			<param-value>.jsp</param-value>
		</init-param>
		<!-- Which locales are supported? Used to filter the incoming browser locales. -->
		<init-param>
			<param-name>supported-locales</param-name>
			<param-value>en, de</param-value>
		</init-param>
		<!-- Standard character encoding. Usually you should not need to change 
			this. -->
		<init-param>
			<param-name>default-character-encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
		<!-- What is the default content-type we send back to the client. Usually 
			you should not need to change this. -->
		<init-param>
			<param-name>default-content-type</param-name>
			<param-value>text/html</param-value>
		</init-param>
	</servlet>

	<!-- Map the geeMVC servlet to '/'. -->
	<servlet-mapping>
		<servlet-name>geeMVC-Servlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

</web-app>

Now that we have successfully created our project directory structure, lets move on to creating our first controller.

Clone this wiki locally