gpt4 book ai didi

CSS 未反射(reflect)在 Maven 构建 Spring-Hibernate 元素上

转载 作者:行者123 更新时间:2023-11-28 10:27:01 25 4
gpt4 key购买 nike

我在 Eclipse 中使用 Eclipse M2E 插件在 Maven 上构建了一个 Spring-Hibernate 元素。虽然我的功能运行良好,但我需要为应用程序提供一些视觉吸引力,并且我希望为此使用 CSS。但是,我的 CSS 从未反射(reflect)在应用程序上。我已经尝试查看关于 stackoverflow 的几个密切相关的问题,但无法正确解决。

Spring not finding resource files (css, jsp...)

External CSS does not load in web page

这是我尝试过的方法(在 jsp 本身上进行了很多点击和试验):

index.jsp

> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
> "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html
> xmlns="http://www.w3.org/1999/xhtml"> <%@page contentType="text/html"
> %>
>
> <%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <%@page
> session="true"%> <html> <head> <link rel="stylesheet"
> href="${pageContext.request.contextPath}/styles.css" /> <link
> rel="stylesheet" href="styles.css" /> <link rel="stylesheet"
> href="<c:url value="style.css"/>" type="text/css" />

<!-- even tried this with some reference I got somewhere,
while this atleast gave the body the color as mentioned in CSS,
nothing else worked including the form fields-->

<style>
<%@include file="styles.css"%>
</style>
</head>

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"
id="WebApp_ID" version="2.5">

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

<!-- Spring MVC -->
<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>

<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.css</url-pattern>
</servlet-mapping>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring-security.xml
</param-value>
</context-param>

<!-- Spring Security -->
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>

<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

</web-app>

Controller 中的请求映射:

@RequestMapping("/")
public ModelAndView welcomePage(Map<String, Object> map) {

ModelAndView model = new ModelAndView();
model.setViewName("index");

// more code

return model;

}

最佳答案

您需要确保您的 css 文件位于正确的位置。例如,这是我使用的结构:

\webapp\resources\css\mycss.css
\webapp\resources\images
\webapp\WEB-INF\

在我的 jsp 中,我这样引用 css:

<link href="<c:url value="/resources/css/mycss.css"/>" rel="stylesheet">

对于 spring-mvc,你还需要指定资源的位置,在你的 spring-mvc.xml 文件中,添加:

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

关于CSS 未反射(reflect)在 Maven 构建 Spring-Hibernate 元素上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23693406/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com