Struts 2 static resource mapping code example

In the struts 2 framework, there are some common static content that it needs, such as JS files and some CSS files. When the framework needs these static contents, filterdidcatcher will automatically provide them to us. So how does filterdidcatcher know that we are requesting static content? As long as any request starts with "/ struts /", the filterdidcatcher will think that it is requesting static content. After identifying that the request is a request for static resources, how does the filterdidcatcher match and find static resources? Here are two key points:

1. Determine the resource path to request. The filterdidcatcher intercepts the content after / struts / as the requested resource. For example, now the request is / struts / XHTML / styles CSS, then filterdidcatcher will put XHTML / styles CSS as the path of the resource we want to request: styles. CSS under the XHTML directory CSS file.

2. Where to find the requested static content. By default, filterdidcatcher will randomly select org. Org apache. struts2. Static and template. If we want to find static content in other packages, we need to find it on the web When configuring filterdidcatcher in XML, add a parameter "packages" to it, and then set its value to a series of package names separated by commas or spaces, as shown below:

Description: there is a directory "static" under the web application. Now to access the "top. HTML" file, that is, to access "localhost: 8080 / static / top. HTML", the server always holds a 404 error.

Reason: in the dofilter method of filterdispatcher class of struts 2, if the request is a static resource, struts 2 will judge whether the request can be processed. The code here is as follows:

Java code

Among them, the canhandle method of defaultstaticcontentloader class will judge the request path:

Java code

Here, the value of servestatic is true, and the resource to be accessed starts with "/ static", so true is returned here.

Then, you will enter the findstaticresource method of defaultstaticcontentloader class. The first line of the method is:

Java code

Here, the cleanuppath method is defined as follows:

Java code

Struts 2 truncates "/ static". In this way, when parsing later, it becomes a request for "/ top. HTML", so 404 error will be reported.

summary

Tragic mistake, thought it was a bug in my own program, and changed it for a long time. We need to strengthen our understanding of the specific implementation in open source programs. I hope this article will be helpful to you. Interested friends can refer to: detailed explanation of struts 2 using ognl to traverse map method, projection of struts 2 through ognl expression, analysis of struts 2 modifying upload file size limit method, etc. thank you for your support for programming tips website.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>