Struts principles and applications (2)

Source: Internet
Author: User
Chapter 3: struts Configuration

Struts Principle and Practice

Struts can run in any web iner that supports jsp1.2 and servlet2.3.

Struts submits all requests to the same central controller, org. Apache. Struts. Action. actionservlet class.

Web. xml configuration

<Servlet-Name> action </servlet-Name>
<Servlet-class> org. Apache. Struts. Action. actionservlet </servlet-class>
<Init-param>
<Param-Name> config </param-Name>
<Param-value>/WEB-INF/struts-config.xml </param-value>
</Init-param>
<Load-on-startup> 2 </load-on-startup>

<Servlet-mapping>

A standard URL style using struts is as follows:

Extended ing: http://www.my _ site_name.com/mycontext/actionname. Do

Path ing: http://www.my _ site_name.com/mycontext/do/action_name

 

<Servlet-Name> action </servlet-Name>
<URL-pattern> *. Do or/do/* </url-pattern>
</Servlet-mapping>

Struts running

Struts first calls the init () method of actionservlet 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:

<? XML version = "1.0" encoding = "UTF-8"?>
<! Doctype Struts-config public "-// Apache Software Foundation // DTD struts configuration 1.1 //" http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd ">
<Struts-config>
<Data-sources/> // define the data source
<Form-beans/> // define actionform
<Global-exceptions/> // defines a global exception
<Global-forwards/> // defines the global Redirection URL
<Action-mappings/> // define action
<Controller/> // configure the Controller
<Message-resources/> // configure the resource file
</Struts-config>

Struts consists of the preceding parts. The most important ones are action and form. The following describes the processing process.

When a request is submitted to actionservlet, actionservlet searches for the corresponding form and action. First, it maps the submitted request object to form ., Then, the form is passed to the action for processing. Action to get the form, the XML mapping, request, response four objects, and call the execute () method, then return a forward-URL (corresponding view) to the actionservlet, and finally return to the client.

Let's look at a simple example.

 

Chapter 4: Example 1: Basic Framework

Struts Principle and Practice

Note: instance 1 is the simplest struts program. It only uses one form and one action
The function is to pass the value entered on the homepage to the action and return the result after judgment. If it is null, empty is returned.
The Code is as follows:

Input. jsp:
<Form method = "Post" Action = "/example. do "> <br/> enter the value <br/> <input type =" text "name =" test "/> <br> /> <input type = "Submit" name = "Submit"> <br/> <input name = "reset" type = "reset"> <br/> </form> <br/>

Struts-config.xml:
<Struts-config> <br/> // configure formbean <br/> <form-beans> <br/> <form-bean name = "exampleactionform" type = "com. zotn. struts. example1.exampleactionform "/> <br/> </form-beans> <br/> // configure action <br/> <action-mappings> <br/> <action name =" exampleactionform "Path ="/example "type =" com. zotn. struts. example1.exampleaction "> <br/> // foward in action <br/> <forward name =" foward "Path ="/foward. JSP "/> <br/> </Action-mappings> <br/> </Struts-config> <br/>

Action:
Public class exampleaction extends action {<br/> Public actionforward execute (actionmapping, actionform, httpservletrequest request, httpservletresponse response) {<br/> // obtain the corresponding form <br/> exampleactionform EAF = (exampleactionform) actionform; <br/> // obtain the input test <br/> string test = EAF. gettest (); <br/> // judge and put the value in the request <br/> If ("". equals (TEST) {<br/> request. setattribute ("test", "EMPTY"); <br/>} else {<br/> request. setattribute ("test", test); <br/>}< br/> // use mapping to find the corresponding URL and return actionfoward <br/> return actionmapping. findforward ("foward"); <br/>}< br/>

Formbean:
Public class exampleactionform extends actionform {<br/> private string test; <br/> Public String gettest () {<br/> return test; <br/>}< br/> Public void settest (string test) {<br/> This. test = test; <br/>}< br/>

 

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.