我正在开发Spring,Hibernate,JSP应用程序。我正在尝试从我的JSP页面显示图像。图像未显示在浏览器中。我也无法将CSS链接到JSP页面。
我的JSP页面是:
<%@ page contentType="text/html;charset=UTF-8"%> <%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%> <html> <head> <link href="css/style.css" rel="stylesheet" type="text/css" /> </head> <body id="page1"> <div class="headerlogo" align="center"><a href=""><img src="images/Logo1.png" alt="" /></a></div> <div align="center"> <h1><spring:message code="header.title"/></h1> Language : <a href="?lang=en_US">English</a>| <a href="?lang=sp_SP">Spanish</a> <h3> <a href="home"><spring:message code="header.gotohome" /> </a> </h3> </div> </body> </html>
我已经设定
<http pattern="/images/**" security="none" /> <http pattern="/css/**" security="none" />
用于我的图像和CSS文件夹
我的Spring Security xml是:
<?xml version="1.0" encoding="UTF-8"?> <beans:beans xmlns="http://www.springframework.org/schema/security" xmlns:beans="http://www.springframework.org/schema/beans" 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.1.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd"> <http pattern="/images/**" security="none" /> <http pattern="/css/**" security="none" /> <http auto-config="true" use-expressions="true"> <intercept-url pattern="/admin*" access="hasRole('SYS_ADMIN')" /> <form-login login-page="/login" default-target-url="/role-check" authentication-failure-url="/login?error=true" /> <logout logout-success-url="/login" /> </http> <authentication-manager> <authentication-provider> <jdbc-user-service data-source-ref="fmsDataSource" users-by-username-query="select USERNAME,PASSWORD, 'true' as enabled from users where USERNAME=?" authorities-by-username-query="select u.USERNAME, ur.AUTHORITY from users u, user_roles ur where u.USER_ID = ur.USER_ID and u.USERNAME =? " /> </authentication-provider> </authentication-manager> </beans:beans>
我已通过在web.xml文件中添加以下映射解决了该问题
web.xml
<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> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.gif</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.jpg</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.png</url-pattern> </servlet-mapping>
并添加<%=request.getContextPath()%>图片src网址。喜欢<img src="<%=request.getContextPath()%>/images/logo.png" />
<%=request.getContextPath()%>
<img src="<%=request.getContextPath()%>/images/logo.png" />