Action class is the key to Struts 2 applications. We have implemented the business logic in most Action classes. Action class to respond to user operations, when the user clicks a URL. One or more methods in the Action class are executed and a string result is returned. Displays a specific JSP page based on the result value.
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; }}
The HelloWorldAction class of the Struts2 framework creates an object and calls the execute method to respond to user operations. Execute the execute method in the business logic and return a String constant. Simply put, you must execute an operation class for each URL, or you can directly use the name of this class as the operation name, or map to some other names using struts. xml file, as shown in.
Create View)
We need a JSP to submit the final message. This page uses the pre-defined action in the Struts 2 framework, which will occur in the struts ing defined in the Struts. xml file.
<%@ page contentType=text/html; charset=UTF-8 %><%@ taglib prefix=s uri=/struts-tags %>
The taglib Command tells the Servlet container that the page will use the Struts2 labels, which will be passed through s before. S: The property tag displays the property value of the handler class 'name'. It is the getName () method that calls the HelloWorldAction class ().
Next, create a home page:
We also need to create the index. jsp page file in the WebContent folder. This file is used as the initial action URL. You can directly click the HelloWorldAction class definition method called by the Struts2 framework and render the HelloWorld. jsp view.
There may be a problem:
The web project cannot be deployed to the local tomcat configured in eclipse.I. Problems Found
Create a Dynamic Web Project in eclipse, configure the local tomcat, write the code, and select Run on Server, however, after running the program, the created Project name does not appear in the webapps under the tomcat installation directory.
Ii. Verification
Obviously, the project is not automatically deployed in Tomcat's Webapps, but in other containers. Enter http: // localhost: 8080/webDemo/login. jsp in the built-in browser. However, when http: // localhost: 8080 is opened in an external browser, the expected Kitten Screen does not appear.
Iii. Cause
Eclipse does not deploy the project to webapps under the tomcat installation directory by default in MyEclipse, but is deployed to the working directory by default.
In. metadata. pluginsorg. eclipse. wst. server. core mp1wtpwebapps, The tmp1 folder stores the project information.
4. Modify
To deploy the project to webapps under the tomcat installation directory by default, show view> servers> Find the tomcat to be modified> right-click ① To stop the Tomcat server (stop) in eclipse) ② add and remove items deployed in the container ③ clear the container related data (clean) ④ open the tomcat modification interface (open)
⑤ Locate the servers location and select the second (User tomcat Installation) ⑥ modify the deploy path to webapps 7 and save and close
It must be noted that ① ② must be operated; otherwise, the following steps will be grayed out and cannot be operated.
Another error occurs at The beginning of jsp: JavaWeb: the superclass javax. servlet. http. HttpServlet was not found on The Java Build Path
In the original Java Web project, no Tomcat runtime classes were added. The following are specific solutions: 1. Right-click the web Project-> attribute or Build Path-> Java Build Path-> Libraries-> Add Libray... -> Server Runtime-> Tomcat Server 2. Switch to Orader and Export on the Java Build Path interface and select Tomcat.
The Code on the home page is as follows (index. jsp ):
<%@ page language=java contentType=text/html; charset=ISO-8859-1 pageEncoding=ISO-8859-1%><%@ taglib prefix=s uri=/struts-tags%>
The hello action definition in the preceding view file will be mapped to the HelloWorldAction class and its execution method using the struts. xml file. When you click the "Submit" button, it will cause the Struts2 framework to run the execution method definition in it, based on the return values of the HelloWorldAction class and method, select and present a response for the corresponding view.
Configuration File
We need a ing to work with URL, HelloWorldAction class (model), and HelloWorld. jsp (view ). Ing tells Struts 2 how the framework class will respond to user operations (URLs). This type of method will be executed and the View selected based on the method returned to display the string results.
/Hello.jsp
Here, we set struts. devMode to true, because we are working on the development environment, we need to see some useful log messages. Then, we define a package called helloworld. It is useful to create a package when you want to put the classes in it. In our example, our action is named "hello". The corresponding URL is/hello. action and the backup is HelloWorldAction. class. HelloWorldAction. class.ExecuteThe method is the URL method/hello. action that is run at the time of the call. If the execute method returns success, we will introduce the user to HelloWorld. jsp.
The next step is to createWeb. xmlFile. The entry point of the Struts2 application will be a filter defined in the deployment descriptor (web. xml.
In the web. xm file, we define a filter org. apache. struts2.dispatcher. FilterDispatcher. web. xml file that requires creating WebContent under the WEB-INF folder. Eclipse has created a web. xml file when creating a project. Therefore, we only need to modify it as follows:
Struts 2
index.jsp
struts2
org.apache.struts2.dispatcher.FilterDispatcher
struts2
/*
We have specified index. jsp as the file on our homepage. We have configured all the URLs running on the Struts2 filter (I. e, any URL that matches the pattern /*)
Enable detailed logs:
You can enable the complete logging function, and create both by creating a job and Struts2Logging. propertiesFile inWEB-INF/classesFile. In the property file, note the following two lines:
org.apache.catalina.core.ContainerBase.[Catalina].level = INFOorg.apache.catalina.core.ContainerBase.[Catalina].handlers = java.util.logging.ConsoleHandler
By default, logging. properties specifies the ingress record of ConsoleHandler to stdout and FileHandler in the same way. You can set the Log threshold of a processing program to SEVERE, WARNING, INFO, CONFIG, FINE, FINER, FINEST, or ALL.
That's it. We are ready to run our Hello World application using the Struts 2 framework.
The following are a few important information to be aware of. The most important thing is to check the error information and find the cause after an error occurs. The problem is due to two reasons:
1) writing errors in the configuration file.
2) the import package is incomplete. Just follow the error message to export the package.