Struts2 of SSH learning (i.)

Source: Internet
Author: User

Brief introduction of 1.strust2 frame history

WebWork is developed by the Opensymbony organization and is a Java EE web framework dedicated to component and code reuse. (also a mature web-based MVC framework).

Mvc:model is a model view that is a view control is a controller, which is a framework pattern.

It is worth mentioning that the three-tier architecture of Web projects (and MVC is two completely different architectures)

1. Presentation layer: The STRUTS2 framework is where it works, a framework for the MVC three-tier architecture of the view layer. In fact, in mobile development, such as Android development, but also in the presentation layer work, we can also use the MVC three layer architecture to develop programming.

2. Business Logic Layer: Service layer, processing business logic, such as determining whether the user name exists, the password is correct, whether the permissions have, whether the account is frozen, the account is abnormal, whether the user points to achieve operational requirements, and so on, many for some to perform a certain operating conditions of judgment.

3. Data Access layer: That is often said DAO layer, specifically for dealing with the database interaction, such as common additions and deletions and other changes. Jdbc/hibernate technology is used in this layer.

In the project, these different layers of representation, in fact, in our own built in the different package to write the respective hierarchy of the Java classes used. For example, in the project we have built three package, the role of the Java class in three series is the code of the presentation layer, the code of the business logic layer, the data Access layer code.

Summary: With the development of the WebWork framework, the STRUTS2 framework is derived, so the STRUTS2 framework is webwork upgrade rather than a new frame, so the stability, performance and so on have a good guarantee, It also absorbs the advantages of both the struts1 frame and the webwork. So the STRUTS2 framework is also a web-based MVC framework.

However, it is worth noting that the STRUTS2 framework is not a continuation or upgrade of the STRUTS1 framework, and the design concepts are completely different. Instead of inheriting from Struts1, it is a very good framework that combines the virtues of struts1 and xwork.

2.struts2 framework introduced into the Web Project 2.1 STRUTS2 related jar packages introduced into the project

We can download all packages on the Apache website, with not only the jar packages that struts2 must have, but also examples for developers to learn. We will unpack the all package open the app folder, with a good pressure to open (no decompression) Struts2-blank.war file, into the Web-inf/lib, copy all the jar packages under the file into the Web project.

Copy the jar package directly into the Lib under Web-inf under Project WebContent.

2.2 Configuring Struts.xml

Put the STRUTS2 configuration file directly under SRC, the name must be called Struts.xml (because the runtime configuration file will be automatically read by the framework, to the back of the use of annotation technology can greatly reduce the amount of XML file writing work). It should be noted that if the Struts.xml file does not automatically elevate the label content, the corresponding DTD file needs to be configured.

<?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" />    <constant name="struts.devMode" value="true" />    <package name ="default" namespace="/" extends="struts-default">         <action name="first*"             class="com.verney.struts2.action.FirstAction"method="{1}">            <result name="success">/WEB-INF/jsp/first.jsp</result>        </action>    </package></struts>    

Constant is configured with a number of constant tags, such as the constant label in the default configuration default.properties file, you can set your own. "Struts.enable.DynamicMethodInvocation" indicates whether a dynamic method call is allowed, where false indicates a prohibition; "Struts.devmode" value= "true" means developer mode.

Multiple package tags can be configured in one Struts.xml file, and multiple action tags can be configured in a single tag, and multiple result can be configured within an action tag.

It is important to note that the namespace namespace problem, namespace belongs to the package's label, uniquely identifies the current package. Used to manage the path of the request action. The so-called package packages represent the configuration of the request action path. The "/" here represents the previous level. extends= "Struts-default" means that the current package inherits another package named Struts-default, This package is defined in the Struts-default.xml file in the core jar of STRUTS2, which defines a number of default configurations

First* is the future in the browser to enter this action name, if the input is First1, then * represents 1, if the input firstlogin, then the login is indicated. But if multiple actions that need to be configured do not have a very obvious rule, then this is not the way to configure it for long.

The action tag is used to configure the name of the action and the source path of class, and whenever a request is made to the name, it is struts.xml to find it, and then to load the method to the corresponding class path, method= "{1}" Indicates that the first first is matched and the name of the method is passed. After each action is accessed, there is a return value for the string type, and the return value corresponds to the name of result, which determines the path of the jump. Of course two different names of the action can be corresponding to the same Java class, this time the role of the method tag is reflected:

<action name="test1" class="类A" method="login"><action name="test2" class="类A" method="register">

The action of using Test1 and test2 two different names is to access two methods in the same class: the Login and register methods. However, the login method and the Register method must be similar to the Execute method, all of which are strings of type string.

The result tag represents the path of the jump, and if excute in the action class returns "Success" (the argument must be spelled, these parameters are already defined in the frame default.properties), jump to/web-inf/ Go to the jsp/first.jsp page.

The format of the request action Path is: namespace + "/" +action name+ ". Action"

These struts.xml configuration files can be found in the Struts2-blank.war of all packages downloaded from the official website, and can save these commonly used and not easy to remember files, when needed to use directly to use.

2.3 Configuring the STRUTS2 framework filter in the Web. xml file

Because most of the use of frames is configured in the Web. xml file, we have to make sure that you build the. xml file when creating a new project. The purpose of the configuration filter is to intercept the action in the STRUTS2 frame. Note If you do not configure this filter,struts2 frame, you will not be able to work.

<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>
2.4 Writing the action method in the framework

There is a Java class called action in the Struts framework, which is like a Java class called a servlet in the EE project, in fact, in the STRUTS2 framework, it encapsulates the relevant content of the servlet, Except that the STRUTS2 framework extends many other functions in addition to the functionality of the servlet, and it also greatly simplifies previous tedious operations in the servlet. The reason why the action is more powerful than the servlet feature is that we have configured a special filter in the STRUTS2 framework in the Web. xml file, and the STRUTS2 framework can add a lot of functionality to the action when it's blocked.

The action class you write is best placed in an action package, where the name of the action class is best to end with an action, and the specification is written for easy administration.

Write the action method in the framework:

    • 1) method Inheritance Actionsupport class

    • 2) Override the Excute method, the type must be of type string, the name must be Excute, and must be no parameter.

      public class FirstAction extends ActionSupport{    private static final long serialVersionUID = 1L;    private String excute() {        System.out.println("excute");        return SUCCESS;        }    }

There are also 5 static properties of type string:

public static final String SUCCESS = "success";public static final String NONE = "none";public static final String ERROR = "error";public static final String INPUT = "input";public static final String LOGIN = "login";

Create a new first.jsp, deploy the project to the server, turn on the server, and try the browser access.

This struts2 even the introduction of success. Of course, the only thing mentioned here is the fur of struts2, the strength of this framework has not been reflected, we need to learn slowly. In fact, we can learn a lot about the framework by carefully reading the Default.properties properties file. A framework is recognized by many people, because it does a lot of development process cumbersome and have to do things, highly encapsulated under the premise is the embodiment of design ideas, we learn the framework is to learn the predecessors excellent design ideas, so even when the other framework to use the time will be very easy to get started.

Struts2 of SSH learning (i.)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.