Struts learning Summary (1)

Source: Internet
Author: User
Tags tld
Document directory
  • Common configurations:

Struts is an open-source framework developed by the Jakarta project team of Apache and an implementation of the MVC design pattern.

Struts2.x is based on webwork, so struts1.x and struts2.x are very different.

1. Relationship between struts and traditional MVC

Ii. Struts Configuration

Because manual configuration of Struts is troublesome, it is generally configured through myeclipse.

After creating a web project, you need to add the struts package. You can find that some files have been created in advance, such

(1) struts-config.xml is the core configuration file in struts, in this file can be configured action and actionform;

(2) configure the actioning between actionservlet and tag library in Web. xml;

(3) applicationresource. properties is a resource file;

Note:Resource files do not support Chinese CharactersFor this reason, if you want to upload Chinese characters in the image, the conversion will be unicodecode, and the conversion will be passed through native2ascii.exe;

Common configurations:

(1) Configure actionform:


<Form-beans>

<Form-bean name = "name" type = "class"/>

</Form-beans>


(2) configure action:

<Action-mappings>

<Action attribute = "form name" input = "error page" name = "form name" Path = "Action path" Scope = "range" type = "category"/>

</Action-mappings>


(3) Configure forward:

<Action>

<Forward name = "Jump name" Path = "path"> </forward>

</Action>


(4) configure the Resource Name:

<Message-resources parameter = "resource file name"/>


(5) configure the TLD file:

Struts also has its own tag library, while the tag library description file is in WEB-INF/lib/struts-taglib-1.3.10.jar \ META-INF \ TLD;

Put the TLD file in the WEB-INF folder;

The configuration in Web. XML is as follows:

 <jsp-config>  <taglib>  <taglib-uri>struts/bean</taglib-uri>  <taglib-location>/WEB-INF/struts-bean.tld</taglib-location>  </taglib>  <taglib>  <taglib-uri>struts/logic</taglib-uri>  <taglib-location>/WEB-INF/struts-logic.tld</taglib-location>  </taglib>  <taglib>  <taglib-uri>struts/html</taglib-uri>  <taglib-location>/WEB-INF/struts-html.tld</taglib-location>  </taglib>  <taglib>  <taglib-uri>struts/nested</taglib-uri>  <taglib-location>/WEB-INF/struts-nested.tld</taglib-location>  </taglib>  <taglib>  <taglib-uri>struts/tiles</taglib-uri>  <taglib-location>/WEB-INF/struts-tiles.tld</taglib-location>  </taglib>  </jsp-config>
Iii. Introduction to struts entry labels Note: To write tags in JSP files, you must add <% @ taglib %> to import the tag library;

<HTML: Text property = "info"> Is a text box named Info;

<HTML: errors/>Displays all errors. These errors are saved through actionerrors;

<Logic: present name = "info" Scope = "page">

$ {Info}

</Logic: Present>

Indicates that if info exists, the output is displayed. If Info does not exist, it is skipped.

<HTML: Form Action = "" method = "">

Note: The submission path of the action needs to be based on the path Attribute + web of the action in the struts-config.xml. the URL-pattern in XML is determined; for example, URL-pattern = *. do; if the path attribute is/Hello, the submit path is/hello. do;

Iv. actionform

Actionform is mainly used for verification. It is the same as JavaBean. For example, if two attributes are submitted: String info1 and string info2, two attributes with the same name must be used in the class inheriting actionform and the getter and setter methods are implemented;

Actionform method:

(1) Public actionerrors validate (actionmapping mapping, httpservletrequest req); verification function:

(2) Public void reset (actionmapping mapping, httpservletrequest req); reset function;

 

Note:

(1) attributes in actionform are automatically assigned values.Without manual assignment, that is, if the form is submitted and enters the actionform for verification, the actionform attribute is assigned the submitted value;

(2) If the actionerrors returned by validate is null, the verification is successful, the action is processed; if the verification fails, the page jumps to the INPUT attribute in the struts-config.xml;

5. Get started with actionerrors and actionmessage

Actionerrors is used to save a lot of error messages; actionmessage is used to save an error message;

Common Methods of actionmessage:

(1) actionmessage message = new actionmessage (string key );

The parameter key is configured in applicationresource. properties, for example, in the resource file: Error. MSG = ABC;

When actionmessage messge = new actionmessage ("error. MSG"); is executed, ABC is saved for display;

Common Methods of actionerrors:

(1) actionerrors errors = new actionerrors ();

(2) errors. Add (string name, actionmessage message );

6. Getting started with action

Action is similar to servlet in MVC. Common Methods in action:

(1) Public actionforward execute (actionmapping mapping, actionform form, httpservletrequest req, httpservletresponse resp); accept the actionform parameters and perform some operations.

(2) Public void saveerrors (httpservletrequest req, actionerrors errors); Save the error information for <HTML: errors/> display;

(3) Public void savemessages (httpservletrequest req, actionmessages messages );

Generally, they are all super. saveerrors (request, errors); called;

VII. actionmapping

Used for redirection. Common Methods:

(1) Public actionforwardFindforward(String forward); The <forward> tag has been configured in the struts-config.xml, and the forward parameter needs to be set to the name attribute of the tag;

(2) Public actionforwardGetinputforward(); Get the error jump page set for the INPUT attribute and jump;

 

VIII. Struts Process

 

(1) JSP sends the request and the actionservlet receives it;

(2) actionservlet calls actionform through the struts-config.xml for execution (call the validate method );

(3) actionservlet calls action for execution;

(4) Jump to the action and display the result;

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.