在/*上映射全局前端控制器servlet时如何访问静态资源我将SpringMVC调度器映射为全局前端控制器servlet/*.<servlet>
<servlet-name>home</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> </servlet> <servlet-mapping>
<servlet-name>home</servlet-name>
<url-pattern>/*</url-pattern> </servlet-mapping>但是,此映射将停止对CSS、JS、图像等静态文件的访问,这些文件都位于/res/文件夹。我怎么能访问他们呢?
3 回答
拉丁的传说
TA贡献1789条经验 获得超8个赞
<servlet-mapping> <servlet-name>home</servlet-name> <url-pattern>/app/*</url-pattern> </servlet-mapping>
白衣非少年
TA贡献1155条经验 获得超0个赞
url-pattern
/pages/*
/static
Filter
/*
<filter> <filter-name>filter</filter-name> <filter-class>com.example.Filter</filter-class></filter><filter-mapping> <filter-name>filter</filter-name> <url-pattern>/*</url-pattern></filter-mapping><servlet> <servlet-name>controller</servlet-name> <servlet-class>com.example.Controller</servlet-class></servlet><servlet-mapping> <servlet-name>controller</servlet-name> <url-pattern>/pages/*</url-pattern></servlet-mapping>
doFilter()
:
HttpServletRequest req = (HttpServletRequest) request;String path = req.getRequestURI().substring(req.getContextPath().length());if (path.startsWith("/static")) { chain.doFilter(request, response); // Goes to default servlet.} else { request.getRequestDispatcher("/pages" + path).forward(request, response);}
/pages
"/static"
"/pages"
init-param
添加回答
举报
0/150
提交
取消