Struts 2

Struts 2 and Log4J Example

In this example we will learn how to integrate Struts2 application with Log4j logging framework. Apache Struts is a free, open-source, MVC framework for creating elegant, modern Java web applications.

It favors convention over configuration, is extensible using a plugin architecture, and ships with plugins to support REST, AJAX and JSON.

Log4J is Apache’s popular logging framework.

Tools and technologies used in this example are java 1.7, Maven, Intellij, Log4j-1.2.16, Struts-2.3.15.1, Tomcat-7.0.54

1. Project Structure

Below is the project structure used in this example.

Figure 1. Project Structure
Figure 1. Project Structure

Struts2ExampleAction is the Struts Action class. The resource folder contains the Log4J properties file (log4j.properties) and the Struts configuration file (struts.xml).

2. Maven dependency

Below is the pom file which define the dependencies for Struts 2 and Log4J.

pom.xml

<?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>com.javacodegeeks</groupId>
  <artifactId>struts2-log4j</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <dependencies>
    <dependency>
      <groupId>org.apache.struts</groupId>
      <artifactId>struts2-core</artifactId>
      <version>2.3.15.1</version>
    </dependency>
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.16</version>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
          <source>1.6</source>
          <target>1.6</target>
        </configuration>
      </plugin>
    </plugins>
    <finalName>${project.artifactId}</finalName>
  </build>
</project>

3. web.xml

Below in the web.xml file.

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
 http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
 version="3.0">

 <filter>
   <filter-name>struts2</filter-name>
   <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
 </filter>

 <filter-mapping>
   <filter-name>struts2</filter-name>
   <url-pattern>/*</url-pattern>
 </filter-mapping>

</web-app>

4. index.jsp

Below is the index.jsp file which the user will be redirected to.

index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
 <head>
   <title>Java Code Geek Strut2 Log4J example</title>
 </head>
 <body>
   <h1>Java Code Geeks - Welcome to Struts 2 Log4j Example</h1>
 </body>
</html>

5. Configuration file

Below are the two configuration files used for this example

5.1 Struts Configuration

Below is the struts.xml configuration file

struts.xml

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

<!DOCTYPE struts PUBLIC
 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
 "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
  <constant name="struts.convention.result.path" value="/"></constant>
  <package name="example" namespace="/" extends="struts-default">
    <action name="example" class="com.javacodegeeks.Struts2ExampleAction">
      <result name="success">/index.jsp</result>
    </action>
  </package>
</struts>

5.2 Log4j Configuration

Below is the log4j.properties file

log4j.properties

# Root logger option
log4j.rootLogger=INFO, file

log4j.appender.file=org.apache.log4j.RollingFileAppender

log4j.appender.file.File=C:\\javacodegeeks\\struts2\\logger.log
log4j.appender.file.MaxFileSize=5MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

6. Struts Action file

Below is the Struts action file Struts2ExampleAction.java

package com.javacodegeeks;

import com.opensymphony.xwork2.ActionSupport;
import org.apache.log4j.Logger;

/**
 * JavaCodeGeeks Example class.
 */
public class Struts2ExampleAction extends ActionSupport {

 private static final long serialVersionUID = 1L;

 private static final Logger LOGGER = Logger.getLogger(Struts2ExampleAction.class);

 /**
 * {@inheritDoc}
 * @see com.opensymphony.xwork2.Action
 */
 public String execute(){
   LOGGER.debug("Inside Struts2ExampleAction.execute() method");
   LOGGER.debug("JavaCodeGeeks Struts 2 Example");
   return SUCCESS;
 }
}

7. Output

Run the tomcat and deploy the application. Once the application is in running state go to URL ‘localhost:8081/struts2-log4j/example’. My tomcat is running on 8081 port. Use the port whereever your tomcat is running.

Figure 2. Output
Figure 2. Output

Since we are using the File appender the log messages will go in the file defined in the log4j.properties file: C:\\javacodegeeks\\struts2\\logger.log

2015-02-08 21:55:11 DEBUG ExampleAction:16 - Inside Struts2ExampleAction.execute() method
2015-02-08 21:55:11 DEBUG ExampleAction:16 - JavaCodeGeeks Struts 2 Example

8. Download the source code

This was an example of Struts 2 and Log4J Integration.

Download
You can download the full source code of this example here : Struts2 Log4J Integration

Mohammad Meraj Zia

Senior Java Developer
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button