Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从SpringMVC访问CSS_Css_Jsp_Spring Mvc - Fatal编程技术网

从SpringMVC访问CSS

从SpringMVC访问CSS,css,jsp,spring-mvc,Css,Jsp,Spring Mvc,我需要从SpringMVC访问CSS。 我花了很多时间搜索谷歌和堆栈溢出,但没有找到为什么我的css没有被访问。请帮助。。。。 我已将mvc标记包含在servlet.xml中,并将css保存在webapps/resources文件夹中 CSS文件: @CHARSET "ISO-8859-1"; html, body { height: 100%; margin: 0; font-size: 20px; } #header { height: 20%;

我需要从SpringMVC访问CSS。 我花了很多时间搜索谷歌和堆栈溢出,但没有找到为什么我的css没有被访问。请帮助。。。。 我已将mvc标记包含在servlet.xml中,并将css保存在webapps/resources文件夹中

CSS文件:

@CHARSET "ISO-8859-1";

html, body {
    height: 100%;
    margin: 0;
    font-size: 20px;
}

#header {
    height: 20%;
    background-color:#c4e8a2;
    padding: 1rem;
}
#left {
    width: 20%;
    height: 80%;
    position: fixed;
    outline: 1px solid;
    background:#42b0f4;
}
#right {
    width: 80%;
   /* height: auto;*/
    height: 80%;
    outline: 1px solid;
   /* position: absolute; */
    position:fixed;
    right: 0;
    background:#42b0f4;
}



/*#footer
{
    width:100%;
    height:20%;
    position: absolute;
    bottom : 0;
    height : 50 px ;
    left :0;
    background:#42b0f4;

}*/



#footer {
  position: absolute;
  right: 0;
  bottom: 0;
  left: 0;
  padding: 1rem;
  background-color:#c4e8a2;
  text-align: center;
}
[...]

<context:component-scan base-package="com.controller"></context:component-scan>
<context:component-scan base-package="com.dao"></context:component-scan>
<context:component-scan base-package="com.model"></context:component-scan> 

<mvc:resources mapping="/resources/**" location="/resources/css/"
    cache-period="31556926"/>
<mvc:annotation-driven />   

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
<property name="prefix" value="/WEB-INF/jsp/"></property>  
<property name="suffix" value=".jsp"></property>  
</bean>  

<bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>  
<property name="url" value="jdbc:oracle:thin:@localhost:1521:xe"></property>  
<property name="username" value="system"></property>  
<property name="password" value="*****"></property>  
</bean>  

<bean id="jt" class="org.springframework.jdbc.core.JdbcTemplate">  
<property name="dataSource" ref="ds"></property>  
</bean>  


<bean id="dao" class="com.dao.UserDao">  
<property name="template" ref="jt"></property>  
</bean>  


<bean id="loginservice" class="com.service.LoginService">
</bean>  
</beans>  
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>  
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>  
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>   
<!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">
<title>Insert title here</title>
</head>
<body>
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Login page</title>

<link href="/resources/css/test.css" rel="stylesheet" type="text/css">
  </head>

  <body>

 <div id = "header"> 
<jsp:include page="headerlogin.jsp"/>
</div>
        <h1>Login Here</h1>  
       <form:form method="POST" action="/Project/onSubmit">    
        <table >    

         <tr>    
          <td>Username : </td>   
          <td><form:input path="username"  /></td>  
         </tr> 

          <tr>    
          <td>Password :</td>    
          <td><form:input path="password" type="password" name="password" /></td>  
         </tr>   

         <tr>    
          <td> </td>    
          <td><input type="submit" value="Login" /></td>    
         </tr>    
        </table>    
       </form:form>   
 <div id="footer">
 <jsp:include page="Footer.jsp"/>
</div> 
   </body> 
</body>
</html>
Servlet.xml:

@CHARSET "ISO-8859-1";

html, body {
    height: 100%;
    margin: 0;
    font-size: 20px;
}

#header {
    height: 20%;
    background-color:#c4e8a2;
    padding: 1rem;
}
#left {
    width: 20%;
    height: 80%;
    position: fixed;
    outline: 1px solid;
    background:#42b0f4;
}
#right {
    width: 80%;
   /* height: auto;*/
    height: 80%;
    outline: 1px solid;
   /* position: absolute; */
    position:fixed;
    right: 0;
    background:#42b0f4;
}



/*#footer
{
    width:100%;
    height:20%;
    position: absolute;
    bottom : 0;
    height : 50 px ;
    left :0;
    background:#42b0f4;

}*/



#footer {
  position: absolute;
  right: 0;
  bottom: 0;
  left: 0;
  padding: 1rem;
  background-color:#c4e8a2;
  text-align: center;
}
[...]

<context:component-scan base-package="com.controller"></context:component-scan>
<context:component-scan base-package="com.dao"></context:component-scan>
<context:component-scan base-package="com.model"></context:component-scan> 

<mvc:resources mapping="/resources/**" location="/resources/css/"
    cache-period="31556926"/>
<mvc:annotation-driven />   

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
<property name="prefix" value="/WEB-INF/jsp/"></property>  
<property name="suffix" value=".jsp"></property>  
</bean>  

<bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>  
<property name="url" value="jdbc:oracle:thin:@localhost:1521:xe"></property>  
<property name="username" value="system"></property>  
<property name="password" value="*****"></property>  
</bean>  

<bean id="jt" class="org.springframework.jdbc.core.JdbcTemplate">  
<property name="dataSource" ref="ds"></property>  
</bean>  


<bean id="dao" class="com.dao.UserDao">  
<property name="template" ref="jt"></property>  
</bean>  


<bean id="loginservice" class="com.service.LoginService">
</bean>  
</beans>  
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>  
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>  
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>   
<!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">
<title>Insert title here</title>
</head>
<body>
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Login page</title>

<link href="/resources/css/test.css" rel="stylesheet" type="text/css">
  </head>

  <body>

 <div id = "header"> 
<jsp:include page="headerlogin.jsp"/>
</div>
        <h1>Login Here</h1>  
       <form:form method="POST" action="/Project/onSubmit">    
        <table >    

         <tr>    
          <td>Username : </td>   
          <td><form:input path="username"  /></td>  
         </tr> 

          <tr>    
          <td>Password :</td>    
          <td><form:input path="password" type="password" name="password" /></td>  
         </tr>   

         <tr>    
          <td> </td>    
          <td><input type="submit" value="Login" /></td>    
         </tr>    
        </table>    
       </form:form>   
 <div id="footer">
 <jsp:include page="Footer.jsp"/>
</div> 
   </body> 
</body>
</html>
[…]
JSP文件:

@CHARSET "ISO-8859-1";

html, body {
    height: 100%;
    margin: 0;
    font-size: 20px;
}

#header {
    height: 20%;
    background-color:#c4e8a2;
    padding: 1rem;
}
#left {
    width: 20%;
    height: 80%;
    position: fixed;
    outline: 1px solid;
    background:#42b0f4;
}
#right {
    width: 80%;
   /* height: auto;*/
    height: 80%;
    outline: 1px solid;
   /* position: absolute; */
    position:fixed;
    right: 0;
    background:#42b0f4;
}



/*#footer
{
    width:100%;
    height:20%;
    position: absolute;
    bottom : 0;
    height : 50 px ;
    left :0;
    background:#42b0f4;

}*/



#footer {
  position: absolute;
  right: 0;
  bottom: 0;
  left: 0;
  padding: 1rem;
  background-color:#c4e8a2;
  text-align: center;
}
[...]

<context:component-scan base-package="com.controller"></context:component-scan>
<context:component-scan base-package="com.dao"></context:component-scan>
<context:component-scan base-package="com.model"></context:component-scan> 

<mvc:resources mapping="/resources/**" location="/resources/css/"
    cache-period="31556926"/>
<mvc:annotation-driven />   

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
<property name="prefix" value="/WEB-INF/jsp/"></property>  
<property name="suffix" value=".jsp"></property>  
</bean>  

<bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>  
<property name="url" value="jdbc:oracle:thin:@localhost:1521:xe"></property>  
<property name="username" value="system"></property>  
<property name="password" value="*****"></property>  
</bean>  

<bean id="jt" class="org.springframework.jdbc.core.JdbcTemplate">  
<property name="dataSource" ref="ds"></property>  
</bean>  


<bean id="dao" class="com.dao.UserDao">  
<property name="template" ref="jt"></property>  
</bean>  


<bean id="loginservice" class="com.service.LoginService">
</bean>  
</beans>  
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>  
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>  
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>   
<!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">
<title>Insert title here</title>
</head>
<body>
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Login page</title>

<link href="/resources/css/test.css" rel="stylesheet" type="text/css">
  </head>

  <body>

 <div id = "header"> 
<jsp:include page="headerlogin.jsp"/>
</div>
        <h1>Login Here</h1>  
       <form:form method="POST" action="/Project/onSubmit">    
        <table >    

         <tr>    
          <td>Username : </td>   
          <td><form:input path="username"  /></td>  
         </tr> 

          <tr>    
          <td>Password :</td>    
          <td><form:input path="password" type="password" name="password" /></td>  
         </tr>   

         <tr>    
          <td> </td>    
          <td><input type="submit" value="Login" /></td>    
         </tr>    
        </table>    
       </form:form>   
 <div id="footer">
 <jsp:include page="Footer.jsp"/>
</div> 
   </body> 
</body>
</html>

在此处插入标题
登录页面
在这里登录
用户名:
密码:

尝试通过访问您的
.css
。像这样:

<link rel="stylesheet" href="<c:url value="/some/realtiv/pathToYour.css" />">

由于您的jsp文件位于WEB-INF文件夹中,如何将以下代码片段添加到WEB.xml并重新部署您的应用程序b?默认将从springMVC请求过滤器中排除css访问请求


违约
*.css
下面的代码用于将服务器路径添加到每个href链接


.css
文件放入
WEB-INF
中的资源文件夹中,然后将其放入
mvc dispatcher servlet.xml

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


Hi..谢谢您的快速回复。但它不起作用:(如果您将css文件放入/resources/css/文件夹,那么您可以使用servlet.xml中的location=“/resources/”并使用访问它。如果您想使用location=“/resources/css/”,那么您可以使用我已经尝试过的方法访问它,正如您所说的,但它不起作用:(这可能是缓存问题。如果您使用Chrome,请尝试每次按ctrl+F5以清除缓存。@GurkanYesilyurt,尝试了相同的方法,但没有成功:(对不起,我没有理解您的意思。如果我使用*.css而不是/,那么我的jsp页面将不会被访问。@Sarbani default是一个保留的url,您不需要创建一个名为defaultHi lucumt的servlet名称,谢谢您的解释。我尝试了相同的方法,但无效:(@Sarbani如果仍然不起作用,我建议您使用绝对路径来访问静态文件,我已经更新了我的答案,请尝试thisHi Riyaz,谢谢您的回复。我需要包括任何jar来使用它吗?我收到了这个错误-未知标记(spring:url)。是的,您需要在jsp页面中使用它