Struts Principle and Application (2)

zhaozj2021-02-11  229

Chapter 3: Struts Configuration

Struts PrinciPle and Practice Struts can run in any web container supporting JSP1.2 and servlet2.3

Struts submits all the requests to the same center controller, org.apache.struts.Action.ActionServlet class

Web.xml configuration

Action org.apache.struts.Action.ActionServlet config < Param-value> /Web-inf/struts-config.xml 2

A standard URL style using Struts is as follows:

Extended mapping: http://www.my_site_name.com/mycontext/Actionname.do

Path mapping: http://www.my_site_name.com/mycontext/do/action_name

Action *. do or / do / *

Struts run

Struts first calls the ActionServlet's init () method when the Container is started. Initialize various configurations. These configurations are written in the struts-config.xml file.

A standard Struts-Config file contains the following structure:

// Define Data Source // Define Actionform // Define Global Exception // Define global steering url // Configuring action // Configuring Controller // Configuring Resource File

Struts consists of several parts described above. The most important one is Action and Form. The following is simply described below.

A request submits to the ActionServlet, ActionServlet looks for the corresponding Form and Action, first map the submitted request object to the Form. , Then pass the Form to the action for processing. Action gets FORM, the XML's mapping, request, and the execute () method then returns a Forward-URL (corresponding view) to the ActionServlet, eventually returned to the client. Let's take a simplest instance.

Chapter 4: EXAMPLE 1: Basic Framework

Struts PrinciPle and Practice Description: Example One is the simplest Struts program. It only uses 1 Form and 1 Action function to pass the value of the page input to the Action, and return the result after judgment. If it is freedom to return EMPTY code as follows:

INPUT.JSP:

please input the value



Struts-config.xml:

// Configure FORMBEAN

// Configure Action

// Action internal foward

Action: public class example exAset Extends action {

Public ActionForward Execute (ActionMApping ActionMApping, Actionform Actionform, httpservletRequest Request, httpservletResponse response) {

// Get the corresponding form

EXAMPLEACTIONFORM EAF = (ExampleActionform) Actionform;

/ / Get the input Test

String test = eaf.gettest ();

// Judgment and put the value into the request

IF ("" .equals (test)) {

Request.setattribute ("test", "empty");} else {

Request.setttribute ("Test", Test);

}

// Look for the corresponding URL by mapping, return ACTIONFOWARD

Return ActionMapping.FindForward ("Foward");

}

}

FORMBEAN: PUBLIC CLASS EXAMPLEACTIONFORM EXTENDS ACTIONFORM {

PRIVATE STRING TEST;

Public string gettest () {

Return Test;

}

Public void settest (String test) {

THIS.TEST = TEST;

}

}

转载请注明原文地址:https://www.9cbs.com/read-4963.html

New Post(0)