如何使用“css”在“jsp”在Spring MVC项目? [英] How to use "css" in the "jsp" in Spring MVC project?

查看:121
本文介绍了如何使用“css”在“jsp”在Spring MVC项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个弹簧MVC项目,其中我试图使用 css 样式表,通过引用它来设计我的JSP页面。但是不知何故我的css文件没有被拾起,一旦我在控制器中的我的方法。



下面是我的JSP文件 -

 < html> 
< head>
< link rel =stylesheettype =text / csshref =test.css/>
< / head>

< body>

< h1>所有标题1元素将为红色< / h1>
< h2>所有标题2元素将为蓝色< / h2>
< p>段落中的所有文本都是绿色的。< / p>

< / body>
< / html>

下面是我的 test.css -

  h1 {color:red;} 
h2 {color:blue;}
p {color:绿色;}

下面是我在控制器类中的方法 -

  @RequestMapping(value =testing,method = RequestMethod.GET)
public Map< String,String> testing(){

final Map< String,String> model = new LinkedHashMap< String,String>();

return model;
}

目录结构如下 -

 webapp / 
| - resources /
| + - css /
| test.css
+ - WEB-INF /
+ - views /
test.jsp

不是为我工作..有什么问题,我在这里做吗?



这是我的web.xml文件 -



UPDATE: p>

 <?xml version =1.0encoding =UTF-8?& 
< web-app xmlns:xsi =http://www.w3.org/2001/XMLSchema-instancexmlns =http://java.sun.com/xml/ns/javaeexmlns :web =http://java.sun.com/xml/ns/javaee/web-app_3_0.xsdxsi:schemaLocation =http://java.sun.com/xml/ns/javaee http:// java.sun.com/xml/ns/javaee/web-app_3_0.xsdid =WebApp_IDversion =3.0>
< display-name> testweb< / display-name>
< listener>
< listener-class> com.host.webres.resource.env.EbayResourceRuntimeListener< / listener-class>
< / listener>
< welcome-file-list>
< welcome-file> index.html< / welcome-file>
< welcome-file> index.htm< / welcome-file>
< welcome-file> index.jsp< / welcome-file>
< welcome-file> default.html< / welcome-file>
< welcome-file> default.htm< / welcome-file>
< welcome-file> default.jsp< / welcome-file>
< welcome-file> index< / welcome-file>
< / welcome-file-list>
< servlet>
< servlet-name> appServlet< / servlet-name>
< servlet-class> org.springframework.web.servlet.DispatcherServlet< / servlet-class>
< init-param>
< param-name> contextConfigLocation< / param-name>
< param-value> /WEB-INF/spring/context.xml< / param-value>
< / init-param>
< load-on-startup> 1< / load-on-startup>
< / servlet>
< servlet-mapping>
< servlet-name> appServlet< / servlet-name>
< url-pattern> / testweb / *< / url-pattern>
< / servlet-mapping>
< / web-app>

下面是我的 context.xml 文件 -

 <?xml version =1.0encoding =UTF-8?& 
< beans:beans xmlns =http://www.springframework.org/schema/mvc
xmlns:mvc =http://www.springframework.org/schema/mvc
xmlns:xsi =http://www.w3.org/2001/XMLSchema-instance
xmlns:beans =http://www.springframework.org/schema/beans
xmlns:context =http://www.springframework.org/schema/context
xmlns:aop =http://www.springframework.org/schema/aop
xmlns:task =http://www.springframework.org/schema/task
xsi:schemaLocation =
http://www.springframework.org/schema/mvc http://www.springframework。 org / schema / mvc / spring-mvc-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1。 xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework .org / schema / aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/task http:// www。 springframework.org/schema/task/spring-task-3.1.xsd\">

<! - DispatcherServlet上下文:定义这个servlet的请求处理基础设施 - >

<! - 允许proxys - >
< aop:aspectj-autoproxy />

<! - 启用Spring MVC @Controller编程模型 - >
< mvc:annotation-driven>
< mvc:message-converters>
<! - 支持使用渐进式渲染的AJAX处理。使用RaptorResponseWriter替换HttpOutputMessage - >
< beans:bean class =com.host.terr.kernel.filter.RaptorJacksonHttpMessageConverter/>
< / mvc:message-converters>
< / mvc:annotation-driven>

< context:component-scan base-package =com.host.terr.config/>
< context:component-scan base-package =com.host.personalization.bullseye.zookeeper.p13nzook.controller/>

<! - 通过有效地提供相应目录中的静态资源
来处理HTTP GET请求 - >
< resources mapping =/ js / **location =/ js //>
< resources mapping =/ css / **location =/ css //>

<! - 解析被@Controllers渲染的视图到/ WEB-INF / views目录中的.jsp资源 - >
< bean:bean class =org.springframework.web.servlet.view.InternalResourceViewResolver>
< bean:property name =prefixvalue =/ WEB-INF / views //>
< beans:property name =suffixvalue =。jsp/>
< / bean:bean>
< / beans:beans>

这是异常我在我的控制台,但浏览器显示数据没有任何css -

  404 /testweb/test.css未找到/testweb/test.css未找到},相关:{}} 


解决方案

此css资源问题



请尝试下面

 < link rel =stylesheettype =text / csshref =css / test.css/> 

,如果由bowaser加载,您可以看到此资源。


I am working on a spring MVC project in which I am trying to use css style sheets by referencing it to design my JSP page. But somehow my css files are not being picked up once I hit my method in the controller..

Below is my JSP file -

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

<body>

<h1>All header 1 elements will be red</h1>
<h2>All header 2 elements will be blue</h2>
<p>All text in paragraphs will be green.</p>

</body>
</html>

And below is my test.css file -

h1 {color:red;}
h2 {color:blue;}
p {color:green;}    

And below is my method in controller class -

@RequestMapping(value = "testing", method = RequestMethod.GET)
public Map<String, String> testing() {

    final Map<String, String> model = new LinkedHashMap<String, String>();

    return model;
}

Directory structure is like this -

webapp/
|-- resources/
|   +-- css/
|           test.css
+- WEB-INF/
  +-- views/
        testing.jsp

But somehow it's not working for me.. Is there anything wrong I am doing here?

UPDATE:-

Here is my web.xml file-

<?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_3_0.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>testweb</display-name>
  <listener>
    <listener-class>com.host.webres.resource.env.EbayResourceRuntimeListener</listener-class>
  </listener>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
    <welcome-file>index</welcome-file>
  </welcome-file-list>
  <servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/spring/context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>/testweb/*</url-pattern>
  </servlet-mapping>
</web-app>

And below is my context.xml file -

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:task="http://www.springframework.org/schema/task"
    xsi:schemaLocation="
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd">

    <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

    <!--  Allow proxys -->
    <aop:aspectj-autoproxy />

    <!-- Enables the Spring MVC @Controller programming model -->
    <mvc:annotation-driven>
            <mvc:message-converters>
              <!--  Support AJAX processing with progressive rendering. Overrides HttpOutputMessage with RaptorResponseWriter -->
            <beans:bean class="com.host.terr.kernel.filter.RaptorJacksonHttpMessageConverter"/>                 
            </mvc:message-converters>
    </mvc:annotation-driven>

    <context:component-scan base-package="com.host.terr.config" />
    <context:component-scan base-package="com.host.personalization.bullseye.zookeeper.p13nzook.controller" />       

    <!-- Handles HTTP GET requests by efficiently serving up static resources 
         in the corresponding directory -->
    <resources mapping="/js/**" location="/js/" />
    <resources mapping="/css/**" location="/css/" />

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>
</beans:beans>

This is exception I am getting on my console but browser shows the data without any css -

404 /testweb/test.css Not Found /testweb/test.css Not Found }, Correlations : {} }

解决方案

this css resource issue

please try below

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

and you can see this resource if be load by bowaser.

这篇关于如何使用“css”在“jsp”在Spring MVC项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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