Gradle build Struts2 application example
Struts 2 is a flexible MVC framework, this post gets you start with Struts 2 and build with Gradle, the Groovy language based Java build tool. All operations are done in command line.
Step 1. Create project layout
Create project directory with the following command.
mkdir src\main\java\com\makble\struts2\action mkdir src\main\resources mkdir src\src\webapp\view mkdir src\src\webapp\WEB-INF
Step 2. Add build.gradle
The dependencies are simple, just add struts2-core dependency, all the necessary packages will depend on struts2-core.
apply plugin: 'base' apply plugin: 'java' apply plugin: 'jetty' apply plugin: 'war' apply plugin: 'maven' repositories { mavenCentral() } dependencies { compile 'org.slf4j:slf4j-api:1.5.8' compile 'org.slf4j:slf4j-simple:1.5.8' compile 'ch.qos.logback:logback-core:0.9.17' // struts 2 compile 'org.apache.struts:struts2-core:2.3.15.1' }
The mavenCentrla will fetch all artifacts from remote Maven repository and use Gradle's own artifacts cache system. If you already used Maven for a long time, its possible the local Maven repository already contains the packages, to use that set the repositories as follows:
repositories { mavenLocal() mavenCentral() }
Step 3. Add action class
To make it simple, we extends from DefaultActionSupport.
package com.makble.struts2.action; import org.apache.struts2.dispatcher.DefaultActionSupport; @SuppressWarnings("serial") public class MyAction extends DefaultActionSupport { private String name; @Override public String execute() throws Exception { name = "David"; return "myview"; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
Step 4. Configuring struts.xml
The struts.xml should be added to resources folders, it will be copied to classes directory of the project.
<?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> <include file="struts-default.xml"/> <package name="default" extends="struts-default"> <action name=""> <result>/view/index.jsp</result> </action> <action name="myAction" class="com.makble.struts2.action.MyAction"> <result name="myview">/view/myview.jsp</result> </action> </package> </struts>
More details about struts-default.xml: What is struts-default.xml and how to use it.
Step 5. web.xml and views
The web.xml register Struts2 filter.
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Archetype Created Web Application</display-name> <filter> <filter-name>struts2_filter</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2_filter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib prefix="s" uri="/struts-tags" %> <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>Struts example</title> </head> <body> <h1>Home Page</h1> Go to <a href="myAction.action">action</a> </body> </html>
myview.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib prefix="s" uri="/struts-tags" %> <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>Struts example</title> </head> <body> <h2><s:property value="name"/></h2> </body> </html>
To build the project, run
gradle build
A war file will be generated in build/libs, copy this war file to tomcat webapps directory and start tomcat server.