JavaExample.net | Struts 2 web application white project with annotation

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 />
    </appender></p>
<p>    <!-- specify the logging level for loggers from other libraries -->
    <logger name="com.opensymphony">
     <level value="DEBUG" />
    </logger></p>
<p>    <logger name="org.apache.struts2">
      <level value="DEBUG" />
    </logger></p>
<p>   <!-- for all other loggers log only debug and above log messages -->
     <root></p>
<priority value="DEBUG"/>
        <appender-ref ref="STDOUT" />
     </root> </p>
<p></log4j:configuration>

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">
 <display-name>booking-inside-webapp</display-name>
 <filter>
 <filter-name>struts2</filter-name>
 <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
 <init-param></p>
<param-name>actionPackages</param-name>
<param-value>package.of.all.my.struts.actions</param-value>
 </init-param>
 </filter></p>
<p> <filter-mapping>
 <filter-name>struts2</filter-name>
 <url-pattern>/*</url-pattern>
 </filter-mapping></p>
<p> <welcome-file-list>
 <welcome-file>index.html</welcome-file>
 <welcome-file>index.htm</welcome-file>
 <welcome-file>index.jsp</welcome-file>
 <welcome-file>default.html</welcome-file>
 <welcome-file>default.htm</welcome-file>
 <welcome-file>default.jsp</welcome-file>
 </welcome-file-list></p>
<p></web-app>

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>
<p>@Results({
@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";
 }</p>
<p>}

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

example:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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"></p>
<p></head>
<body>
success
</body>
</html>

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 *