今天搭建了一个项目,框架式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配置文件中配置资源映射,

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 using HTML5 form validation attributes like required, pattern, min, max, and length limits to validate user input directly in the browser.

The article discusses the <iframe> tag's purpose in embedding external content into webpages, its common uses, security risks, and alternatives like object tags and APIs.

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

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

Article discusses best practices for ensuring HTML5 cross-browser compatibility, focusing on feature detection, progressive enhancement, and testing methods.

The article discusses the viewport meta tag, essential for responsive web design on mobile devices. It explains how proper use ensures optimal content scaling and user interaction, while misuse can lead to design and accessibility issues.

This article explains the HTML5 <time> element for semantic date/time representation. It emphasizes the importance of the datetime attribute for machine readability (ISO 8601 format) alongside human-readable text, boosting accessibilit


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver CS6
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

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

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software
