Struts1.x Series Tutorials (17)

Source: Internet
Author: User

Struts1.x Series Tutorials (17): Using Includeaction and Forwardaction classes to include and transfer to Web resources

First, use the Includeaction class to include Web resources

Although you can use the <jsp:include> tag in a JSP page to include another Web resource, the struts framework advocates that when you include a Web resource, you should go through the struts controller before the struts controller is responsible for including the Web resources. The Org.apache.struts.actions.IncludeAction class provides functionality that includes other Web resources. Use the Includeaction class to configure <action> elements in Struts-config.xml. The code for configuring the Includeaction class is as follows:

<action path= "/include" type= "Org.apache.struts.actions.IncludeAction"
input= "/firstvalidator.jsp" validate= "false"
Name= "Firstvalidatorform" parameter= "/firstvalidator.jsp"/>

You should be aware of two points when configuring the above code:

The 1.firstvalidator.jsp page uses Firstvalidatorform for data validation, and all should set the Validate property of the <action> element to "false." Otherwise, validation is performed when the include action is run. All the properties in the Firstvalidatorform are not assigned. Therefore, each property cannot pass validation. The reader can set the Validate property to "true" to see what happens.

2. The functions and effects of the Includeaction class are exactly the same as the <jsp:include> labels. This means that the response header cannot be changed in other Web resources contained in Includeaction. This will not change the value of the Content-type field. Therefore, according to the above settings to run the program, all the Chinese information will display garbled. There are many solutions, such as implementing a subclass of the Includeaction class yourself, and setting the Content-type value in the subclass's Execute method. Or it's easier to add the following code to the filter (Encodingfilter):

Response.setcharacterencoding ("GBK");

Enter http://localhost:8080/samples/include.do in IE to access the include action.

Although you can use the <jsp:forward> tag to forward requests to another Web resource in a JSP page, the Struts framework advocates that when you forward a Web resource, you should go through the Struts controller before forwarding the Web resource to the struts controller. The Org.apache.struts.actions.ForwardAction class provides the ability to forward other Web resources. Use the Forwardaction class to configure <action> elements in the Struts-config.xml file. The following code demonstrates how to use the Forwardaction class in a <action> element to contain additional Web resources:

<action path= "/forward" type= "Org.apache.struts.actions.ForwardAction"
input= "/firstvalidator.jsp" validate= "false"
Name= "Firstvalidatorform" parameter= "/firstvalidator.jsp"/>

Because a forwarded web resource can change the value of the Content-type field, when forwarding using the Forwardaction class, You do not need to change the value of the Content-type field differently (because Content-type values are already set in the Firstvalidator.jsp page).

Enter http://localhost:8080/samples/forward.do in IE to access forward actions.

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.