从Spring MVC访问CSS [英] Accessing CSS from Spring MVC

查看:91
本文介绍了从Spring MVC访问CSS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从spring mvc访问CSS. 我花了很多时间搜索Google和堆栈溢出,但没有找到为什么无法访问我的CSS的问题.请帮助.... 我在servlet.xml中包含了mvc标签,并将css保留在webapps/resources文件夹中

I need to access CSS from spring mvc. I spend a lot time searching google and stack overflow but didn't find why my css is not being accessed.Please help.... I have included mvc tag in servlet.xml and kept the css in webapps/resources folder

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;
}

Servlet.xml:

[...]

<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>  

JSP文件:

<%@ 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文件位于 WEB-INF 文件夹中,因此如何将以下代码段添加到您的 web.xml中并重新部署您的应用程序b?默认会从springMVC请求过滤器中排除css访问请求.

Since your jsp file are within the WEB-INF folder,how about add the follow snippet to your web.xml and redeploy your applicationb?The default will exclude the css access request from the springMVC request filter.

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

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

Below code is used to add server path to each of your href link

<%
String path = request.getContextPath();
String serverPath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort();
String basePath = serverPath + path+"/";
%>
<base href="<%=basePath%>"/>

这篇关于从Spring MVC访问CSS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆