Some may call it Legacy, but this is an introductory article on Struts 2. Recently, new client-side technologies have evolved, and some people may say that the server-side technology itself is legacy, let alone Struts2.
However, when you run into something that is currently in operation, you really need to understand it. I recently encountered such a pattern and studied hard.
When I tried to make HelloWorld, I had a hard time finding a document that covered everything, so I will write this article as a reminder of that time.
Struts2, even if it is said to be a legacy, seems to have a lot of version upgrades, and it is still new. At the time of writing this article, I would like to introduce through Hello World what you can build a development environment for Struts2 using the latest version.
It is a web application framework that adopts the MVC model implemented in Java. The MVC model is the idea of separately implementing Model (data), View (screen), and Controller (business logic). However, although Struts2 has a function to separate V and C, it does not provide a function to separate M from C, so it is often realized by introducing an OR mapper etc. separately.
There used to be a framework called Struts that dominated the world, but Struts 2 is fundamentally different. The original development process is completely different, it seems that it was developed under the name "WebWork2", and that name was changed to Struts2 with the end of support for Struts.
Struts2 is still supported and new versions are released fairly often.
It's a little off topic, but you can dramatically improve your productivity by deploying Struts2 in large-scale web applications such as enterprise systems. In addition, although it is not limited to Struts2, by introducing a framework, it is possible to absorb and level the differences in implementation policies due to the habits of developers. However, the author thinks that it is not suitable for small-scale development because it takes time to introduce.
From now on, we will build a development environment to realize Hello World with Struts2. There are three ways to build it.
This time, I will build it by myself.
2 is that you need to study Maven first, and it may not be available depending on the environment. Even if you try to use 3, you cannot start it from the default state with an error, so it is faster to build it by yourself than to use it.
Therefore, let's build it by ourselves, also by moving our hands and remembering it!
The author uses an integrated development environment for basic development. I think that basic Eclipse will be used for Java development, but this time I will use Eclipse as well.
The environment I am using is "Eclipse 4.4 (Luna)". The versions of Eclipse can be different, so readers should use the one that is close to them.
① Select File > New > Other > Web > Dynamic Web Project and proceed to the next.
② Enter the following information to proceed to the next.
Setting items | Set value |
---|---|
Project name | Struts2HelloWorld ※Project nameは自由に決めていただいて構いません |
Target runtime | Tomcat8 (Java8) |
Dynamic web module version | 3.1 |
Constitution | Tomcat8 (Java8)デフォルトConstitution |
③ Add the "resources" folder by adding a folder and proceed to the next.
(4) Check the generation of web.xml deployment descriptor and complete.
⑤ If the project is completed as shown in the image below, the project creation is complete.
Introduce Struts2 to the created project.
Download Struts 2 from here. At the time of writing this article, the latest version seems to be "2.5.14.1". Although not used in HelloWorld, download "struts-2.5.14.1-all.zip" with a sample application. Also, please download "log4j-core-2.9.1.jar" separately as you will need it later. Please match the version of the jar file downloaded here with log4j-api.
"Struts-2.5.14.1-min-lib.zip" is an empty Struts2 project with a minimum configuration. In my environment, I couldn't start with an error from the beginning. It was a hassle to solve, so I built it myself from the beginning, but I would appreciate it if you could comment if you were able to use it well.
Introduce Struts 2 to your project. Unzip the downloaded struts-2.5.14.1-all.zip and copy the following jar file from the lib folder to the "project root / Struts2HelloWorld / WebContent / WEB-INF / lib" folder.
file name | Remarks |
---|---|
commons-fileupload-1.3.3.jar | |
commons-io-2.5.jar | |
commons-lang-2.4.jar | |
commons-lang3-3.6.jar | |
commons-logging-1.1.3.jar | |
freemarker-2.3.26-incubating.jar | |
javassist-3.20.0-GA.jar | |
log4j-api-2.9.1.jar | |
log4j-core-2.9.1.jar | This is a separately downloaded jar file. log4j-Please match the version with api. |
ognl-3.1.15.jar | |
struts2-core-2.5.14.1.jar | |
xmlpull-1.1.3.1.jar | |
xpp3_min-1.1.4c.jar | |
xstream-1.4.10.jar |
This is the minimum library configuration for running Struts2.
Create "struts.xml" which is the configuration file of Struts2 in "/ Struts2 HelloWorld / resources".
As an aside, Struts2 allows you to implement your application without an XML configuration file, which is called zero configuration. However, do developers like XML? Even if Struts2 is introduced, most of the cases I have seen are written in XML. Although there is an advantage that the application settings can be understood only by looking at this file, the author thinks that there is nothing better than not, considering the trouble of writing the setting file.
Especially in Struts2, as mentioned at the beginning, it is often used in combination with other frameworks such as Spring and OR mapper, and each framework requires an XML configuration file, so the amount of XML to be managed increases tremendously. It should be a zero configuration to free the developer from that XML hell, but for some reason it is not used (laugh)
It's been a long time, but this time I will write the configuration file together for more cases. I've included comments along with it, but skip it for now.
■ Contents of struts.xml
Another, place the setting file "log4j2.xml" of "log4j2" which is a log output library.
■ Contents of log4j2.xml (Please restore the comment part arbitrarily)
<!-Logger settings used for output->
This is not necessary, but it is convenient to place a file called "struts.properties" in the same folder and write "struts.ui.theme = simple" because it can suppress the automatic style sheet tag generation by Struts2.
At this point, the introduction of Struts 2 is complete. I've done a fair amount of work, so I'll list the tree of the projects I've configured. If the project has the following tree, the development environment construction is complete.
■ Project tree structure Application root ├─src ├─resources │ log4j2.xml │ struts.properties │ struts.xml │ ├─build │ └─classes * The same materials placed in the source folder are automatically generated here. │ log4j2.xml │ struts.properties │ struts.xml └─WebContent ├─META-INF │ MANIFEST.MF │ └─WEB-INF │ web.xml │ └─lib commons-fileupload-1.3.3.jar commons-io-2.5.jar commons-lang-2.4.jar commons-lang3-3.6.jar commons-logging-1.1.3.jar freemarker-2.3.26-incubating.jar javassist-3.20.0-GA.jar log4j-api-2.9.1.jar log4j-core-2.9.1.jar ognl-3.1.15.jar struts2-core-2.5.14.1.jar xmlpull-1.1.3.1.jar xpp3_min-1.1.4c.jar xstream-1.4.10.jar
Now that we have built the development environment, we will start implementing the application itself.
Prepare the top page when accessing the application. This time, we will implement it using JSP.
Right-click WebContent> New> JSP to open the dialog. Enter "index.jsp" in the file name to complete.
■index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!-Enable Struts2 tag library-> <%@ taglib prefix="s" uri="/struts-tags"%> <!-Use HTML5-type declarations->
In Struts2, the class responsible for processing Web applications, such as when submitted by JSP, is called Action. Action and transition destination page are defined in struts.xml, but I will explain it later, so please implement as follows at the moment.
Right-click on the src folder> New> From Class, enter the following information to complete.
Input items | Input value |
---|---|
package | sample |
name | HelloWorldAction |
■HelloWorldAction
package sample;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
/**
/** name */ private String name;
/**
[Action execution process]
Execute the process to output Hello World.
Implement the method to be executed using the method name specified in struts.xml.
In Struts2, the page transition destination is determined by the processing result of the action,
This time, the fixed character string of "success" is returned.
*
* @return
*/
public String execute() {
logger.info ("The parameter entered is" {} ".", name);
return "success";
}
/**
[Name acquisition process]
Get the name.
When Struts2 takes the value entered in the form as a parameter
Because getter is used, the information you want to use as a parameter is
Declare it as a property of Action class and prepare a getter.
*
@return name */ public String getName() { return name; }
/**
[Name setting process]
Set the name.
As with getters, prepare setters for the values you want to pass between pages.
*
@param name name */ public void setName(String name) { this.name = name; } }
Implement the transition destination when the button on the top page is pressed. Create a view / sample folder in the WebContent directory and place the transition destination page there. The location directory itself can be anywhere in WebContent, but this time it will be the above directory. Add "HelloWorld.jsp" to the sample folder in the same procedure as the top page.
■HelloWorld.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Hello Struts2 world!</title>
</head>
<body>
<!-Displays the data passed between messages. -> Hello Struts2 world, <s: property value = "name" />