After struts/Servlet and action are transferred to JSP, the path problem (struts2, JSP path, Action path, Action jump, relative path, absolute path)

Source: Internet
Author: User

Q: How do I handle the action path when using struts2? Also, in the JSP to which action is transferred, How to Write JS, CSS, and Graph

Film path? (For example, Access

Http: // localhost/project/listuser. Action to http: // localhost/project/user

/Listuser. jsp. At this time, the browser stays at http: // localhost/project/adduser. Action. At this time, the original image location of JSP may all go wrong)

A: action does not need to care about the path. We do not use the action namespace. Because such a match is used:
<Filter-mapping>
<Filter-Name> struts2 </filter-Name>
<URL-pattern> *. Action </url-pattern>
</Filter-mapping>
Regardless of the path on the front, struts2 only looks at the matching.

To solve the JSP path problem, I think the following methods are basically perfect (if there is a problem, please point it out ):
Step 1: add the base tag and a Java
<%
String Path = request. getcontextpath ();
String basepath = request. getscheme () + "://"
+ Request. getservername () + ":" + request. getserverport ()
+ Path + "/";
%>
<Base href = "<% = basepath %>">

Step 2: Use "semi-absolute path" for all paths ". The so-called semi-absolute path is what I am talking about. Example: <LINK rel = "stylesheet" type = "text/CSS" href = "style/table.css"/>
This is relative to table.css in the style directory of the previous jspdirectory.
On the premise that struts2 and base are configured, the browser will interpret it as http: // localhost/project/style/table.css.
That is to say, you need to write the absolute path from the root directory of the project, but remove "/"

Step 3: Skip all actions. do not include the directory location for the action path submitted by form. Write only the action name, for example, adduser. Action.

In this way, the browser stays at the http: // localhost/project/XXXXX. Action layer.

In addition, I found another more natural way to make the JSP originally located in the relative path available, without having to modify the links one by one:

Step 1: add the base in the JSP head, but change getcontextpath to getrequesturi.
D
<%
String Path = request. getrequesturi ();
String basepath = request. getscheme () + "://"
+ Request. getservername () + ":" + request. getserverport ()
+ Path;
%>
<Base href = "<% = basepath %>">
Note that the request. getrequesturi is the absolute path of the JSP.
For example, the address bar of the browser is http: // localhost: 8084/project/listuser. Action.
Base is <base href = "http: // localhost: 8084/project/admin/listuser. jsp">

Step 2: Use relative paths to access all resources on the JSP page.

Step 3, you don't have to worry about the action path.

This method is compatible with the use of action and the use of action

In addition, use the absolute path of the project when configuring the JSP path in struts. xml. For example, if listuser. jsp is in the Admin directory under the root directory, write

<Result>/admin/listuser. jsp </result>

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.