[Note] Struts2 environment construction using Gradle in Eclipse

【Overview】

Build an environment for Struts2 with the Gradle project in Eclipse. (For some reason, I got a prefix = "s" related error from the dynamic web project and it didn't work, so I used the Gradle project)

【Method】

** ① Create a Gradle project. ** ** The project name is appropriate (Struts2_gradle in this case), and click the "Finish" button. SnapCrab_新規 Gradle プロジェクト_2017-6-18_9-14-40_No-00.png

** ② Make the folder structure of the dynamic project. ** ** Right-click the created Struts2_gradle → click "Properties". Click "Project Facets" from the list on the left, then click "Convert to Facet Form ...". Check "Dynamic Web Module" and click "OK". SnapCrab_Struts2_gradle のプロパティー_2017-6-18_9-19-33_No-00.png

** ③ Rewrite build.gradle. ** ** Rewrite "build.gradle" existing in Struts2_gradle project as follows.

build.gradle


/*
 * This build file was auto generated by running the Gradle 'init' task
 * by 'nobu' at '17/06/18 8:36' with Gradle 2.14.1
 *
 * This generated file contains a sample Java project to get you started.
 * For more details take a look at the Java Quickstart chapter in the Gradle
 * user guide available at https://docs.gradle.org/2.14.1/userguide/tutorial_java_projects.html
 */

// Apply the java plugin to add support for Java
apply plugin: 'java'

// In this section you declare where to find the dependencies of your project
repositories {
    // Use 'jcenter' for resolving your dependencies.
    // You can declare any Maven/Ivy/file repository here.
    jcenter()
}

// In this section you declare the dependencies for your production and test code
dependencies {
    // The production code uses the SLF4J logging API at compile time
    compile 'org.slf4j:slf4j-api:1.7.21'

    // Declare the dependency for your favourite test framework you want to use in your tests.
    // TestNG is also supported by the Gradle Test task. Just change the
    // testCompile dependency to testCompile 'org.testng:testng:6.8.1' and add
    // 'test.useTestNG()' to your build script.
    testCompile 'junit:junit:4.12'
    compile "org.apache.struts:struts2-core:2.3.15.1"
    compile "javax.servlet:javax.servlet-api:3.1.0"
}

task copyDependencies(type:Copy) {
    copy {
        from configurations.runtime
        into 'WebContent/WEB-INF/lib'
    }
}

** ④ Add the library. ** ** Right-click on the Struts2_gradle project → click “Gradle” → click “Refresh Gradle Project”.

** ⑤ Create a folder / file as shown below. ** **

Folder structure


Struts_gradle
├── build.gradle
├── src
│   └── main
│       └── java
│           └── com
│               └── tutorialspoint
│                   └── struts2
│                       └── HelloWorldAction.java
└── WebContent
    ├── HelloWorld.jsp
    ├── index.jsp
    └── WEB-INF
        ├── logging.properties
        ├── web.xml
        └── classes
            └── struts.xml

** ⑥ Rewrite the contents of the file. ** **

HelloWorldAction.java


package com.tutorialspoint.struts2;

public class HelloWorldAction {
	private String name;

	public String execute() throws Exception {
		return "success";
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
}

HelloWorld.jsp


<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Hello World</title>
</head>
<body>
Hello World, <s:property value="name"/>
</body>
</html>

index.jsp


<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!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">
<title>Hello World</title>
</head>
<body>
	<h1>Hello World From Struts2</h1>
	<form action="hello">
		<label for="name">Please enter your name</label><br/>
		<input type="text" name="name" />
		<input type="submit" value="Say Hello" />
	</form>
</body>
</html>

logging.properties


org.apache.catalina.core.ContainerBase.[Catalina].level = INFO
org.apache.catalina.core.ContainerBase.[Catalina].handlers = \
java.util.logging.ConsoleHandler

web.xml


<?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>Struts2</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <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>
</web-app>

struts.xml


<?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="helloworld" extends="struts-default">
		<action name="hello"
			class="com.tutorialspoint.struts2.HelloWorldAction"
			method="execute">
				<result name="success">/HelloWorld.jsp</result>
			</action>
	</package>
</struts>

** ⑦ Execute the project. ** ** Right-click Struts2_gradle → click "Run" → "Run on Server". Then the following screen will appear. SnapCrab_Hello World - Google Chrome_2017-6-18_9-42-59_No-00.png

[WAR file]

I uploaded the WAR file of the project created this time to GitHub. GitHub_nobu1234(Struts_gradle)

[Reference site]

tutorialspoint_Struts2 → Contents of Java file and xml file Try Struts2 (1) → Add Struts2 library to build.gradle Copy the dependent library with Gradle → How to add the library to WEB-INF / lib [Note!] Make a WAR with Gradle → Add servlet-api to build.gradle due to Servlet related error in jsp file

Recommended Posts

[Note] Struts2 environment construction using Gradle in Eclipse
[Environment construction] Eclipse installation
Allow development in Eclipse environment using iPLAss SDK
[Java] Environment construction procedure for developing struts 1.3 with Eclipse
JavaFX environment construction in Java 13
Using Amateurs UML in Eclipse
Java development environment construction (Mac + Pleiades All in One Eclipse 4.7 + Spring Boot + Gradle (Buildship))
◆ Spring Boot + gradle environment construction memo
Building a Lambda development environment in Eclipse
Rails API server environment construction using docker-compose
Batch implementation in RubyOnRails environment using Digdag
Run jooby's Eclipse development environment on Gradle
[Note] Execute java program in the integrated development environment Eclipse-I tried using git
[For beginners] I tried using DBUnit in Eclipse
[For beginners] I tried using JUnit 5 in Eclipse
Environment construction procedure for using PowerMock with JUnit
[Rails] Run LINEBot in local environment using ngrok
Kaggle environment construction using official Docker and vscode
[Ubuntu 18.04] Environment construction for using PyTorch with RTX3090
Prepare for log output using log4j in Eclipse.
Development environment construction using IntelliJ IDEA + Maven + Tomcat 9
Resolve "Cannot resolve symbol" in IntelliJ projects using Gradle
Support out of support in docker environment using centos6
Django development environment construction using Docker-compose (personal memorandum)
Tips for generating files for eclipse projects in Gradle
I stumbled when I tried using neo4j in the jenv environment, so make a note
[Java] Environment construction
Java environment construction
MVC in Eclipse.
[Spring] Environment construction
Docker environment construction
[LeJOS] Let's program mindstorm-EV3 in Java [Environment construction part 2]
[Mac] VS Code development environment construction (Java, Gradle, Node.js)
Automatically generate DDL using [jpa-schema-gradle-plugin] plugin in Spring × Gradle
[Note] Build a Python3 environment with Docker in EC2
Until building Spring-Boot using Eclipse on Mac (Gradle version)
Ruby on Rails environment construction using VirtualBox, Vagrant, cyberduck
Database environment construction with Docker in Spring boot (IntellJ)
Construction of data analysis environment using Docker (personal memorandum)