繁体   English   中英

在春季网络项目中使用CSS和JS

[英]Using CSS and JS in spring web-project

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

我的项目结构

<%@ 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-调度-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和创建Web应用程序。

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM