This is the 14th day of my participation in the August More Text Challenge. For details, see:August is more challenging

Let’s start with the history of Struts2.

Struts1

  • Is the first PUBLISHED MVC framework in the world
  • It was released in 2001

Struts2

  • It’s not an upgrade to Struts1
  • It’s based on WebWork
  • Take advantage of both Struts1 and WebWork

So what exactly is Struts2? Struts2 is a framework for developing MVC applications. It provides solutions to some common problems in Web application development:

  • Management of page navigation activities
  • Validity verification of user input data
  • Unified layout
  • scalability
  • Internationalization and localization
  • Support for Ajax
  • Duplicate submission of forms

We assume a scenario, there are multiple links in a JSP page, click on each link to the corresponding processing, each corresponding to a Java class, hyperlinks in the class there is a way, our purpose is to click on a hyperlink call the corresponding method of Java classes, for such a scenario, we have two solutions, One is to call Java classes through servlets, and the other is to call Java classes through filters. However, both methods are cumbersome, with a lot of repetitive code, and the Struts2 framework is a very useful tool to help solve this problem.

So, how do you set up a Struts2 development environment? To set up a Struts2 environment, we generally need to do the following steps:

  1. Create a JavaWeb project
  2. Find the JAR files you need to develop your Struts2 application
  3. Creating a JSP file
  4. Create the action file
  5. Write a Struts2 configuration file
  6. Add Struts2 MVC framework startup configuration to web.xml

We followed the steps step by step. Create a Web project named test0425_structs2. Then there are the jar packages. Struts2 has a lot of jar packages.Do so many JAR packages need to be copied into the project? In fact, we don’t need to, let’s select a few more important JAR packages to import. Because baidu web disk upload number is limited, so I can not upload so many JAR packages, I upload a few we need to use next, if you need a complete JAR package, you can go to Baidu to download.

Link: pan.baidu.com/s/1vpzscP_u… Extract code: JS2U copy this section of content after opening Baidu web disk mobile App, operation is more convenient oh

Importing these key JARS into the project completes the second step. Let’s take a look at what these JARS do. Struts2-core-2.3.1.1. jar: Struts2 core class library xwork-core-2.3.1.1.jar: Command mode framework,WebWork and Struts2 are based on xwork ogNL-3.0.3. jar: Object Graph Navigation Language. The Struts2 framework reads and writes Object properties from and freemarker-2.3.18.jar: The Struts 2 UI tag template uses FreeMarker to write Commons-logging-1.1.x. jar: a logging package produced by ASF. The Struts 2 framework uses this logging package to support Log4J and JDK 1.4+ logging. Commons -fileupload-1.2.2.jar: Commons -io-2.0.1.jar: Commons -lang-2.5.jar: Commons -lang-2.5.jar: Asm-3.3. jar: provides bytecode read and write functionality, including the core functionality that other JARS are based on. Asm-commons -3.3.jar: provides event-based presentation. Asm-tree-3.3.jar: Provides an object-based representation. Javassist-3.11.0.ga.jar: Code generation tool that Struts2 uses to extend Java classes at run time

Next, create a few JSP files and post the code directly up. Create a new primer folder and create text.jsp in that folder

<%@ page language="java" pageEncoding="utf-8" contentType="text/html; charset=utf-8"%>
<%@ taglib uri="/struts-tags"   prefix="s"%>
<html>
  <head>
    <title>My JSP 'index.jsp'< span style = "box-sizing: border-box; color: RGB (74, 74, 74); line-height: 20px; white-space: normal;"${pageContext.request.contextPath }/primer.helloWorldAction.action">helloWorld</a> 
	
  </body>
</html>
Copy the code

Go ahead and create success.jsp in that folder

<%@ page language="java" pageEncoding="utf-8" contentType="text/html; charset=utf-8"%>
<%@ taglib uri="/struts-tags"   prefix="s"%>
<html>
  <head>
    <title>My JSP 'index.jsp'Starting page</title> </head> <body> < world > >!!!! <br> </body> </html>Copy the code

The fourth step is to create the action file. Create the HelloWorldAction class in the SRC directory and implement the Action interface.

public class HelloWorldAction implements Action {

	public String execute(a) throws Exception {
		System.out.println("HelloWorldAction ...");
		return "success"; }}Copy the code

Step five, write the configuration file. Create a struts. XML file in the SRC directory. The name of the file is fixed and cannot be changed.


      
<! DOCTYPEstruts PUBLIC
	"-//Apache Software Foundation// Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
	<! *name: indicates the name of the package, which is unique and is used to inherit *namespace: namespace, *extends:"struts-default. XML "is also a configuration file provided by the struts2 framework -->
	<package name="primer" namespace="/primer" extends="struts-default">
		<! -- action tag: indicates the request link *name: the name of the action, which is unique *class: the full path of the action class to be executed -->
		<action name="helloWorldAction" class="cn.itcast.action.HelloWorldAction">
			<! -- result tag: return the result *name: return the result type, which corresponds to the value returned by the execute() method * The text part after the result tag: the page to go to -->
			<result name="success">/success.jsp</result>
		</action>
	</package>
</struts>

Copy the code

As a final step, configure the filters in the web.xml file.

<filter>
		<filter-name>StrutsPrepareAndExecuteFilter</filter-name>
		<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>StrutsPrepareAndExecuteFilter</filter-name>
		<url-pattern>/ *</url-pattern>
	</filter-mapping>
Copy the code

This is the filter provided by Struts2, so the contents of the class tag are fixed. That completes all the steps.

Deploy and run the project.Click On helloWorld to see what you see.The program runs successfully. It is important to note whether the startup page of your own project is configured in the information in the web.xml file. If not configured, enter the completed URL in the address bar, such as:http://localhost:8080/test0425_struts/primer/test.jsp,An error will be reported if you do not enter the following resource details. So now that we’ve got struts2, it’s a bit complicated, but it’s not that hard to do with logic. Space is limited, so I’ll cover the rest of Struts2 in a few subsections in the next blog.