我正在尝试创建一个Spring MVC应用程序。我的第一页正在显示,但css& js没有得到应用。
以下是我的档案:
Web.xml中
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>SpringMVC</display-name>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
调度-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:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<context:component-scan base-package="com.spring.web.controller" />
<mvc:resources mapping="css/**" location="/css/" />
<mvc:resources mapping="js/**" location="/views/js/" />
<mvc:resources mapping="images/**" location="/views/images/" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
我的LoginController.java
@Controller
@RequestMapping(value="/")
public class LoginController {
private final Logger logger = Logger.getLogger(LoginController.class);
@RequestMapping(method=RequestMethod.GET)
public ModelAndView getModelAndView()
{
ModelAndView model=new ModelAndView("Login", "WelcomeMessage", "WELCOME TO SPRING MVC");
return model;
}
}
我的Css&amp; Js和其他文件如下所示
我的Login.jsp 是
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<link rel="stylesheet" href="css/login.css" />
<script type="text/javascript" src="js/login_sign_script.js"></script>
<title>Welcome</title>
请帮忙。在此先感谢
答案 0 :(得分:1)
因为您已将资源文件放入 views 文件夹,而视图是WEB-INF
文件夹的子文件夹。
WEB-INF
资源可供Web应用程序的资源加载程序访问,而不会直接公开显示。
您可以从 What is WEB-INF used for in a Java EE web application?
中找到有关if的详细信息所以有两种方法可以做到:
1.如果您仍想将其放入WEB-INF/views
文件夹,则需要更改mvc资源映射,如下所示:
<mvc:resources mapping="/css/**" location="/WEB-INF/views/css/" />
<mvc:resources mapping="/js/**" location="/WEB-INF/views/js/" />
2.从WEB-INF/views
删除css和js文件夹,并将它们直接放在webapps
文件夹中,如下所示:
然后我们可以更新资源映射,如下所示:
<mvc:resources mapping="/css/**" location="/css/" />
<mvc:resources mapping="/js/**" location="/js/" />
或者我们可以删除mvc资源映射并在web.xml
中添加默认的servlet映射,如下所示,它也可以正常工作:
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.css</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.js</url-pattern>
</servlet-mapping>
编辑于2018-04-11
源代码位于 Google Drive