You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 3 Next »

Apache Wicket - web site

The tutorial is almost finished. We have to design the web page that we will use to consult the incidents published in the database. The web framework that we will use is Apache Wicket.

Step 1 : Web pages

To display the incidents in a web page, we will create the file HomePage.html in the directory src/main/java/org/apache/camel/example/reportincident. This file contain html tags with some wicket tags. One of the benefit of Apache Wicket compare to other web frameworks is that it try to keep the html page as clean as possible to facilitate the work of the web designer and integration with code made by the developers. It is a component framework like JSF but not based on programmable page like JSP, ...

<html>
<head>
	<title>Report Incident HomePage</title>
	<link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<body>
	<strong>Report Incident HomePage</strong>
	<br/>
	<p/>
	<span wicket:id="message">message will be here</span> (1)
	<p/>
	
	<table cellspacing="0" class="dataview">
    	<tr>
        	<th>Id</th>
        	<th>Incident Date</th>
        	<th>Incident Ref</th>
        	<th>First Name</th>
        	<th>Last Name</th>
        	<th>Summary</th>
        	<th>Details</th>
        	<th>Email</th>
        	<th>Phone</th>
        	<th>Origin</th>
        	<th>Creation date</th>
    	</tr>
    	<tr>
        	<td><span wicket:id="incidentId">[incidentId]</span></td> (2)
        	<td><span wicket:id="incidentDate">[incidentDate]</span></td>
        	<td><span wicket:id="incidentRef">[incidentRef]</span></td>
        	<td><span wicket:id="givenName">[givenName]</span> </td>
       	 	<td><span wicket:id="familyName">[familyName]</span></td>
        	<td><span wicket:id="summary">[summary]</span></td>
        	<td><span wicket:id="details">[details]</span></td>
        	<td><span wicket:id="email">[email]</span></td>
        	<td><span wicket:id="phone">[phone]</span></td>
        	<td><span wicket:id="creationUser">[creationUser]</span></td>
        	<td><span wicket:id="creationDate">[creationDate]</span></td>
    	</tr>
	</table>

</body>
</html>

Remarks :
(1) - The tag wicket:id="message" is used to display top of the screen a message
(2) - The tags <td> are enriched with parameter wicket:id="" to allow to the wicket code page to replace this property with the property field of the Incident class.

Step 2 : Web page code

HomePage.java

/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.camel.example.reportincident;

import java.util.Iterator;
import org.apache.camel.example.reportincident.model.Incident;
import org.apache.camel.example.reportincident.service.IncidentService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.wicket.AttributeModifier;
import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.markup.repeater.data.IDataProvider;
import org.apache.wicket.model.AbstractReadOnlyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.spring.injection.annot.SpringBean;

/**
 * Homepage
 */
public class HomePage extends WebPage {

	private static final long serialVersionUID = 1L;

	private static final transient Log LOG = LogFactory.getLog(HomePage.class);

	@SpringBean
	private IncidentService incidentService;

	/**
	 * Constructor that is invoked when page is invoked without a session.
	 * 
	 * @param parameters
	 *            Page parameters
	 */
	public HomePage(final PageParameters parameters) {

		LOG.debug("Spring service : " + incidentService.toString());

		// Add the simplest type of label
		add(new Label("message", "List of incidents coming from web services or file : "));

		// Add paging
		DataView dataView = new DataView("pageable", new IncidentProvider()) {

			@Override
			protected void populateItem(final Item item) {
				Incident incident = (Incident) item.getModelObject();
				item.add(new Label("incidentId", String.valueOf(incident
						.getIncidentId())));
				item.add(new Label("incidentDate", String.valueOf(incident
						.getIncidentDate())));
				item.add(new Label("incidentRef", incident.getIncidentRef()));
				item.add(new Label("givenName", incident.getGivenName()));
				item.add(new Label("familyName", incident.getFamilyName()));
				item.add(new Label("summary", incident.getSummary()));
				item.add(new Label("details", incident.getDetails()));
				item.add(new Label("email", incident.getEmail()));
				item.add(new Label("phone", incident.getPhone()));
				item.add(new Label("creationUser", incident.getCreationUser()));
				item.add(new Label("creationDate", String.valueOf(incident
						.getCreationDate())));

				item.add(new AttributeModifier("class", true,
						new AbstractReadOnlyModel() {
							@Override
							public Object getObject() {
								return (item.getIndex() % 2 == 1) ? "even"
										: "odd";
							}
						}));
			}
		};
		
        // dataView.setItemsPerPage(20);
        add(dataView);

        // add(new PagingNavigator("navigator", dataView));

	}

	private class IncidentProvider implements IDataProvider {

		public Iterator iterator(int first, int count) {
			return incidentService.findIncident().iterator();
		}

		public int size() {
			return incidentService.findIncident().size();
		}

		public IModel model(Object object) {
			return new Model((Incident) object);
		}

		public void detach() {
			// TODO Auto-generated method stub

		}
	}

	private class IncidentDetachModel extends LoadableDetachableModel {

		private long id;

		@Override
		protected Object load() {
			return incidentService.findIncident(String.valueOf(id));
		}

		/**
		 * @param c
		 */
		public IncidentDetachModel(Incident i) {
			this(i.getIncidentId());
		}

		public IncidentDetachModel(long id) {

			if (id == 0) {
				throw new IllegalArgumentException();
			}
			this.id = id;
		}

	}

}

WicketApplication.java

package org.apache.camel.example.reportincident;

import org.apache.wicket.protocol.http.WebApplication;
import org.apache.wicket.spring.injection.annot.SpringComponentInjector;

/**
 * Application object for your web application. If you want to run this application without deploying, run the Start class.
 * 
 * @see org.apache.wicket.example.Start#main(String[])
 */
public class WicketApplication extends WebApplication
{    
	
	/**
	 * Init
	 */
    public void init() {
        super.init();
        addComponentInstantiationListener(new SpringComponentInjector(this));
    }

	
    /**
     * Constructor
     */
	public WicketApplication()
	{
	}
	
	/**
	 * @see org.apache.wicket.Application#getHomePage()
	 */
	public Class<HomePage> getHomePage()
	{
		return HomePage.class;
	}

}

Step 3 : web.xml configuration

<?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>reportincident.web</display-name>

	<context-param>
		<param-name>contextClass</param-name>
		<param-value>org.springframework.osgi.web.context.support.OsgiBundleXmlWebApplicationContext</param-value>
	</context-param>

	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<filter>
		<filter-name>camel.example.reportincident.web</filter-name>
		<filter-class>org.apache.wicket.protocol.http.WicketFilter</filter-class>
		<init-param>
				<param-name>applicationClassName</param-name>
				<param-value>org.apache.camel.example.reportincident.WicketApplication</param-value>
				<param-name>applicationFactoryClassName</param-name>
				<param-value>org.apache.wicket.spring.SpringWebApplicationFactory</param-value>
    	</init-param>
	</filter>
	<filter-mapping>
		<filter-name>camel.example.reportincident.web</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>


</web-app>

Step 4 : Add spring stuffs

<?xml version="1.0"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:osgi="http://www.springframework.org/schema/osgi"
xsi:schemaLocation="http://www.springframework.org/schema/beans
                    http://www.springframework.org/schema/beans/spring-beans.xsd
                    http://www.springframework.org/schema/osgi
                    http://www.springframework.org/schema/osgi/spring-osgi.xsd">
                    
 <osgi:reference id="incidentService" interface="org.apache.camel.example.reportincident.service.IncidentService"/>
 
</beans>

Step 4 : Adapt the pom.xml file

 

Package the application

To simplify our deployment procedure, we will use the provisioning mechanism of Apache Servicemix called 'Feature'. Using a feature file, we will define the bundles that we will package and their dependencies.

Deploy

Test it

Conclusion

TODO

#Resources

  • No labels