每当我向RequestMapping路径添加任何内容时,我遇到解决CSS文件路径的问题。在下面的示例中,如果我更改
@RequestMapping(value =“/ articles / view”,method = RequestMethod.GET)
到
@RequestMapping(value =“/ articles”,method = RequestMethod.GET)
它工作正常。如果我尝试使用路径变量,我也会在映射我的css文件时遇到问题。我的css文件位于名为/ data / css的文件夹中,其中/ data与我的/ WEB-INF处于同一级别。
ArticlesAction.java:
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
@Controller
public class ArticlesAction {
@RequestMapping(value="/articles/view", method = RequestMethod.GET)
public String printWelcome(ModelMap model) {
//model.addAttribute("id", id);
return "articles";
}
}
的web.xml
<?xml version="1.0" encoding="utf-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
MVC-调度-servlet.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:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.test.actions" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
articles.jsp
<!DOCTYPE html>
<html class="no-js" lang="en-US">
<!-- Head section -->
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1" />
<meta name="description" content="Responsive website template by kubasto" />
<meta name="msapplication-TileImage" content="data/img/tile/w.png" />
<meta name="msapplication-TileColor" content="#089bc3" />
<link rel="shortcut icon" href="data/img/favicon/w.png" />
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Rokkitt:400,700" />
<link rel="stylesheet" href="data/css/fancybox.css" />
<link rel="stylesheet" href="data/css/flexslider.css" />
<link rel="stylesheet" href="data/css/mejs.css" />
<link rel="stylesheet" href="data/css/tipsy.css" />
<link rel="stylesheet" href="data/css/base.css" />
<link rel="stylesheet" href="data/css/structure.css" />
<link rel="stylesheet" href="data/css/parts.css" />
<link rel="stylesheet" href="data/css/widgets.css" />
<link rel="stylesheet" href="data/css/bright.css" />
<link rel="stylesheet" href="data/css/color.css" />
答案 0 :(得分:0)
尝试使用特殊的mvc:resources标记:
<mvc:resources location="/, classpath:/META-INF/web-resources/" mapping="/resources/**"/>
要使用它,请将“mvc”命名空间添加到mvc-dispatcher-servlet.xml