11、静态资源(springmvc)

学完本次课程后,你能够:
本课目Copyright©文都智链
1、项目依赖
2、静态资源存储
3、SpringMvc配置
4、控制器
5、视图

1.项目依赖

<dependencies>
<!--日志-->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-api</artifactId>
      <version>2.13.3</version>
    </dependency>

    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-core</artifactId>
      <version>2.13.3</version>
    </dependency>
<!--这个可以将springmvc的需要的包都整合起来 -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.3.4</version>
    </dependency>
 <!--servlet依赖-->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>4.0.1</version>
    </dependency>

  </dependencies>

依赖图:

   2、静态资源存储

 要在 webapp 底下建立静态资源包: 里面存放对应的静态资源:

       css:

body{
	/*黄色背景*/
	background-color: yellow;
	
}


#css{
	
	height:100px;
}

#hellocss{
	
	color: gold;
	
}

js:

function helloworld(){
	
	
	var element = document.getElementById("hellojs");
	
	element.innerHTML = "Hello JavaScript!";
	
	//window.document.writeln("Hello World!")
	
	//window.alert("Hello World!!!");
	
	window.console.log("Hello console!")
	
	
	
}

 3、SpringMvc配置 :

在springmvc.xml文件添加:

 <!-- 静态资源访问,约定:静态资源都放在web应用根的res文件夹中 -->
        <mvc:resources mapping="/static/**" location="/static/"/>
        <!--或者-->
        <mvc:resources mapping="/static/img/**" location="/static/img/"/>
        <mvc:resources mapping="/static/css/**" location="/static/css/"/>
        <mvc:resources mapping="/static/js/**" location="/static/js/"/>

 整体的springmvc.xml:

<?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:mvc="http://www.springframework.org/schema/mvc"
           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

            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd

            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc.xsd"
    >


        <context:component-scan base-package="com.wdzl"/>
        <!--
        如果在web.xml中servlet-mapping的url-pattern设置的是/,
        而不是如.do。表示将所有的文件,包含静态资源文件都交给spring mvc处理。
        就需要用到<mvc:annotation-driven />了。如果不加,
        DispatcherServlet则无法区分请求是资源文件还是mvc的注解,而导致controller的请求报404错误。
        -->

    <!-- 注解驱动 -->
        <mvc:annotation-driven />



        <bean id="internalResourceViewResolver"
              class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/jsp/" />
            <property name="suffix" value=".jsp" />
        </bean>

        <!--
        首先由于web.xml配置了DispatcherServlet,其匹配路径都是“/”,
        既可以理解成所有的路径都要经过DispatcherServlet,
        那么对于那些css , js ,图片等静态资源怎么办呢?
        这个时候<mvc:resources mapping="/static/**" location="/static/"/>
        这句话开始起作用,起含义是对于/static/**这种请求,不会被DispatcherServlet拦截
        ,浏览器可以直接访问,当做静态资源交给Servlet处理。
        -->


    <!-- 静态资源访问,约定:静态资源都放在web应用根的res文件夹中 -->
        <mvc:resources mapping="/static/**" location="/static/"/>
        <!--或者-->
        <mvc:resources mapping="/static/img/**" location="/static/img/"/>
        <mvc:resources mapping="/static/css/**" location="/static/css/"/>
        <mvc:resources mapping="/static/js/**" location="/static/js/"/>


    </beans>

 

4、控制器 :

  通过控制器进入:static-resource.jsp---->再由这个页面  访问静态资源

static-resource.jsp:的内容

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css"
	  href="${pageContext.request.contextPath }/static/css/helloworld.css" />


<script type="text/javascript"
		src="${pageContext.request.contextPath }/static/js/helloworld.js">
			
</script>

<title>静态资源访问</title>
</head>
<body onload="helloworld()">

<div id="jsp">
	<h2>Jsp表达式测试:</h2>
	<%= request.getContextPath() %>		<br/>

</div>

<div id="el">
	<h2>EL表达式测试:</h2>
	${pageContext.request.contextPath }		<br/>
</div>

<div id="css">
	<h2>CSS测试:</h2>
	<p id="hellocss">Hello CSS!</p>
</div>

<div id="js">
	<h2>JS测试:</h2>
	<p id="hellojs"></p>
</div>

<div id="img">
	<h2>IMG测试:</h2>
	<img alt="Hello World"
		 src="${pageContext.request.contextPath }/static/img/helloworld.jpg" />

</div>

</body>
</html>

 5、运行测试:

最直接访问控制器里面的 static-resource.jsp 就行

 

看看web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
  <display-name>Archetype Created Web Application</display-name>

 <!---->
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>

  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>

  <!--监听器-->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>


<!--spring-servlet这个名字是因为上面web.xml中<servlet-name>标签配的值为spring(<servlet-name>spring</servlet-name>),再加上“-servlet”后缀而形成的spring-servlet.xml文件名,如果改为springMVC,对应的文件名则为springMVC-servlet.xml。-->
  
<servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:springmvc-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

  <context-param>
    <param-name>log4jConfiguration</param-name>
    <param-value>classpath:log4j2.xml</param-value>
  </context-param>

</web-app>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值