Using Struts with Codenvy

Apache Struts is a free open source framework for creating Java web applications. The below example demonstrates how to create a Struts2 application in Codenvy.

Start with creating a project structure, something like this:

Create Java class with the following content:

package com.codenvy;

public class HelloWorld
{
   private String message;

   private String userName;

   public HelloWorld()
   {
   }

   public String execute()
   {
      setMessage("Hello " + getUserName());
      return "SUCCESS";
   }

   public String getMessage()
   {
      return message;
   }

   public void setMessage(String message)
   {
      this.message = message;
   }

   public String getUserName()
   {
      return userName;
   }

   public void setUserName(String userName)
   {
      this.userName = userName;
   }

}

Then, create struts.xml file

<?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.devMode" value="true" />
    <package name="default" namespace="/" extends="struts-default">
        <action name="hello" class="com.codenvy.HelloWorld">
            <result name="SUCCESS">/success.jsp</result>
        </action>
    </package>
</struts>

Once done, create two *jsp files for your web app:

index.jsp

<%@taglib uri="/struts-tags" prefix="s" %>

<html>
<head>
<title>Hello World Struts</title>
</head>
<body>
  <center><s:form action="hello" >
        <s:textfield name="userName" label="User Name" />
        <s:submit />
    </s:form></center>
</body>
</html>

success.jsp

<%@taglib uri="/struts-tags" prefix="s" %>
<html>
<head>
<title>Hello World Struts</title>
</head>
<body>
  <center><h1><s:property value="message" /></h1></center>
  <center><img src="http://docs.codenvy.com/wp-content/uploads/img1.png" alt="Codenvy"></center>
</body>
</html>

web.xml should sit under WEB-INF folder:

<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.FilterDispatcher </filter-class>
</filter>
<filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

Finally, specify all dependencies, plugins and modules in pom.xml file:

<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.codenvy</groupId>
  <artifactId>sample</artifactId>
  <version>1.0</version>
  <packaging>war</packaging>
  <properties>
    <struts2.version>2.2.3.1</struts2.version>
  </properties>
  <dependencies>
    <dependency>
      <groupId>org.apache.struts</groupId>
      <artifactId>struts2-core</artifactId>
      <version>2.2.3.1</version>
      <exclusions>
        <exclusion>
          <artifactId>tools</artifactId>
          <groupId>com.sun</groupId>
        </exclusion>
      </exclusions>
    </dependency>
    <dependency>
      <groupId>org.apache.struts</groupId>
      <artifactId>struts2-config-browser-plugin</artifactId>
      <version>2.2.3.1</version>
    </dependency>
    <dependency>
      <groupId>org.apache.struts</groupId>
      <artifactId>struts2-junit-plugin</artifactId>
      <version>2.2.3.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>commons-logging</groupId>
      <artifactId>commons-logging</artifactId>
      <version>1.1.1</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.5</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-mock</artifactId>
      <version>2.0.8</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>2.5</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>servlet-api</artifactId>
      <version>2.4</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>jsp-api</artifactId>
      <version>2.0</version>
      <scope>provided</scope>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.mortbay.jetty</groupId>
        <artifactId>maven-jetty-plugin</artifactId>
        <version>6.1.21</version>
        <configuration>
          <scanIntervalSeconds>10</scanIntervalSeconds>
          <scanTargets>
            <scanTarget>src/main/webapp/WEB-INF</scanTarget>
            <scanTarget>src/main/webapp/WEB-INF/web.xml</scanTarget>
            <scanTarget>src/main/resources/struts.xml</scanTarget>
            </scanTargets>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

Run the app at Run-Run application menu to see the below greeting:

struts