Well, we are configuring a new struts 2 web application with annotation support.

Before all, we download last full GA release of Struts 2 framework and Apache Tomcat 7

After we can start from an Eclipse “Dynamic web project”.

On /lib of our struts 2 zip archive we find and copy following jar files to WEB-INFlib :

  • commons-fileupload-X.X.X.jar
  • commons-io-X.X.X.jar
  • commons-lang-X.X.jar
  • commons-logging-X.X.X.jar
  • commons-logging-api.X.X.jar
  • freemarker-X.X.X.jar
  • ognl-X.X.X.jar
  • struts2-core-X.X.X.X.jar
  • xwork-core-X.X.X.jar
  • javassist-X.X.X.jar
  • log4j-X.X.X.jar
To enable annotation behaviour we need also following library:
  • asm-X.X.X.jar
  • asm-commons-X.X.X.jar
  • asm-tree-X.X.X.jar
  • struts2-convention-plugin-X.X.X.jar
Well, now, we add a new log4j.xml config file under /src:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration PUBLIC "-//log4j/log4j Configuration//EN" "log4j.dtd"></p>
<p><log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"></p>
<p>    <appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
       <layout class="org.apache.log4j.PatternLayout"> </p>
<param name="ConversionPattern" value="%d %-5p %c.%M:%L - %m%n"/>
       </layout><br />
<p>    <!-- specify the logging level for loggers from other libraries -->
    <logger name="com.opensymphony">
     <level value="DEBUG" />
<p>    <logger name="org.apache.struts2">
      <level value="DEBUG" />
<p>   <!-- for all other loggers log only debug and above log messages -->
<priority value="DEBUG"/>
        <appender-ref ref="STDOUT" />
     </root> </p>

Also we add servlet filter on web.xml for enable struts framework with annotaton support

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
 id="WebApp_ID" version="3.0">
<p> <filter-mapping>
<p> <welcome-file-list>

Well, now we build the first struts 2 action with annotation

package mypackage;</p>
<p>import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;</p>
<p>import com.opensymphony.xwork2.Action;</p>
@Result(name="input", location="/jsp/success.jsp" ),
@Result(location="/jsp/error.jsp" )
public class LoginAction implements Action {</p>
<p> @Override
 public String execute() throws Exception {
 return "input";

We create also success.jsp and error.jsp on /WebContent/jsp/


<%@ page language="java" contentType="text/html; charset=UTF-8"
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></p>

Well, now we can test our simple work browsing http://localhost:8080/login …

If it is all ok we will see as result the succes page!

Tagged with →  
Share →

Leave a Reply

Your email address will not be published. Required fields are marked *