Struts-helloapp Study of Xiao Kee

Source: Internet
Author: User
Tags contains html form http request key access




<BEAN:MESSAGE&GT: For output localized textual content, its key attribute specifies the message key, and the text content that matches the message key is derived from the specialized resource Bundle

<bean:write&gt: The property value used to output JavaBean. In this case, it is used to output the UserName property value of the Personbean object: <bean:write name= "Personbean" property= "UserName"/>

<logic:present> tags are used to determine whether JavaBean exists within a specific range, and only when JavaBean exists will the contents of the label body be executed.





The controller component includes the Actionservlet class and the action class. The Actionservlet class is the struts framework, which is the control hub of the entire struts framework, and usually does not need to be extended. The Struts framework provides an extended action class for handling specific HTTP requests.





When the user submits an HTML form, the Struts framework automatically assembles the form data into the Actionform bean. The properties in the Actionform bean correspond to field one by one in the HTML form. The struts framework will then automatically invoke the Actionform Bean's Validate () method for form validation. If the Actionerrors object returned by the Validate () method is null or does not contain any Actionmessage objects, it means there is no error and data validation passes. If the Actionmessage object is included in the Actionerrors, a validation error occurs, the Struts framework saves the Actionerrors object to the request scope, and then forwards it to the appropriate view component, which passes through the < The html:errors> tag displays the error messages contained in the Actionerrors object in the request scope, prompting the user to modify the error.





The Action class works: When the Actionform Bean is created and the form validation passes, the struts framework invokes the Execute () method of the action class. The Execute () method contains the following parameters:

Actionmapping: Contains the configuration information for this action, and corresponds to the <action> element in the Struts-config.xml file.

Actionform: Contains the user's form data, and when the struts framework calls the Execute () method, the data in Actionform has been validated by the form.

HttpServletRequest: The current HTTP Request object

HttpServletResponse: The current HTTP response object

The Execute () method of the Action class returns the Actionforward object, which contains the request forwarding path information.





When the Helloaction class invokes the HttpServletRequest setattribute () method, it is necessary to provide a property key named "Personbean" when the Personbean object is passed to hello.jsp:

Request.setattribute ("Personbean", Pb);

Hello.jsp reads the Personbean object through this property key named "Personbean":

<logic:present name= "Personbean" scope= "Request" >


Hello <bean:write name= "Personbean" property= "UserName"/>!<p>


</logic:present>





<action Path = "/helloworld"//Specify the path of the request access action

Type = "Hello." Helloaction "//Specify the full class name of the action

Name = "Helloform"//Specifies the Actionform Bean to be passed to the action

scope = "Request"//Specify the storage scope of the Actionform bean

Validate = "true"//Specify whether to perform form validation

input = "/hello.jsp" >//Specify the forwarding path when form validation fails

<forward name= "SayHello" path= "/hello.jsp"/>//defines a request forwarding path

</action>

The <action> element in this example is configured with the Helloaction component, and the corresponding class is hello. Helloaction, the request access path is "HelloWorld", and when the action class is invoked, the struts framework should pass the Helloform bean that already contains the form data to it. The Helloform bean is stored in the request scope and should be validated before calling the action class. If form validation fails, the request is forwarded to the page hello.jsp that the user entered, allowing the user to correct the error.


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.