JSP引入外部文件遇到的一个问题_html/css_WEB-ITnose
今天搭建了一个项目,框架式Spring、SpringMVC和Mybatis,在JSP中引入js文件和css文件的时候发现一直引入不了,后来发现是SpringMVC的一个配置导致的,也就记录一下,目录结构是这样的:
project.jpg
引入外部文件的方式:
<script type="text/javascript" src="${ctx}/js/jquery.min.js"></script><script type="text/javascript" src="${ctx}/js/bootstrap.min.js"></script><script type="text/javascript" src="${ctx}/js/bootstrap-paginator.js"></script><script type="text/javascript" src="${ctx}/js/wiki.js"></script><link rel="stylesheet" type="text/css" href="${ctx}/css/bootstrap.min.css" /><link rel="stylesheet" type="text/css" href="${ctx}/css/shopin-list.css" />
在jsp中定义了ctx的路径,也就是项目的根路径:
<%request.setAttribute("ctx", request.getContextPath());%>
这样也是为了方便管理,在实际应用中,我们只需要这样引用就可以了:
<%@ include file="common/header.jsp"%>
但是自己觉得这样没什么问题,但是fireBug一直报404错误,找不到js文件和css文件,我也找不到头绪,也想了很久,才发现在web.xml中拦截了所有的路径:
<servlet-mapping> <servlet-name>shopin-wiki</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
又在springmvc配置文件中给做了资源映射:
<mvc:resources location="/WEB-INF/css/" mapping="/css/**"/><mvc:resources location="/WEB-INF/js/" mapping="/js/**"/>
但是路径是在/WEB-INF/css/下,就会导致我现在放在webapp目录下的文件找不到。
总结
因为web.xml在项目启动的时候会加载在web.xml文件中配置的springmvc控制器,默认拦截了所有的url,但是静态资源我不想让拦截,我就可以在springmvc配置文件中配置资源映射,

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.

The article discusses the HTML <progress> element, its purpose, styling, and differences from the <meter> element. The main focus is on using <progress> for task completion and <meter> for stati

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

The article discusses the HTML <datalist> element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

The article discusses the HTML <meter> element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates <meter> from <progress> and ex

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

AnexampleofastartingtaginHTMLis,whichbeginsaparagraph.StartingtagsareessentialinHTMLastheyinitiateelements,definetheirtypes,andarecrucialforstructuringwebpagesandconstructingtheDOM.

GiteePages static website deployment failed: 404 error troubleshooting and resolution when using Gitee...
