Thursday, March 15, 2007

A complement to the example of Guice struts plugin


A Struts plugin provided by Guice has been my big concern because Sturts is used for web applications commonly. This would be one of the most attractive features in Guice. Today, I tried the counter example included in source code directory and explained in the user's guide. However, it was not easy to get the example to work. The explanation in the user's guide covers princepal parts but is not perfect. Working on the struts plugin example for a couple of hours on netbeans, I finally succeeded in running the Guice example. This is a complement to the example of Guice struts plugin.

All files that must be edited and created to work the counter example are
web.xml, struts.xml, Counter.java, Count.java, Counter.jsp. Each of all is as follows:
  • web.xml
    User's Guide doesn't refer to web.xml, but Counter class uses @SessionScoped annotation. GuiceFilter is necessary.

<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xsi="http://www.w3.org/2001/XMLSchema-instance" schemalocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Struts Guice Test</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter>
<filter-name>guice</filter-name>
<filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>guice</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

  • struts.xml
    The path defined in a result tag doesn't seem to be correct. WEB-INF is not needed.

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.objectFactory" value="guice" />
<package name="strutsguicetest" extends="struts-default">
<action name="Count" class="com.google.inject.struts2.example.Count">
<result>/Counter.jsp</result>
</action>
</package>
</struts>

  • Counter.java
    I just added packge and import declarations

package com.google.inject.struts2.example;

import com.google.inject.servlet.SessionScoped;

@SessionScoped
public class Counter {
private int count = 0;

public synchronized int increment() {
return count++;
}

public Counter() {
}
}

  • Count.java
    I added package and import declarations and modified a static parameter SUCCESS

package com.google.inject.struts2.example;

import com.google.inject.Inject;
import com.opensymphony.xwork2.ActionSupport;

public class Count {
private final Counter counter;

@Inject
public Count(Counter counter) {
this.counter = counter;
}

public String execute() throws Exception {
return ActionSupport.SUCCESS;
}

public int getCount() {
return counter.increment();
}
}

  • Counter.jsp
    I edited netbeans generated jsp file, so nothing has changed in the main part.

<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>JSP Page</h1>
<h2>Counter Example</h2>
<h3>
<b>Hits in this session:</b>
<s:property value="count"/>
</h3>
</body>
</html>

All libraries I set in the libraries directory are guice-1.0.jar, guice-servlet-1.0.jar, guice-struts2-plugin-1.0.jar, struts2-core-2.0.6.jar, xwork-2.0.1.jar, ognl-2.6.11.jar, freemarker-2.3.8.jar, commons-logging-1.0.4.jar, 8 jars in total. A file structure in my strutsguicetes project is shown in the above image.

The URL to make the example to run is http://localhost:8084/strutsguicetest/Count.action. Since netbeans assigns 8084 port to Tomcat 5.5.17, and my project name is strutsguicetest, I tried this URL. When I clicked a reload button, the number shown on my browser increased one by one.

No comments: