What is Structs2? It evolved on the basis of Struts1 and is a framework of MVC mode. In fact, its function is very simple, which is to separate the View and the logical processing layer. I am not talented and have a low level of knowledge. I used to use Spring to do projects. Since I was going to graduate, I needed to change careers (let’s say that). The company uses the Structs framework. There is no way, so I can learn from scratch!
Required software - Eclipse, TomCat 7.0, Struts-2.3.1.2 (download the source code yourself), the following is a specific small project to explain.
First, the working principle of Struts2: First, Tomcat parses the URL entered by the user, parses out the project name, and then the program looks for the started project, then goes directly to the configuration file web.xml, and finds the configuration file of struts2--struts.xml according to the configuration of the web.xml file, that is, filter, and then finds the action accessed by the user based on the namespace, and then jumps to the action interface required by the user.
Maybe it's more troublesome to see the above, so let's use a typical small project to illustrate it:
1. Open Eclipse (Note that this Eclipse is not a classic but a downloaded IDE version, which is very convenient for web development), create a new Web Project, and be careful to follow the next method. In this way, you can choose the web.xml item in the last step, eliminating the manual addition step.
2. Add the jar package, unzip the downloaded structs source code package, and then add the jar package you used in the lib file, and add the lib folder under WebContent-WEB-INF.
3. Add struts.xml file. The simplest struts.xml file is in the downloaded struts source code app. You can find one from it, or you can write one yourself in the following way (the simplest) and put it directly into the root directory of src:
Copy the code code as follows:<?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="front" namespace="/" extends="struts-default">
<action name="index">
<result name="success">/Index.jsp</result>
</action>
</package>
</struts>
Let's explain the above content: the struts.devMode in line 6 is configured to adopt the development mode. The development mode means that dynamic modification can be demonstrated dynamically without restarting the server. It is false by default; the package in line 7 is like a Java package, used for classification; namespace is very important and is used to split the url. As mentioned above, the url to be opened is:....../index, where / represents namepcae, and index represents the name attribute of the action. The class in action is a class, used for some logical processing of ordinary java classes; result is the jsp (View) interface transferred to, and the above jsp interface is placed in the root directory of WebContent.
4. Write Java class-Action02, create a new package com.myservice.web, and then create a new Java class Action02. The content is very simple.
The code copy is as follows: package com.myservice.web;
import com.opensymphony.xwork2.ActionSupport;
public class Action02 extends ActionSupport{
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
return "success";
}
}
It is to override the execute method and directly return a string success, which corresponds to the result attribute in the action.
There are two other methods that can complete the function of class, but they are not often used. One is to not inherit any classes and interfaces, and directly write an execute method, which also returns a String; the other is to write a class to implement the Action interface, and then overwrite the Action's execute method. In fact, ActionSupport also implements the Action interface, but it also implements many other methods, which can be used very conveniently. Therefore, in development, the method of inheriting the ActionSupport class is adopted.
5. Write a JSP file and create a new jsp file in the root directory of WebContent. Note that it should set its language to utf-8, such as:
Copy the code as follows: <%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!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>MyWeb02</title>
</head>
<body>
<h1>Tomorrow and tomorrow, how many tomorrows</h1>
</body>
</html>
Through the above work, the simplest writing is completed, and then you can start server (Tomcat7.0) and enter http://localhost:8080/xxx/index in the browser, where xxx represents the project name.