java - 在 Spring Web 项目中使用 CSS 和 JS

标签 java spring jsp spring-mvc

我知道 stackoverflow 上有一些关于 Spring Web 应用程序中静态内容的主题,但是在阅读了 5 个主题并进行了大量尝试之后,我仍然看不到页面的内容。 我认为我的 jsp 文件有正确的路径。我的项目结构如下所示:

My project structure

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ page contentType="text/html; charset=ISO-8859-1"%>

<!DOCTYPE HTML>
<html>
<head>

<link rel="stylesheet" type="text/css" href="<c:url value = '/css/stylesheet.css' />" />
<script type="text/javascript" src="<c:url value = '/js/restful.js' />"></script>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Snore-O-Meter</title>
</head>
<body>
<a href="javascript:HelloWorld()">Hello</a>
</body>
</html>

但我认为这是 web.xml 或 mvc-dispatcher-servlet.xml 的默认值:

web.xml

<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<display-name>Spring MVC Application</display-name>

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

mvc-dispatcher-servlet.xml

<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-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

<context:component-scan base-package="com.springapp.mvc"/>

<mvc:annotation-driven />

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

</beans>

谢谢你的帮助,我知道这是我对这个主题的无知,但是我有两本关于 Spring 的书,我找不到任何关于这个主题的有用信息,我真的需要真正的应用程序来学习 Spring 和创作网络应用程序。

最佳答案

将所有资源文件夹(css、js、字体、图像等)放在公共(public) resources 文件夹下。然后添加资源处理程序

<mvc:resources mapping="/resources/**" location="/resources/" />

然后您可以将映射更改为,例如

<script type="text/javascript" src="<c:url value = '/resources/js/restful.js' />"></script>

关于java - 在 Spring Web 项目中使用 CSS 和 JS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18581451/

相关文章:

java - “SwingUtilities.updateComponentTreeUI(this)”从 JComboBox 中删除自定义文档

java - 想在 jhipster 中添加 New Bean

java - 使用 Spring-Boot 创建一个简单的 JAR 而不是可执行的 JAR

java - Struts异常

java - gwt:如何使用setEventListener?

java - 带有进度条的延迟加载图像会在显示图像时损坏图像

java - GAE/J 低级数据存储 API 和 DataNucleus JDO 之间拥有的一对多关系的差异

java - 如何在 Spring 集成中使用 Java DSL 创建 ws 入站网关?

java - JSF 应用程序中的自动注销

java - JSP、Java、Spring、迭代 HashMap 并从其值调用基本 getter 方法?