我正在开发一个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>
答案 0 :(得分:3)
我已通过在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>
并在图片src网址中添加<%=request.getContextPath()%>
。
与<img src="<%=request.getContextPath()%>/images/logo.png" />
答案 1 :(得分:1)
您可以使用JSTL <link href="<c:url value='/css/style.css'/>"
标记来为图像做同样的事情。 <img src="<c:url value='/images/Logo1.png'/>"
。
您还需要添加到页面
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>