SpringMVC对静态资源的访问(js、css、img)
在网上找了很多的内容,都没法解决,最后通过https://blog.csdn.net/wild46cat/article/details/52456715中内容解决的,在此记录一下。
项目结构:
pom.xml内容:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.demo</groupId>
<artifactId>HelloSpringMVC</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>HelloSpringMVC Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies> <dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency> <!-- Servlet LIbrary -->
<!-- http://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency> <!-- Spring dependencies -->
<!-- http://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.1.4.RELEASE</version>
</dependency> <!-- http://mvnrepository.com/artifact/org.springframework/spring-web -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.1.4.RELEASE</version>
</dependency> <!-- http://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.1.4.RELEASE</version>
</dependency> </dependencies> <build>
<finalName>HelloSpringMVC</finalName>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<url>http://localhost:8080/manager/text</url>
<username>admin</username>
<password>Pass@1</password>
<path>/${project.artifactId}</path>
</configuration>
</plugin>
</plugins>
</build>
</project>
web.xml内容:
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>HelloWorldSpring</display-name> <servlet>
<servlet-name>spring-mvc</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet> <servlet-mapping>
<servlet-name>spring-mvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping> <!-- Other XML Configuration -->
<!-- Load by Spring ContextLoaderListener -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/root-context.xml</param-value>
</context-param> <!-- Spring ContextLoaderListener -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
root-context.xml内容:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"> </beans>
spring-mvc-servlet.xml内容:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd"> <mvc:annotation-driven /> <mvc:resources mapping="/html/**" location="/html/" />
<mvc:resources mapping="/images/**" location="/images/" />
<mvc:resources mapping="/css/**" location="/css/" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"></property>
<property name="suffix" value=".jsp"></property>
</bean> <context:component-scan base-package="com.demo.springmvc"/> </beans>
HelloWorldController内容:
package com.demo.springmvc; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping; @Controller
public class HelloWorldController { @RequestMapping("/hello")
public String hello(Model model) {
model.addAttribute("greeting", "Hello Spring MVC");
return"helloworld";
} @RequestMapping("/heihei")
public String heihei(){
return "redirect:/html/final.html";
}
}
helloworld.jsp内容(最后是通过添加${pageContext.request.contextPath}解决访问问题,其他的配置网上基本千篇一律):
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/css/c.css">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<div style="margin-bottom: 10px;"><img alt="" src="${pageContext.request.contextPath}/images/43.png"></div>
<h1 class="a">${greeting}</h1>
<form action="/HelloSpringMVC/heihei">
<input type="submit" value="获取页面">
</form>
</body>
</html>
final.html内容:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href="../css/c.css">
<title>Insert title here</title>
</head>
<body class="a">
<h2>A simple HTML page</h2>
<div style="margin-bottom: 10px;"><img alt="" src="../images/43.png"></div>
<form action="/HelloSpringMVC/hello">
<table>
<tr>
<td>
<input type="submit" value="获取HTML页面"/>
</td>
</tr>
</table>
</form>
</body>
</html>
运行效果:
整个项目的搭建与测试参考:
https://blog.csdn.net/wild46cat/article/details/52456715
MAVEN创建WEB项目及部署:
https://www.cnblogs.com/Ming8006/p/6346712.html
https://www.cnblogs.com/hongmoshui/p/7994759.html
SpringMVC对静态资源的访问(js、css、img)的更多相关文章
- idea中JSP页面不能访问静态资源(图片,js,css)
必须配置SpringMvc对访问静态资源的支持,idea默认就是在main/webapp 下的文件路径,要在web-info同级的resource文件下放置,JSP中 ${pageContext.re ...
- (八)springmvc之静态资源的访问。
一.直接调用 行内样式或者js直接调用没有问题. <span style="font-size:26px;color: Blue">行内样式</span> ...
- 用node.js搭建一个静态资源站 html,js,css正确加载 跳转也完美实现!
昨天买了一个服务器想着用来测试一些自己的项目,由于是第一次建站,在tomcat,linux,node.js间想了好久.最终因为node搭建比较方便没那么麻烦就决定用node.js来搭建网站项目. 搭建 ...
- SpringMVC常用配置-添加静态资源处理器-针对SpringMVC中静态资源无法访问的问题
- SpringMVC -- 梗概--源码--贰--静态资源的访问问题
配置:<mvc:default-servlet-handler/> 1>静态资源:除了Servlet.Controller之外的资源,如:js,css,png,html等 2> ...
- SpringMVC拦截静态资源的解决方法
本文中的各软件版本简要信息: IDE:Myeclise17 JDK:1.8.0_111 spring:5.1.8 springMVC:5.1.8 mybatis:3.2.2 Tomcat:9.0 在使 ...
- SPRING-MVC访问静态文件,如jpg,js,css
如何你的DispatcherServlet拦截 *.do这样的URL,就不存在访问不到静态资源的问题.如果你的DispatcherServlet拦截“/”,拦截了所有的请求,同时对*.js,*.jpg ...
- SpringMVC 部署项目静态资源文件访问问题
问题:采用SpringMVC 部署项目后程序加载或用浏览器访问时出现类似的警告,2011-01-19 10:52:51,646 WARN [org.springframework.web.servle ...
- springmvc之静态资源访问不到 -记一次惨痛的经历
springmvc之静态资源访问不到 -记一次惨痛的经历 问题描述:项目正常启动,可以访问页面,但是无法找到静态资源文件,如css,js等文件资源. 控制台: $ 未定义 页面: GET http:/ ...
随机推荐
- Windows下nginx下安装amqp
1.复制php_amqp.dll 到php/ext/ 2.复制rabbitmq.4.dll 到php/3.复制rabbitmq.4.dll 到windows/system32/ (如32位) ,64位 ...
- 折腾newifi3 d2笔记
1.忘记密码,恢复出厂 通电开机,等正常运行后,长按RESET大约6~8秒,见所有灯开始一起慢闪,可松手等重启就是出厂状态了,出厂IP是:192.168.99.1 2.免拆机刷breed 首先要打开s ...
- 在CentOS6.9 x86下编译libusb-1.0.22遇到的两个问题
OS版本:CentOS 6.9 x86,内核版本2.6.32 问题一:configure.ac:36: error: Autoconf version 2.69 or higher is requir ...
- [邀月博客] SQL Server 2008中SQL增强之二:Top新用途
top数为变量时 declare @intTop intset @intTop=2print @intTop --set rowcount @intTop--select * from [dbo].[ ...
- grep -iq 与grep -qi 意思
就是有的时候你不需要直接打印出结果,比如在shell脚本中,你只需要知道grep有没有找到指定的字符串,而不需要满屏幕打印出来,因为那样会很难看.这只可以加-q选项,执行结果是:如果找到了,会返回0, ...
- Zabbix 各种报错信息和遇到的问题处理(持续总结更新~~~~~)
问题1:Zabbix poller processes more than 75% busy 解决: 1.修改配置文件: # vim /etc/zabbix/zabbix_server.conf St ...
- 100-days: twenty-five
Title: Want to be happy? Rent a Finnish person(芬兰人) to teach you how rent n.租金; 地租; (意见等的) 分裂,分歧; (衣 ...
- python note 08 文件操作
1.相对路径与绝对路径比较 #绝对路径 f = open('d:\pzw.txt',mode='r',encoding='UTF-8') content = f.read() print(conten ...
- NET CORE 2.0发布在IIS上提示502.5错误
在装了WindowsHosting和NET CORE SDK后,如果没有重启服务器,访问站点报以上错误,解决办法要么重启,要么执行以下两条命令: net stop was /y net start w ...
- 关于@JsonIgnore的理解
首先:@JsonIgnore是一个能够在后端发送给前端数据的时候对后端发送出的json字符串能够发挥作用的一个注解 如果比如user表,你在password这个属性上添加@JsonIgnore,就会s ...