zoukankan      html  css  js  c++  java
  • 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>
    View Code

    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>
    View Code

    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>
    View Code

    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>
    View Code

    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";
        }
    }
    View Code

    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>
    View Code

    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>
    View Code

    运行效果:

     整个项目的搭建与测试参考: 

        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

        

  • 相关阅读:
    babel的使用以及安装配置
    常见的浏览器兼容性问题与解决方案——CSS篇
    ES6入门——变量的解构赋值
    ES6入门——let和const命令
    第一个移动端项目
    向Github提交更改的代码
    MySQL安装配置
    HTTP 状态消息
    HTMl基础
    Shell 常用的命令
  • 原文地址:https://www.cnblogs.com/lihuajie/p/9186309.html
Copyright © 2011-2022 走看看