Question about URL passing in Struts (redirect attribute of the forward tag)

Source: Internet
Author: User
Tags url forwarding

 

In a struts project, the following problem occurs:

The user's logon page address is:

Http: // localhost: 8080/sduprs/user/user_login.jsp

The address displayed after logon is:

Http: /localhost: 8080/sduprs/userLogin. do

What's even more tragic is that we use the frame framework in the background. In this way, all the operation addresses after logon are shown as the above addresses. If you are not satisfied, change the address to the actual address. I checked the information on the Internet for half a day and found that it was a url forwarding problem. The specific point is the problem of ActionForward. The following information about ActionForward:

(1) ActionForward is one of Struts's core classes. Its base class has only four attributes: name/path/redirect/classname. During Struts-based Web application development, after the Action is completed, the program uses the Struts-config. link xml to the specified ActionForward and upload it to the core class ActionServlet of Struts. The ActionServlet uses the PATH provided by ActionForward to pass the control to the next step. ActionForward controls the next step of the program. ActionForward indicates the URI of an application, which includes the path and parameters.
(2) In addition to setting parameters in the struts-config.xml and page, the ActionForward parameter can also be created by adding parameters in the Action class or re-creating an ActionForward in the Action.
(3)There is an important attribute redirect in ActionForward. When redirect is set to false, all content stored in the http request and request context is saved and only available in the same application. When redirect = true, the Web client initiates a new http request. The requested resources can be in the same application or not, and the original request parameters are not saved, the original request context is also cleared. The new http request only contains the parameters contained in the path attribute of ActionForward. In the same application, the context of user sessions is maintained.
(4) ActionForward is divided into Global Forwarding and local forwarding. The findForward method of the Mapping object of the Action first checks the local forwarding list. If the lookup fails, it searches the Global Forwarding list. In the writing format, we generally define the global variable as a String constant to avoid misunderstanding and make the forwarding list structure clear and easy to read.

In fact, struts has two targeting methods: Forwarding and redirection (redirect ). When we use redirection, the page is completely redirected to the request view. The url on the browser is also updated. This advantage is that the request is completely transferred, but the request cannot be used. setAttribute transmits the parameter value. When we use forward, the request of this request is passed to the jsp or servlet to be targeted. Execute the requested jsp or servlet at the same time, and the result is displayed. However, the browser still displays the request url, not the result url. The advantage of this method is that the request value can be passed to the requested jsp or servlet. setAttribute. The disadvantage is that the url is still the requested url, not the result url. If it is refreshed, the request will be executed again.

According to the above introduction, I found that you must be cautious when using redirect. After carefully troubleshooting the login page action, we found that only the user variable was written to the session and no request. setAttribute was used to pass the parameter. So we set the redirect attribute of ActionForward in the struts-config.xml to true:

In this way, the problem I encountered will be solved.

Related Article

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.