Struts configuration and Explanation

Source: Internet
Author: User
Tags tld

Struct:

(1) struts is a web development framework that complies with JSP model2 development specifications.

(2) Web framework based on MVC development specifications

Advantages of the MVC hierarchical development model:

(1) reusable code
(2) Implement loose coupling between layers
(3) Each layer is independent, self-contained, and has a low coupling degree.
Advantages: easy to manage, flexible configuration, and convenient Modification
Disadvantages: less interaction points, concentrated interaction, and sometimes inconvenient Interaction
Implementation: Strong isolation reduces and centralizes interaction points, and uses the interface technology to process coupling (Keyword: Interface) which is not easy to modify. However, it is also required in some places, management requires loose coupling between layers to implement data and real logic, which is isolated from business logic.

Framework:

(1) high-level tools for project development (Nature)

(2) provide the prepared software architecture and related software packages

Advantages:

(1) focus more on software development;
(2. customizes many repeated parts to avoid errors and facilitate maintenance;
(3) standardized the development process, greatly accelerating the maintenance and modification speed, simplifying the maintenance process, improving efficiency, and shortening the cycle.

 

 

1) Struts framework:
Purpose: display the Transfer Parameters of the encapsulation parameter of the user form Foreground Data that accepts the web action (Conversion packet) result data
Ii) features:
1) An open-source project organized by Apache: Jakarta-Struts;
2) all advantages of MVC are provided based on the MVC design model;
3) complies with sun JSP model2 development specifications;
4) heavy and scalable Web framework;
5) Advantages of servlet | JSP technology can be comprehensively utilized;
3) workflow:
<Struts-config.xml>
JSP ------------ actionform ------------ action --------- BO---------DAO----------PO.............DB

4) procedure:
Action I) [add struts capabilities to a web project]
Solution 1: add the myce ce plug-in
① Project name -- right key -- myeclipse --- add struts capabilities
Maintenance upgrade --- struct1.1 --- 1.2 ---- tomcat installation directory ---- webinf ---- lib ---- Replace the jar package with 1.2.
Solution 2: manually add
① Open a new package name path under SRC:
② Create an applicationresources. properties under com. Hj. Struts;
Install struts jar package: directly to the camcat installation directory --- webinf --- lib --- copy to WEN-INF \ Lib;
4 build struts-config.xml: method 2 )....... -- New XML (Basic templates) ----- from a DTD file --- XML catalog entry --- Apache DTD struts config 1.2 --- OK
⑤ Get the validator-rules.xml from the jar package and copy it to the WEB-INF;
6 from the jar package to obtain the struts tag Library: six large. TLD file into the WEB-INF;
Note: 1.1 indicates 6 TLD files; 1.2 indicates 5 TLD files; // The templates library is missing;
7. Configure web. xml:
Method 2) use the design tool of Web. XML to configure Servlet and init-param (config, debug: 3, and automatically output load-on-startup: 0 and detail: 3)

Action II) add struts components to a web project
Three core components: [JSP], [actionform], and [action]
[Three methods for creating components]:
Method I) project name --- Right key --- New --- Other --- myeclipse --- web struts --- struts1.1/1.2 --- nine items --- Action, form, form and Action & JSP ,..., message resources, etc.
Method II) <struts Graphical Editor>: struts-config.xml --- design --- New

Independent component creation
(1) Create JSP Components
Solution 1) Use common JSP labels
(1) create a JSP page: select the template --- "Default JSP template"
(2) Add form: Open the snippets tool:
Note: The name of the form element attribute key must be consistent with the attribute name in actionform.
(3) Add action =... target URL in Form
Action = "/WEB Project name/action Action Logic name. Do"

Solution 2): JSP with Struts labels
(1) create a JSP page: select a template:
"Standard JSP using struts 1.2" 1.1 (automatically added template)
(2) Add form: Use snippets tool to add: (in HTML templates --- double-click input form template)
(3) Add action = .. target urlaction = "/action Action Logic name" in Form"
Struts labels must be run on the server first, parsed to common HTML, and then executed on IE. If no backend is configured, an error is reported on the page.

(2) Configure Struts-config.xml
① JSP
(2) Configure <action> under <action-mappings> In struts-config.xml

<Action Path = "/nskp"
Parameter = "flag"
Attribute = "fptjform"
Name = "fptjform"
Scope = "request"
Type = "org. springframework. Web. Struts. delegatingactionproxy"
>

Path = "/Action Logic name" // consistent with <form action name. To search for action with the same name, start struts action (conversation) to process the request;
Name = "actionform logical name" // must be consistent with a <form-bean name = "logical name. Used to find the actionform class, generate an object, and encapsulate the front-end form data;
In struts-config.xml, <form-bean name = "" type = "">

<Form-bean label name = "actionform logic name" type = "package name. actionform class name"/>
--- The attribute encapsulate form Element Data and the validate () method can perform format verification;

(4) with "struts-config.xml" in the <attribute = "key name"
// The key name of the actionform object stored in the scope, which is generally the same as the logic name of actionform;

⑤ ............... Scope = "request | session"; (only two scopes exist)
6. Create an actionform class;
(1) Under SRC: Build package name path: COM. Hj. Struts. Form
(2) Write class extends acionform // abstract class
(3) Write global attributes and set them to private:
(4) rewrite the validate () method (based on project requirements)
Content: Format verification logic,
Returned value: If the returned actionerrors is null or empty, the format verification is successful;
Contains actionerror | when an actionmessage object is specified, format verification fails;
Failed to go to <action input = "URL of the error page">

(5) rewrite the reset () method in action
This. property name = NULL; // set the property to the initialization value;
// Function: the attribute is reset to null. It is used to restore the attributes of the foreground bean of actionform to the default value, so that the attribute encapsulates new data;

(6) with "struts-config.xml" in the <validate = "false | true" // actionform object, validate () method is executed;

(7) With "struts-config.xml" in the error page url failure ";

(8 )............. <Type = "package name. Action class name" --- the action class is removed after the validate () method in the actionform object is verified successfully.

(9) Write Action class
(1) New Class
(2) extends action
(3) override the execute () method. If it is a self-written method, it must inherit the dispatchaction class.
(4) Writing various business functions:
// Technical point 1: receive form data at the front end
Loginform LF = (loginform) form; (or use request. getparameter ("name") or Foreground Data)
String username = lf. GetUserName ();
String Password = lf. GetPassword ();
System. Out. println (username );
System. Out. println (password );
5) Action:
Return an actionforward object to determine the next forwarding target;
// Actionforward object Generation Method 1:

)
// Locate the forward tag information configured in the struts-config.xml through the configuration object,
// Obtain the forwarding target URL
Forward with add match in struts-config.xml <forward name = "Forward logic name" Path = "target page url"/>
This tag configuration automatically generates an actionforward configuration object, which is put into actionmapping and passed to action for use in the action;

Writable Write result page;
Target page url

 

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.