Environment configuration of Struts2 and struts2
Before learning about struts2, we must first understand the purpose of using struts2? What benefits does it bring to us?
Design Objectives
The first goal of Struts design is to apply the MVC pattern to web programming. Here, the benefits of the MVC mode are not mentioned.
Technical advantages
Struts2 has two technical advantages. First, all Struts2 applications are based on The client/server HTTP Exchange Protocol. The assumervlet API reveals that Java Servlet is only a small subset of Java APIs, in this way, we can use a powerful Java language for programming in the business logic section.
The second is to provide a clear implementation of MVC, which contains many key components involved in processing the request, such as the interceptor, OGNL Expression Language, and stack.
Because struts2 has such a goal and has such advantages, this is the reason for us to learn struts2. Next, we will thoroughly analyze the working principles of struts.
Working Principle
The following figure describes how Suruts2 works. Next we will introduce the core content of each step in steps.
If you are confused about the above explanation, you don't have to worry about it. Let's take an example to understand the above explanation. I think this is the best learning path for beginners, next I will lead you to the world of Struts2.
For better work, we must first sharpen the tool. We need to download the Struts2 configuration jar file for later development and use, go to the Struts official website, and download the required Struts version, here we use struts-2.3.16.1, download the Struts version, decompress it, and manage the jar package. I personally suggest you use the frequently used jar files, the Unified Classification is stored in one location, so that we can reference jar in the future. We know that eclipse provides a User libraries for me, we can add our own libraries to manage our jar files.
After downloading Struts, We need to download a Tomcat server to deploy our Struts project. If you need to describe it here, you must install JAVA jdk before installing Tomcat, otherwise, the installation will fail, and we also need to note that we recommend that you install and unzip the Tomcat version to facilitate the integration of myEclipse and make these preparations, now we can start the development of the first Struts2 project.
Open myEclipse, integrate the Tomcat server we just installed, and click Window -- "preferences" at the top of myEclipse:
After configuring Tomcat, create a Helloword project and import our jar file. Here we will teach you a little trick. It will be much easier to import the jar file later. The specific operations are as follows:
After importing the jar file, we need to configure some of our Struts2 environment, open the WEB-INF under our project WebRoot, find web. xml, add the basic configuration information of Struts2:
<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</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>
After configuring our web. xml, add the Struts. xml file below. How can this file be written? The answer is very simple, open our Struts decompression file, find the struts2-blank under the apps, unzip the file, unzip and find the struts2-blank \ WEB-INF \ classes struts. xml: Copy this file to the src directory of our project. Here, the configuration of the Struts development environment is basically complete. Next we will compile a simple Helloword together, start our Struts2 learning journey.
Our Action file:
public class Helloword { public String helloword(){ return "success"; }}
Our Struts configuration file:
<? Xml version = "1.0" encoding = "UTF-8"?> <! DOCTYPE struts PUBLIC "-// Apache Software Foundation // DTD Struts Configuration 2.3 // EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <! -- <Constant name = "struts. enable. DynamicMethodInvocation" value = "false"/> --> <! -- Set whether the debugging mode is used --> <! -- <Constant name = "struts. devMode" value = "true"/> --> <! -- <Package name = "default" namespace = "/" extends = "struts-default"> --> <! -- Default file access --> <! -- <Default-action-ref name = "index"/> --> <! -- Error handling file --> <! -- <Global-results> --> <! -- <Result name = "error">/error. jsp </result> --> <! -- </Global-results> --> <! -- Error Type --> <! -- <Global-exception-mappings> --> <! -- <Exception-mapping exception = "java. lang. Exception" result = "error"/> --> <! -- </Global-exception-mappings> --> <! -- Access Control --> <! -- <Action name = "index"> --> <! -- <Result type = "redirectAction"> --> <! -- <Param name = "actionName"> HelloWorld </param> --> <! -- <Param name = "namespace">/example </param> --> <! -- </Result> --> <! -- </Action> --> <! -- </Package> --> <! -- <Include file = "example. xml "/> --> <package name =" default "namespace ="/"extends =" struts-default "> <action name =" helloword "class =" com.edu. action. helloword "method =" helloword "> <result name =" success ">/index. jsp </result> </action> </package> </struts>
The final directory structure:
Here, we have finished introducing this article, and the subsequent content will be continuously updated...