Skip to content

Commit 23a9499

Browse files
committed
Add struts2 integration with examples
1 parent d834a1e commit 23a9499

File tree

17 files changed

+586
-1
lines changed

17 files changed

+586
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
buildscript {
2+
repositories {
3+
jcenter()
4+
}
5+
6+
dependencies {
7+
classpath 'com.bmuschko:gradle-tomcat-plugin:2.3'
8+
}
9+
}
10+
11+
apply plugin: 'war'
12+
apply plugin: 'com.bmuschko.tomcat'
13+
14+
dependencies {
15+
implementation project(":rollbar-struts2")
16+
17+
implementation 'javax.servlet:javax.servlet-api:3.1.0'
18+
implementation 'org.apache.struts:struts2-core:2.5.25'
19+
implementation 'org.apache.struts:struts2-spring-plugin:2.5.25'
20+
21+
def tomcatVersion = '7.0.57'
22+
23+
tomcat "org.apache.tomcat.embed:tomcat-embed-core:${tomcatVersion}",
24+
"org.apache.tomcat.embed:tomcat-embed-logging-juli:${tomcatVersion}"
25+
tomcat("org.apache.tomcat.embed:tomcat-embed-jasper:${tomcatVersion}")
26+
}
27+
28+
tomcatRun.contextPath = '/'
29+
tomcatRunWar.contextPath = '/'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.example.rollbar.struts2.action;
2+
3+
import com.opensymphony.xwork2.ActionSupport;
4+
import com.opensymphony.xwork2.inject.Inject;
5+
import com.rollbar.notifier.Rollbar;
6+
import com.rollbar.struts.RollbarFactory;
7+
import java.util.concurrent.atomic.AtomicInteger;
8+
9+
10+
/**
11+
* Struts2 action with Spring CDI.
12+
*/
13+
public class HelloRollbarAction extends ActionSupport {
14+
15+
private static final AtomicInteger counter = new AtomicInteger(1);
16+
17+
private final Rollbar rollbar;
18+
19+
@Inject
20+
public HelloRollbarAction(RollbarFactory rollbarFactory) {
21+
this.rollbar = rollbarFactory.build();
22+
}
23+
24+
public String index() {
25+
rollbar.info("Executing index action in HelloRollbarAction");
26+
27+
return SUCCESS;
28+
}
29+
30+
public String hello() {
31+
int current = counter.getAndAdd(1);
32+
if (current % 2 == 0) {
33+
throw new RuntimeException("Fatal error at hello rollbar action. Number: " + current);
34+
}
35+
return SUCCESS;
36+
}
37+
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "springframework.org/dtd/spring-beans-2.0.dtd">
3+
4+
<beans>
5+
<bean id="rollbarFactory" class="com.rollbar.struts.RollbarFactory" scope="singleton" >
6+
<property name="accessToken" value="[ACCESS_TOKEN]" />
7+
<property name="captureIp" value="true" />
8+
<property name="environment" value="production" />
9+
<property name="codeVersion" value="1.1.2" />
10+
</bean>
11+
</beans>
12+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE struts PUBLIC
3+
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
4+
"http://struts.apache.org/dtds/struts-2.5.dtd">
5+
<struts>
6+
<constant name="struts.devMode" value="true" />
7+
8+
<package name="basicstruts2" extends="struts-default" namespace="/">
9+
<interceptors>
10+
<interceptor name="rollbarExceptionInterceptor"
11+
class="com.rollbar.struts.interceptor.RollbarExceptionInterceptor" />
12+
13+
<interceptor-stack name="appStack">
14+
<interceptor-ref name="rollbarExceptionInterceptor" />
15+
<interceptor-ref name="defaultStack" />
16+
</interceptor-stack>
17+
</interceptors>
18+
<default-interceptor-ref name="appStack"/>
19+
20+
<default-action-ref name="index" />
21+
22+
<action name="index" class="com.example.rollbar.struts2.action.HelloRollbarAction" method="index">
23+
<result type="chain">hello</result>
24+
</action>
25+
26+
<action name="hello" class="com.example.rollbar.struts2.action.HelloRollbarAction" method="hello">
27+
<result>/WEB-INF/jsp/index.jsp</result>
28+
</action>
29+
30+
</package>
31+
</struts>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<!DOCTYPE html PUBLIC
2+
"-//W3C//DTD XHTML 1.1 Transitional//EN"
3+
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4+
5+
<%@taglib prefix="s" uri="/struts-tags" %>
6+
7+
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
8+
<head>
9+
<title>Hello Rollbar struts2 example application</title>
10+
<s:head />
11+
</head>
12+
<body>
13+
Hello Rollbar!
14+
</body>
15+
</html>
16+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
4+
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
5+
id="WebApp_ID" version="3.0">
6+
7+
<context-param>
8+
<param-name>contextConfigLocation</param-name>
9+
<param-value>classpath*:applicationContext*.xml</param-value>
10+
</context-param>
11+
12+
<filter>
13+
<filter-name>struts2</filter-name>
14+
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
15+
</filter>
16+
<filter-mapping>
17+
<filter-name>struts2</filter-name>
18+
<url-pattern>/*</url-pattern>
19+
</filter-mapping>
20+
21+
<!-- Rollbar listener and filter -->
22+
<listener>
23+
<listener-class>com.rollbar.web.listener.RollbarRequestListener</listener-class>
24+
</listener>
25+
26+
<listener>
27+
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
28+
</listener>
29+
</web-app>

examples/rollbar-struts2/build.gradle

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
buildscript {
2+
repositories {
3+
jcenter()
4+
}
5+
6+
dependencies {
7+
classpath 'com.bmuschko:gradle-tomcat-plugin:2.3'
8+
}
9+
}
10+
11+
apply plugin: 'war'
12+
apply plugin: 'com.bmuschko.tomcat'
13+
14+
dependencies {
15+
implementation project(":rollbar-struts2")
16+
17+
implementation 'org.apache.struts:struts2-core:2.5.25'
18+
implementation 'javax.servlet:javax.servlet-api:3.1.0'
19+
20+
def tomcatVersion = '7.0.57'
21+
22+
tomcat "org.apache.tomcat.embed:tomcat-embed-core:${tomcatVersion}",
23+
"org.apache.tomcat.embed:tomcat-embed-logging-juli:${tomcatVersion}"
24+
tomcat("org.apache.tomcat.embed:tomcat-embed-jasper:${tomcatVersion}")
25+
}
26+
27+
tomcatRun.contextPath = '/'
28+
tomcatRunWar.contextPath = '/'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.example.rollbar.struts2.action;
2+
3+
import com.opensymphony.xwork2.ActionSupport;
4+
import java.util.concurrent.atomic.AtomicInteger;
5+
6+
7+
/**
8+
* Struts2 action that rises an error every even request number.
9+
*/
10+
public class HelloRollbarAction extends ActionSupport {
11+
12+
private static final AtomicInteger counter = new AtomicInteger(1);
13+
14+
public String execute() {
15+
int current = counter.getAndAdd(1);
16+
if (current % 2 == 0) {
17+
throw new RuntimeException("Fatal error at hello rollbar action. Number: " + current);
18+
}
19+
return SUCCESS;
20+
}
21+
22+
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE struts PUBLIC
3+
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
4+
"http://struts.apache.org/dtds/struts-2.5.dtd">
5+
<struts>
6+
<constant name="struts.devMode" value="true" />
7+
8+
<package name="basicstruts2" extends="struts-default" namespace="/">
9+
10+
<interceptors>
11+
<interceptor name="rollbarExceptionInterceptor"
12+
class="com.rollbar.struts.interceptor.RollbarExceptionInterceptor">
13+
<param name="accessToken">[ACCESS_TOKEN]</param>
14+
<param name="captureIp">true</param>
15+
<param name="environment">production</param>
16+
<param name="codeVersion">1.1.2</param>
17+
</interceptor>
18+
19+
<interceptor-stack name="appStack">
20+
<interceptor-ref name="rollbarExceptionInterceptor" />
21+
<interceptor-ref name="defaultStack" />
22+
</interceptor-stack>
23+
</interceptors>
24+
25+
<default-interceptor-ref name="appStack"/>
26+
27+
<default-action-ref name="index" />
28+
29+
<action name="index" class="com.example.rollbar.struts2.action.HelloRollbarAction" method="execute">
30+
<result>/WEB-INF/jsp/index.jsp</result>
31+
</action>
32+
33+
</package>
34+
</struts>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<!DOCTYPE html PUBLIC
2+
"-//W3C//DTD XHTML 1.1 Transitional//EN"
3+
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4+
5+
<%@taglib prefix="s" uri="/struts-tags" %>
6+
7+
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
8+
<head>
9+
<title>Hello Rollbar struts2 example application</title>
10+
<s:head />
11+
</head>
12+
<body>
13+
Hello Rollbar!
14+
</body>
15+
</html>
16+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
4+
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
5+
id="WebApp_ID" version="3.0">
6+
7+
<!-- Rollbar listener and filter -->
8+
<listener>
9+
<listener-class>com.rollbar.web.listener.RollbarRequestListener</listener-class>
10+
</listener>
11+
12+
<filter>
13+
<filter-name>struts2</filter-name>
14+
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
15+
</filter>
16+
<filter-mapping>
17+
<filter-name>struts2</filter-name>
18+
<url-pattern>/*</url-pattern>
19+
</filter-mapping>
20+
</web-app>

rollbar-struts2/build.gradle

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
ext {
2+
struts2Version = '2.5.25'
3+
}
4+
5+
dependencies {
6+
api project(":rollbar-web")
7+
implementation 'org.apache.struts:struts2-core:' + struts2Version
8+
}

0 commit comments

Comments
 (0)