Archive for January, 2010

31
Jan
10

Collapsible Navigation Bar in Apache Tobago

Typical Web applications contain some form of left navigation bar. This page describes how to create a simple tree like collapsible navigation bar using Apache MyFaces Tobago.

Requirements

At this time please complete a simple Hello World application (previous link) before proceeding…

For the site navigation we will use the tc:tree element to display a collapsible table.

Implementing the Model

The code listed below sets up a static Tree Structure that represents the left navigation bar. Each tree element will specify the key and the “outcome”. The key is used to find the Text to display for that item. And the “outcome” will be used by JSF to identify what page to forward the request to.

package com.test;

import java.util.Enumeration;

import javax.faces.context.FacesContext;
import javax.swing.tree.DefaultMutableTreeNode;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.myfaces.tobago.context.ResourceManagerUtil;
import org.apache.myfaces.tobago.model.TreeState;

/**
 * The following constructs a very basic static navigation bar.
 */
public class Navigation {

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

  private DefaultMutableTreeNode tree;

  private TreeState state;

  public Navigation() {

    tree = new DefaultMutableTreeNode(new Node("test", "Root", null));

    DefaultMutableTreeNode home = new DefaultMutableTreeNode(new Node("test", "home", "pages/home"));
    DefaultMutableTreeNode products = new DefaultMutableTreeNode(new Node("test", "products", "pages/products"));

    DefaultMutableTreeNode portableAudio = new DefaultMutableTreeNode(new Node("test", "portable_audio", "pages/portable_audio"));
    portableAudio.add(new DefaultMutableTreeNode(new Node("test", "cd", "pages/cd")));
    portableAudio.add(new DefaultMutableTreeNode(new Node("test", "mp3", "pages/mp3")));

    DefaultMutableTreeNode books = new DefaultMutableTreeNode(new Node("test", "books", "pages/books"));

    DefaultMutableTreeNode newReleases = new DefaultMutableTreeNode(new Node("test", "new_releases", "pages/new_releases"));

    newReleases.add(new DefaultMutableTreeNode(new Node("test", "action_adventure","pages/action_advendure")));

    books.add(newReleases);

    products.add(portableAudio);
    products.add(books);

    tree.add(home);
    tree.add(products);

    state = new TreeState();
    state.expand(tree, 1);
    state.setMarker(home);
  }

  public String navigate() {
    Node selected = (Node) state.getMarker().getUserObject();
    return selected.getOutcome();
  }

  public void updateMarker(String viewId) {
    Enumeration enumeration = tree.depthFirstEnumeration();
    while (enumeration.hasMoreElements()) {
      DefaultMutableTreeNode maybeMarker = ((DefaultMutableTreeNode) enumeration.nextElement());
      Node node = (Node) maybeMarker.getUserObject();
      if (node.getOutcome() != null && viewId.contains(node.getOutcome())) {
        state.setMarker(maybeMarker);
        break;
      }
    }
  }

  public DefaultMutableTreeNode getTree() {
    return tree;
  }

  public void setTree(DefaultMutableTreeNode tree) {
    this.tree = tree;
  }

  public TreeState getState() {
    return state;
  }

  public void setState(TreeState state) {
    this.state = state;
  }

  public String gotoFirst() {
    DefaultMutableTreeNode first = tree.getNextNode();
    state.setMarker(first);
    return ((Node) first.getUserObject()).getOutcome();
  }

  public String gotoPrevious() {
    DefaultMutableTreeNode previousNode = state.getMarker().getPreviousNode();
    if (previousNode != null) {
      state.setMarker(previousNode);
      return ((Node) previousNode.getUserObject()).getOutcome();
    }
    return null;
  }

    public String gotoNext() {
    DefaultMutableTreeNode nextNode = state.getMarker().getNextNode();
    if (nextNode != null) {
      state.setMarker(nextNode);
      return ((Node) nextNode.getUserObject()).getOutcome();
    }
    return null;
  }

  public boolean isFirst() {
    return state.getMarker().getPreviousNode().isRoot();
  }

  public boolean isLast() {
    return state.getMarker().getNextNode() == null;
  }

  public static class Node {

    private String title;
    private String id;
    private String outcome;

    public Node(String resourceBundle, String key, String outcome) {
      this.title = ResourceManagerUtil.getProperty(
          FacesContext.getCurrentInstance(), resourceBundle, key);
      this.id = key;
      this.outcome = outcome;
    }

    public String getTitle() {
      return title;
    }

    public void setTitle(String title) {
      this.title = title;
    }

    public String getId() {
      return id;
    }

    public void setId(String id) {
      this.id = id;
    }

    public String getOutcome() {
      return outcome;
    }

    public void setOutcome(String outcome) {
      this.outcome = outcome;
    }
  }
}

Create the resource bundle file

need to create a resource bundle file in

src/main/webapp/tobago-resource/html/standard/standard/property/test.properties.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
	<entry key="neeraj_name">Neeraj Verma</entry>
	<entry key="home">Home</entry>
	<entry key="products">Products</entry>
	<entry key="portable_audio">Portable Audio</entry>
	<entry key="cd">CD</entry>
	<entry key="mp3">MP3</entry>
	<entry key="books">Books</entry>
	<entry key="new_releases">New Releases</entry>
	<entry key="action_adventure">Action Adventure</entry>
</properties>

Modify Navigation Rules

The next step is to setup the navigation rules. Make the changes highlighted below to the faces-config.xml file. The navigation rules section describes how to forward the user to the next page when they click on the navigation bar. The navigation bar contains the “outcome” for each entry.

src/main/webapp/WEB-INF/faces-config.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE faces-config PUBLIC
  "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN"
  "http://java.sun.com/dtd/web-facesconfig_1_1.dtd">

<faces-config>

  <application>
    <locale-config>
      <default-locale>en</default-locale>
      <supported-locale>de</supported-locale>
      <supported-locale>de_DE</supported-locale>
      <supported-locale>de_AT</supported-locale>
      <supported-locale>de_CH</supported-locale>
    </locale-config>
  </application>

	<navigation-rule>
		<navigation-case>
			<from-outcome>pages/home</from-outcome>
			<to-view-id>pages/home.jsp</to-view-id>
		</navigation-case>
		<navigation-case>
			<from-outcome>pages/products</from-outcome>
			<to-view-id>pages/products.jsp</to-view-id>
		</navigation-case>
		<navigation-case>
			<from-outcome>pages/portable_audio</from-outcome>
			<to-view-id>pages/portable_audio.jsp</to-view-id>
		</navigation-case>
		<navigation-case>
			<from-outcome>pages/cd</from-outcome>
			<to-view-id>pages/cd.jsp</to-view-id>
		</navigation-case>
	</navigation-rule>

  <managed-bean>
    <managed-bean-name>navigation</managed-bean-name>
    <managed-bean-class>com.test.Navigation</managed-bean-class>
    <managed-bean-scope>session</managed-bean-scope>
  </managed-bean>
</faces-config>

Implement the Target Pages

As you can see above each “outcome” leads to a different jsp page. For that reason please take the time to create the 4 jsp’s listed above

  1. src/main/webapp/pages/home.jsp
  2. src/main/webapp/pages/products.jsp
  3. src/main/webapp/pages/portable_audio.jsp
  4. src/main/webapp/pages/cd.jsp

You can put anything you want in those JSP’s “Test 123 [jsp name]” whatever…

Implement the page

Modify the helloWorld.jsp page to look like this: The lines highlighted below are the differences from the file posted in my previous article.

<%@ taglib uri="http://myfaces.apache.org/tobago/component" prefix="tc" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<tc:loadBundle basename="test" var="testBundle"/>

<f:view>
  <tc:page>
    <f:facet name="layout">
      <tc:gridLayout/>
    </f:facet>
    <tc:tree value="#{navigation.tree}"
             mode="menu"
             id="nav"
             nameReference="userObject.title"
             idReference="userObject.id"
             tipReference="userObject.title"
             state="#{navigation.state}"
             showIcons="false"
             showJunctions="false"
             showRoot="false">
      <f:facet name="treeNodeCommand">
        <tc:link action="#{navigation.navigate}" immediate="true"/>
      </f:facet>
    </tc:tree>

  </tc:page>
</f:view>

Test the Application

Start the application in jetty.

mvn jetty:run

Navigate to http://localhost:8080/

You should see a navigation menu smack in top of the html page. It will most likely take up the whole browser window. Next we need to create a layout and place this navigation bar on the left of the page. Please read my next article where I describe how this is done.

That’s All for now…

31
Jan
10

Page Layout with Apache MyFaces Tobago

This page describes the process of setting up a reusable layout for your web application.

Requirements

This page will continue where we left off. Therefore if you have not done so already please follow the instructions to create a simple hello world application + navigation bar.

In this page we will create a banner and footer pages. We will take the navigation bar and place it on the left side of the page. In the body section we will display a simple calendar component.

Tag File

src/main/webapp/WEB-INF/tags/layout/overview.tag

<%@ taglib uri="http://myfaces.apache.org/tobago/component" prefix="tc" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<f:view >
  <tc:page applicationIcon="icon/favicon.ico" label="Test Page Title" id="page" width="1000px" height="750px">

    <jsp:include page="/menu.jsp" />

    <f:facet name="layout">
      <tc:gridLayout border="0" columns="*;4*"
        margin="10px" rows="100px;*;fixed"  />
    </f:facet>

    <tc:cell spanX="2">
      <jsp:include page="/header.jsp"/>
    </tc:cell>

    <tc:cell spanY="2" >
      <jsp:include page="/navigation.jsp"/>
    </tc:cell>

    <tc:cell>
      <jsp:doBody/>
    </tc:cell>

    <tc:cell>
      <jsp:include page="/footer.jsp" />
    </tc:cell>

  </tc:page>
</f:view>

Header

src/main/webapp/header.jsp

<%@ taglib uri="http://myfaces.apache.org/tobago/component" prefix="tc" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<f:subview id="header" >

  <tc:panel>
    <tc:out value="This is the header..."/>
  </tc:panel>
</f:subview>

Menu

src/main/webapp/menu.jsp

<%@ taglib uri="http://myfaces.apache.org/tobago/component" prefix="tc" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<f:subview id="menu" >
<!-- This is a place holder for the top menu -->
</f:subview>

Footer

src/main/webapp/footer.jsp

<%@ taglib uri="http://myfaces.apache.org/tobago/component" prefix="tc" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<f:subview id="footer" >

  <tc:panel>
    <tc:out value="This is the footer..."/>
  </tc:panel>
</f:subview>

Navigation

src/main/webapp/navigation.jsp

<%@ taglib uri="http://myfaces.apache.org/tobago/component" prefix="tc" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<f:subview id="navigation" >

  <tc:panel>
		<tc:box label="Navigation">
			<tc:tree value="#{navigation.tree}" mode="menu" id="nav"
				nameReference="userObject.title" idReference="userObject.id"
				tipReference="userObject.title" state="#{navigation.state}"
				showIcons="false" showJunctions="false" showRoot="false">
				<f:facet name="treeNodeCommand">
					<tc:link action="#{navigation.navigate}" immediate="true" />
				</f:facet>
			</tc:tree>
		</tc:box>
  </tc:panel>
</f:subview>

Actual Page

This page uses the layout we defined above. It specifies only the body of the page. This is typically where we will put the components that interact with the user.
src/main/webapp/helloLayout.jsp

<%@ taglib uri="http://myfaces.apache.org/tobago/component" prefix="tc" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%@ taglib tagdir="/WEB-INF/tags/layout" prefix="layout" %>
<%@ taglib uri="http://myfaces.apache.org/tobago/extension" prefix="tx" %>

<layout:overview>
  <jsp:body>
  	<tc:out value="This is the main body..."/>
  	<tc:panel >
  		<f:facet name="layout">
  			<tc:gridLayout rows="fixed;*" columns="fixed;*"/>
  		</f:facet>
  	<tc:box label="calendar component" height="225" id="box">
  		<tc:calendar />
  	</tc:box>
  	<tc:cell id="abc"/>
  	<tc:cell id="abcd"/>
  	</tc:panel>
  </jsp:body>
</layout:overview>

Test the application

Start jetty and test the application by navigating to:

http://localhost:8080/tobagoTestWeb/faces/helloLayout.jsp

That’s all for now…

31
Jan
10

Using Resource Bundles with Apache Tobago

This page describes the steps necessary to use resource bundles in your Apache Tobago based applications.

This page builds upon the Hello World Application and modifies it to use resource bundles to display messages. If you have not done so already please click on the above link to complete the hello world application before proceeding.

To allow tobago to use property files add the following to:

src/main/webapp/WEB-INF/tobago-config.xml

<resource-dir>tobago-resource</resource-dir>

The above specifies that the tobago-resource directory contains files used for application display. The resource directory follows a standard directory structure. This allow for customization for different themes and locales.

src/main/webapp/tobago-resource/html/standard/standard/property contains the properties files.

add the following
src/main/webapp/tobago-resource/html/standard/standard/property/test.properties.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
 <entry key="neeraj_name">Neeraj Verma</entry>
 </properties>

src/main/webapp/hello.jsp should look like this. The lines highlighted below indicate where the changes were made.

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%@ taglib uri="http://myfaces.apache.org/tobago/component" prefix="tc" %>
<%@ taglib uri="http://myfaces.apache.org/tobago/extension" prefix="tx" %>

<tc:loadBundle basename="test" var="testBundle"/>  

<f:view>
  <tc:page>
    <f:facet name="layout">
      <tc:gridLayout/>
    </f:facet>
    <tc:out value="Hello World"/>
    <tc:out escape="false" value="#{testBundle.neeraj_name}" />
  </tc:page>
</f:view>

Save and run the page in jetty

mvn jetty:run

Navigate to http://localhost:8080/

You should see hello world up top and somewhere in the middle you will see my name.

That’s all for now.

31
Jan
10

Restarting Jetty Automatically using scanTargets

This page describes the process of setting up jetty such that it will restart when resources are changed.

By default jetty only scans a limited set of directories for changes. If you change a jsp or property file chances are that the changes wont be registered. If you find this happening then try something like this in your maven pom.xml file.

<plugin>
	<groupId>org.mortbay.jetty</groupId>
	<artifactId>maven-jetty-plugin</artifactId>
	<configuration>
		<scanTargetPatterns>
			<scanTargetPattern>
				<directory>src/main/webapp</directory>
				<includes>
					<include>**/*</include>
				</includes>
			</scanTargetPattern>
		</scanTargetPatterns>
		<scanIntervalSeconds>2</scanIntervalSeconds>
	</configuration>
</plugin>

28
Jan
10

Hello World with Apache Tobago

This page describes the process of setting up and running a Hello World Application using Apache MyFaces Tobago. This is similar to the blank application that is published by the apache group however unlike the blank application this one is totally independent. It is a standalone application that will run on its own without any complicated parent project relationships. Using this page you should be up and running in about 20 minutes.

Requirements

  • A basic understanding of JSF
  • Maven installed and configured

Overview

The hello world application consists of the following files

  1. pom.xml file that describes the project
  2. web.xml that describes the application
  3. faces-config.xml – very basic faces configuration file
  4. tobago-config.xml – very basic tobago configuration file
  5. hello.jsp – The actual hello world jsp that prints the message at “/faces/hello.jsp”

Generate the project

We start with the pom.xml file.

mvn archetype:generate -DarchetypeArtifactId=maven-archetype-webapp

groupId: com.test
artifactId: tobagoTestWeb
Leave the rest defaults

Import it into Eclipse

Import the project by clicking import existing project into workspace.

Modify the POM.xml file

Once the project is built make sure the poml.xml file looks like this:
I know the pom.xml file is a bit large but “It is what it is…”
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>
	<groupId>test</groupId>
	<artifactId>jsfTestWeb</artifactId>
	<packaging>war</packaging>
	<version>1.0-SNAPSHOT</version>
	<name>jsfTestWeb Maven Webapp</name>
	<url>http://maven.apache.org</url>
	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>

<!-- This is tobago itself + 1 theme -->
		<dependency>
			<groupId>org.apache.myfaces.tobago</groupId>
			<artifactId>tobago-core</artifactId>
			<version>1.0.24</version>
			<exclusions>
				<exclusion>
					<groupId>sun.jdk</groupId>
					<artifactId>tools</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<dependency>
			<groupId>org.apache.myfaces.tobago</groupId>
			<artifactId>tobago-theme-speyside</artifactId>
			<version>1.0.24</version>
		</dependency>

<!-- Start Tobago dependencies -->
		<dependency>
			<groupId>commons-beanutils</groupId>
			<artifactId>commons-beanutils</artifactId>
			<version>1.7.0</version>
		</dependency>
		<dependency>
			<groupId>commons-collections</groupId>
			<artifactId>commons-collections</artifactId>
			<version>3.1</version>
		</dependency>
		<dependency>
			<groupId>commons-digester</groupId>
			<artifactId>commons-digester</artifactId>
			<version>1.8</version>
		</dependency>
		<dependency>
			<groupId>commons-fileupload</groupId>
			<artifactId>commons-fileupload</artifactId>
			<version>1.2</version>
		</dependency>
		<dependency>
			<groupId>commons-io</groupId>
			<artifactId>commons-io</artifactId>
			<version>1.1</version>
		</dependency>
		<dependency>
			<groupId>commons-lang</groupId>
			<artifactId>commons-lang</artifactId>
			<version>2.1</version>
		</dependency>
		<dependency>
			<groupId>commons-logging</groupId>
			<artifactId>commons-logging</artifactId>
			<version>1.1</version>
		</dependency>
<!-- End Tobago dependencies -->
<!-- Start JSTL -->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jstl</artifactId>
			<version>1.1.2</version>
		</dependency>
		<dependency>
			<groupId>taglibs</groupId>
			<artifactId>standard</artifactId>
			<version>1.1.2</version>
		</dependency>
<!-- End JSTL -->

<!-- Start JSF and MyFaces -->
		<dependency>
			<groupId>org.apache.myfaces.core</groupId>
			<artifactId>myfaces-api</artifactId>
			<version>1.1.7</version>
		</dependency>
		<dependency>
			<groupId>org.apache.myfaces.core</groupId>
			<artifactId>myfaces-impl</artifactId>
			<version>1.1.7</version>
		</dependency>
<!-- End JSF and MyFaces -->

	</dependencies>
	<build>
		<finalName>jsfTestWeb</finalName>
		<plugins>
			<plugin>
				<groupId>org.mortbay.jetty</groupId>
				<artifactId>jetty-maven-plugin</artifactId>
				<version>7.0.0.v20091005</version>
				<configuration>
					<scanIntervalSeconds>2</scanIntervalSeconds>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

web.xml

that describes the application
src/main/webapp/WEB-INF/web.xml

<?xml version="1.0" encoding="UTF-8"?>
<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>Hello World Tobago Application</display-name>
  <context-param>
    <param-name>javax.faces.PARTIAL_STATE_SAVING_METHOD</param-name>
    <param-value>false</param-value>
  </context-param>  

  <filter>
    <filter-name>multipartFormdataFilter</filter-name>
    <filter-class>org.apache.myfaces.tobago.webapp.TobagoMultipartFormdataFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>multipartFormdataFilter</filter-name>
    <url-pattern>/faces/*</url-pattern>
  </filter-mapping>

<!--  workaround (e.g. for Oracle AS 10.1.2.0.0)-->
  <listener>
    <listener-class>org.apache.myfaces.tobago.webapp.TobagoServletContextListener</listener-class>
  </listener>

  <!-- servlet -->

  <servlet>
    <servlet-name>FacesServlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>3</load-on-startup>
  </servlet>

  <servlet>
    <servlet-name>ResourceServlet</servlet-name>
    <servlet-class>org.apache.myfaces.tobago.servlet.ResourceServlet</servlet-class>
  </servlet>

  <!-- servlet-mapping -->

  <servlet-mapping>
    <servlet-name>FacesServlet</servlet-name>
    <url-pattern>/faces/*</url-pattern>
  </servlet-mapping>

  <servlet-mapping>
    <servlet-name>ResourceServlet</servlet-name>
    <url-pattern>/org/apache/myfaces/tobago/renderkit/*</url-pattern>
  </servlet-mapping>

  <!-- The Usual Welcome File List -->
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>

</web-app>

faces-config.xml

very basic faces configuration file
src/main/webapp/WEB-INF/faces-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE faces-config PUBLIC
  "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN"
  "http://java.sun.com/dtd/web-facesconfig_1_1.dtd">

<faces-config>
  <application>
    <locale-config>
      <default-locale>en</default-locale>
      <supported-locale>de</supported-locale>
      <supported-locale>de_DE</supported-locale>
      <supported-locale>de_AT</supported-locale>
      <supported-locale>de_CH</supported-locale>
    </locale-config>
  </application>
</faces-config>

tobago-config.xml

src/main/webapp/WEB-INF/tobago-config.xml
very basic tobago configuration file

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE tobago-config PUBLIC
    "-//The Apache Software Foundation//DTD Tobago Config 1.0//EN" "tobago-config_1_0.dtd">

<tobago-config>
  <theme-config>
    <default-theme>speyside</default-theme>
  </theme-config>
</tobago-config>

hello.jsp

And Lastly the most important file in the whole application… The actual hello world jsp that prints the message when you navigate to: “/faces/hello.jsp”
src/main/webapp/hello.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%@ taglib uri="http://myfaces.apache.org/tobago/component" prefix="tc" %>
<%@ taglib uri="http://myfaces.apache.org/tobago/extension" prefix="tx" %>
<f:view>
  <tc:page>
    <f:facet name="layout">
      <tc:gridLayout/>
    </f:facet>
    <tc:out value="Hello World"/>
  </tc:page>
</f:view>

index.jsp

src/main/webapp/index.jsp

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>Your Page Title</title>
<meta http-equiv="REFRESH" content="0;url=<c:url value='/faces/hello.jsp'/>"></HEAD>
<BODY>
Optional page text here.
</BODY>
</HTML>

Run the Application

At this time you can run the application in Jetty.

mvn jetty:run

navigate to http://localhost:8080/

Limitations

  • JSF only supports one renderkit (renderkitId) per page (f:view). Because Tobago has it’s own renderkitId you cannot use any non-Tobago components that need a renderer. This means that you will not be able to use tomahawk components on tobago pages.

That’s all for now…

28
Jan
10

Using Tag Files with JSP 2.1 Servlet 2.5

This page is about using Tag files to create custom tags to replace the functionality provided by Tiles2.

Background

Tiles2 framework allow developers to define the layout of the web application in one place. It reduces the amount of duplicate code that is typically found for left, top navigation bars and footers. However in some cases Tile2 can be overkill.

Instead, simple layouts can be accomplished by using jsp tag files.

Requirements

  • Maven – installed and configured

Start a new Maven Project

mvn archetype:generate -DarchetypeArtifactId=maven-archetype-webapp

For the group id enter: com.test
For the artifactId enter: tagTestWeb

Answer the rest of the questions using defaults [Hit Enter].

cd to the project’s folder

Make your pom.xml file look like the one below. (replace it with this one if its easier for you)

<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.test</groupId>
  <artifactId>tagTestWeb</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>tagTestWeb Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    <version>1.1.2</version>
</dependency>
<dependency>
    <groupId>taglibs</groupId>
    <artifactId>standard</artifactId>
    <version>1.1.2</version>
</dependency>
    <dependency>
  <groupId>org.apache.tiles</groupId>
  <artifactId>tiles-jsp</artifactId>
  <version>2.1.4</version>
</dependency>
  </dependencies>
  <build>
    <finalName>tagTestWeb</finalName>
<plugins>
	<plugin>
		<groupId>org.apache.maven.plugins</groupId>
		<artifactId>maven-compiler-plugin</artifactId>
		<version>2.0.2</version>
		<configuration>
			<source>1.6</source>
			<target>1.6</target>
		</configuration>
	</plugin>

	<plugin>
		<groupId>org.mortbay.jetty</groupId>
		<artifactId>jetty-maven-plugin</artifactId>
				<version>7.0.0.v20091005</version>
		<configuration>
			<scanIntervalSeconds>2</scanIntervalSeconds>
		</configuration>
	</plugin>
</plugins>
  </build>
</project>

In the JSP 2.1 and Servlet 2.5 environment you can simply create custom tags by put the tag file into the tags folder under WEB-INF.

Create the directory that will hold the tag files.
mkdir src/main/webapp/WEB-INF/tags

Below we have created a simple tag that prints out a table with a title and some contents in the body.
vi src/main/webapp/WEB-INF/tags/box.tag

<%@ attribute name="color" required="true" rtexprvalue="false" %>
<%@ attribute name="title" required="true" rtexprvalue="false" %>
<table width="320" bordercolor="${color}" border="1" cellpadding="4" cellspacing="0">
	<tr bgcolor="${color}" color="#ffffff">
		<td class="boxHeader" nowrap>
			${title}
		</td>
	</tr>
	<tr>
		<td valign="top" class="boxText">
			<jsp:doBody/>
		</td>
	</tr>
</table>

The code below is another tag that will be included in the first tag. This will demonstrate the capabilities of including on tag inside the body of another.

src/main/webapp/WEB-INF/tags/subbox.tag

<%@ attribute name="color" required="true" rtexprvalue="false" %>
<%@ attribute name="title" required="true" rtexprvalue="false" %>
<table width="320" bordercolor="${color}" border="1" cellpadding="4" cellspacing="0">
	<tr bgcolor="${color}" color="#ffffff">
		<td class="boxHeader" nowrap>
			${title}
		</td>
	</tr>
	<tr>
		<td valign="top" class="boxText">
			<jsp:doBody/>
		</td>
	</tr>
</table>

Put the following into a jsp

src/main/webapp/index.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<%@ taglib prefix="tags" tagdir="/WEB-INF/tags" %>
<html>
<body>
<h2>Hello World!</h2>
<tags:box>
	<jsp:attribute name="color">#314289</jsp:attribute>
	<jsp:attribute name="title">Testing 1234 Title</jsp:attribute>
	<jsp:body>
		This is the body of the text.
	</jsp:body>
</tags:box>
<br/>
<tags:box color="#314289" title="output from c:out tag">
	<jsp:body>
		<c:out value="output from c:out tag"/>
	</jsp:body>
</tags:box>
<br/>
<tags:box color="#314289" title="output from c:out tag">
	<jsp:body>
		<tags:subbox color="#314289" title="this is a sub box">
			<jsp:body>
				<c:out value="this is a sub box"/>
			</jsp:body>
		</tags:subbox>
	</jsp:body>
</tags:box>
</body>
</html>

Run the application

cd to the project base folder.
Run the following at the command prompt.

mvn jetty:run

Navigate to the url: http://localhost:8080/index.jsp

Including multiple sections (JSP Fragments)

Please see the comment from Michael Breed below about including multiple JSP fragments using tag files.

24
Jan
10

Setting Environment Variables in Ubuntu

This page describes the process of setting up environment variables in Ubuntu.

Session-wide environment variables

In order to set environment variables in a way that affects a user’s entire desktop session, one may place commands to set their values in one of the “hidden” script files in the user’s home directory. The more common such files are outlined below.

  • ~/.profile – This is probably the best file for placing environment variable assignments in, since it gets executed automatically by the Display Manager during the startup process desktop session as well as by the login shell when one logs-in from the textual console.
  • ~/.bash_profile or ~./bash_login – If one of these file exist, bash executes it rather then “~/.profile” when it is started as a login shell. (Bash will prefer “~/.bash_profile” to “~/.bash_login”). However, these files won’t influence a graphical session by default.
  • ~/.bashrc – Because of the way Ubuntu currently sets up the various script files by default, this may be the easiest place to set variables in. The default configuration nearly guarantees that this file will be executed in each and every invocation of bash as well as while logging in to the graphical environment. However, performance-wise this may not be the best thing to do since it will cause values to be unnecessarily set many times.

System-wide environment variables

Environment variable settings that affect the system as a whole (rather then just a particular user’s desktop session) can be placed in any of the many system-level scripts that get executed when the system or the desktop session are loaded. Ubuntu defines several locations dedicated to placing such settings:

  • /etc/profile – This file gets executed whenever a bash login shell is entered (e.g. when logging in from the console or over ssh), as well well as by the DisplayManager when the desktop session loads. This is probably the file you will get referred to when asking veteran UNIX system administrators about environment variables. In Ubuntu, however, this file does little more then invoke the /etc/bash.bashrc file.
  • /etc/bash.bashrc – This is is the system-wide version of the ~/.bashrc file. Ubuntu is configured by default to execute this file whenever a user enters a shell or the desktop environment.
  • /etc/environment – This file is specifically meant for system-wide environment variable settings. It is not a script file, but rather consists of assignment expressions, one per line. Specifically, this file stores the system-wide locale and path settings.

The above was taken from the following URL: https://help.ubuntu.com/community/EnvironmentVariables

My thoughts on the above

If you want to change system wide environment variables then /etc/environment is your best bet.

The environment file contains variables that are made effective globally. The following output shows my current environment file.

vi /etc/environment

PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games"

If you want to change session wide then ~/.bashrc or ~/.pam_environment would be your best bet. See the next code block or the comments below.

vi ~/.bashrc

PATH=$PATH:/path_to_where_java_is/bin
export PATH

FreeBSD

To change environment variables globally just change the /etc/login.conf file. and run the command it lists on the top for the changes to take. Also if there are any global variables in your local configuration files. ex. /etc/cshrc.conf or ~/.cshrc then you need to make sure that the files dont clobber any variables from login.conf.

23
Jan
10

Enabling Digest Authentication in Apache

This page describes the process of getting Apache to protect a directory using digest authentication.

Digest Authentication

Using digest authentication, your password is never sent across the network in the clear, but is always transmitted as an MD5 digest of the user’s password. In this way, the password cannot be determined by sniffing network traffic. This is good for when your web clients are not using SSL.

Modules

Digest authentication is implemented by the module mod_auth_digest. Please have that loaded in the httpd.conf

Protecting a Location or Directory

Put the following in the Location or Directory tag…

    AuthType Digest
    AuthName Protected

    # You can use the htdigest program to create the password database:
    # the -c parameter overwrites a file if it exists. Remove it to append.
    #   htdigest -c "/usr/local/user.passwd" Protected myUser
    AuthUserFile "/usr/local/user.passwd"
    AuthDigestProvider file
    require user myUser myUser2 myUser3

Note: You must protect the user file from unauthorized users.

23
Jan
10

Getting Apache to Authenticate Against LDAP

This page describes the process of getting Apache to Authenticate against LDAP server.

Background

Limitations

Currently there is no way to use Digest Authentication against LDAP. However if you will be using SSL this should not be a problem because the username / password would be sent over the ssl channel encrypted.

Procedure

Ensure you have the following modules installed and configured in your apache server…

LoadModule authnz_ldap_module libexec/apache22/mod_authnz_ldap.so
LoadModule ldap_module libexec/apache22/mod_ldap.so

  • ldap_module – LDAP connection pooling and result caching services for use by other LDAP modules
  • authnz_ldap_module – Allows an LDAP directory to be used to store the database for HTTP Basic authentication.

Put the following into the Directory or Location tag in your web server configuration file.
AuthLDAPBindDN and AuthLDAPBindPassword are not required if your server allows anonymous bind when doing the search. When it finds the entry it will perform a bind against it using the user provided password.

AuthType Basic
AuthName LDAP
AuthBasicProvider ldap
Require valid-user
AuthzLDAPAuthoritative on
AuthLDAPBindDN "uid=ldapauthuser,ou=system,dc=yourdomain,dc=com"
AuthLDAPBindPassword "xxx"
AuthLDAPURL "ldap://localhost/ou=people,dc=yourdomain,dc=com"

Protecting Directories Based On Group Membership

There is a way to permission directory locations in Apache based on groups. If the user is a member of a group they are granted access to the location. This can be done by adding “Require ldap-group”. See Highlighted line below.

The following code located inside httpd.conf protects the /svn/group1 repository to only people that are in “cn=group1,ou=svngroups,dc=yourdomain,dc=com” group.

<Location /svn/group1>
DAV svn
SVNPath /var/db/svngroup1
AuthType Basic
AuthName LDAP
AuthBasicProvider ldap
Require valid-user
AuthLDAPBindDN "uid=ldapauthuser,ou=system,dc=yourdomain,dc=com"
AuthLDAPBindPassword "password"
AuthLDAPURL "ldap://localhost/ou=people,dc=yourdomain,dc=com?uid
Require ldap-group cn=group1,ou=svngroups,dc=yourdomain,dc=com
</Location>

References

22
Jan
10

Getting an Authoritative Answer from DNS

This page describes the process of getting an Authoritative answer from DNS.

Typically when you do a DNS lookup you would see something like this…

nslookup yourdomain.com
Server:  some-random.dns.server.com
Address:  xxx.xxx.xxx.xxx

Non-authoritative answer:
Name:    yourdomain.com
Address:  xxx.xx.xxx.xxx

As you can see from the highlighted line that this is a non-authoritative Answer. This means exactly what it says. The answer came from a name server that is not the authoritative source. This can either be from cache or the server had to relay the query another server. Once it obtained the answer it returned it back to you as a non-authoritative answer.

To get an authoritative answer do the following:

  1. Get the name server responsible for your domain
  2. Query the name server directly.

Getting the Name Servers Responsible

nslookup -querytype=NS yourdomain.com
Server:  resolver
Address:1.opendns.com  208.67.222.222

Non-authoritative answer:
yourdomain.net	nameserver = ns51.yourdomain.com
yourdomain.net	nameserver = ns52.yourdomain.com

Query The Name Servers Directly

Pick from one of the name servers that get returned. In my case I picked the second one.

nslookup yourdomain.net ns52.yourdomain.com

This time around you should not be getting a non-authoritative answer.

A better way would be to run the dnsq command.


dnsq mx yourdomain.net ns52.yourdomain.com

Doing a Reverse lookup

In ubuntu you can run the “host” command passing in the hostname or ip address. You can also type host -t MX to find the Mail exchanger record. The type argument value can be any recognized query type: CNAME, NS, SOA, SIG, KEY, AXFR, etc.

example:

prompt> host -t MX silverflix.com
silverflix.com mail is handled by 0 mx.silverflix.com.

Setting up an MX record

In the tinydns configuration file just prefix the record with “@”.




Follow

Get every new post delivered to your Inbox.

Join 34 other followers