Struts201 --- environment match, struts201 --- match
Development Tool: Eclipse Struts version: 2.3.24
Recently, I have been studying the SSH framework. SSH is an integrated framework of struts + spring + hibernate and is a popular open-source framework for Web applications. The systems integrated with the SSH framework are divided into four layers: presentation layer, business logic layer, data persistence layer, and domain Module layer, to help developers build Web applications with clear structures, good reusability, and convenient maintenance in a short period of time. Struts is used as the overall infrastructure of the system and is responsible for the separation of MVC. In the model section of the Struts framework, business redirection is controlled, and the persistence layer is supported by the Hibernate framework. Spring is used for management, manage struts and hibernate. The specific method is to use the Object-Oriented Analysis Method to propose some models as needed, implement these models as basic Java Objects, and then compile the basic DAO (Data Access Objects) interface, the DAO implementation of Hibernate is provided. DAO classes implemented by the Hibernate architecture are used to implement conversion and access between Java classes and databases. Spring manages struts and hibernate.
So how to build a struts environment? There are four steps in total.
Step 1: import the jar package at http://struts.apache.org /. Import the jia package, for example:
Step 2: Create Action, create a package under src, and create a class in the package. This class must inherit ActionSupport. The ActionSupport class is a tool class that implements the Action interface. In addition, it also implements the Validateable interface and provides the data verification function. By inheriting the ActionSupport class, you can simplify Struts 2's Action development. The default execute method can be implemented by inheriting ActionSupport (the return value must be available, which belongs to the String data type ).
Package com. action;
Import com. opensymphony. xwork2.ActionSupport; public class LoginAction extends ActionSupport {@ Override public String execute () throws Exception {System. out. println ("in"); return SUCCESS ;}}
The SUCCESS returned in this method is an enumerated value. You can also return other constants.
Step 3: configure action. Create a struts. xml file under the src folder. The xml file must reference A dtd file that standardizes the xml file. This file is stored inIntroduce copy to your struts2-core-2.3.24.jar under the struts-default.xml package
Struts. xml.
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd">
After importing the specifications, you can configure the xml document.
When you configure the xml document, you may not be prompted automatically.
When you configure the xml file, you may not be prompted automatically. If you do not want to manually write the xml file, you need to configure the automatic prompt. In window --> Preference --> xml catalog
Click Add.
Note:
The key is copied by yourself. It is the dtd File path you introduced. After you copy the path, click File System...
You try againUnzip the struts2-core-2.3.24.jar, select the dtd file, and click OK;
The struts. xml file I configured is as follows:
<? 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> <! -- Extends = "struts-default" requires an interceptor --> <! -- Result type the default value is forwarding --> <package name = "mypackage" extends = "struts-default"> <action name = "login" class = "com. action. loginAction "> <result name =" success "type =" redirect ">/index. jsp </result> </action> </package> </struts>
Step 4: configure the struts2 filter and configure the filter in web. xml. This is the four steps for matching the struts environment.
<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>*.action</url-pattern> </filter-mapping>