1.通过能过滤器添加
public class ContextPathFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
throws IOException, ServletException {
String path = ((HttpServletRequest)servletRequest).getContextPath(); String basepath = path; servletRequest.setAttribute("CP", basepath); filterChain.doFilter(servletRequest, servletResponse); } @Override public void destroy() { } }当然了还得添加到web.xml
<filter> <filter-name>ContextPathFilter</filter-name> <filter-class>com.person.core.web.ContextPathFilter</filter-class> </filter> <filter-mapping> <filter-name>ContextPathFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>剩下的前台引用就可以了
<link rel="stylesheet" type="text/css" href="${CP}/css/style.css"/>
2.就是前端页面直接获取(摘自 点击打开链接)<script type="text/javascript"> var basePath = '${CP}';</script>