SpringMVC对静态资源的访问(js、css、img)

在网上找了很多的内容,都没法解决,最后通过https://blog.csdn.net/wild46cat/article/details/52456715中内容解决的,在此记录一下。

 

项目结构:

SpringMVC对静态资源的访问(js、css、img)_第1张图片

pom.xml内容:


  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  4.0.0
  com.demo
  HelloSpringMVC
  war
  0.0.1-SNAPSHOT
  HelloSpringMVC Maven Webapp
  http://maven.apache.org
  
  
    
      junit
      junit
      3.8.1
      test
    
    
    
    
    
        javax.servlet
        javax.servlet-api
        3.1.0
        provided
    
    
    
    
    
        org.springframework
        spring-core
        4.1.4.RELEASE
    
    
    
    
        org.springframework
        spring-web
        4.1.4.RELEASE
    
    
    
    
        org.springframework
        spring-webmvc
        4.1.4.RELEASE
    
    
  
  
  
    HelloSpringMVC
    
        
            org.apache.tomcat.maven
            tomcat7-maven-plugin
            2.2
            
                http://localhost:8080/manager/text
                admin
                Pass@1
                /${project.artifactId}
            
        
    
  
View Code

web.xml内容:


    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">
    HelloWorldSpring

    
        spring-mvc
        class>
            org.springframework.web.servlet.DispatcherServlet
        class>
        1
    

    
        spring-mvc
        /
    

    
    
    
        contextConfigLocation
        /WEB-INF/root-context.xml
    

    
    
        class>org.springframework.web.context.ContextLoaderListenerclass>
    
View Code

root-context.xml内容:



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

spring-mvc-servlet.xml内容:



    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">
     
    
    
       
    
    
    
       class="org.springframework.web.servlet.view.InternalResourceViewResolver">
               
       
           
       
       package="com.demo.springmvc"/>

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"%>





Insert title here


    

class="a">${greeting}

View Code

final.html内容:






Insert title here

class="a">

A simple HTML page

View Code

运行效果:

SpringMVC对静态资源的访问(js、css、img)_第2张图片

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

    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))