2015-04-29 216 views
3

我是Spring MVC的新手。我正在创建一个项目,其中包含一些输入表单,然后将其显示在另一个页面上。我有一个控制器类,它有两个请求映射方法。一个进入输入页面,另一个进入显示页面。该应用程序工作正常,但我无法加载静态资源,如CSS。我的css位于WebContent下的一个名为css的文件夹中。下面是在eclipse无法在spring mvc应用程序中加载静态资源

http://i.stack.imgur.com/74bM2.png

我的项目结构我部署到JBoss应用程序。我的上下文根设置为productCat

下面是我的web.xml

<?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_2_5.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"> 
    <servlet> 
     <servlet-name>springmvc</servlet-name> 
     <servlet-class> 
     org.springframework.web.servlet.DispatcherServlet 
     </servlet-class> 
     <init-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/config/springmvc-config.xml</param- value> 
     </init-param> 
     <load-on-startup>1</load-on-startup>  
     </servlet> 

<servlet-mapping> 
    <servlet-name>springmvc</servlet-name> 
    <url-pattern>/</url-pattern> 
</servlet-mapping> 

而且用SpringMVC我-config.xml中

<?xml version="1.0" encoding="UTF-8"?> 
    <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" 
    http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd 
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc.xsd  
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context.xsd"> 
<context:component-scan base-package="com.kaushik.controller" /> 
<mvc:annotation-driven /> 
<mvc:resources location="/css/**" mapping="/css/"/> 
<bean id="viewResolver" 
    class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
    <property name="prefix" value="/WEB-INF/jsp/"></property> 
    <property name="suffix" value=".jsp"></property> 
</bean> 

下面是我的控制器

package com.kaushik.controller; 

    import org.apache.commons.logging.Log; 
    import org.apache.commons.logging.LogFactory; 
    import org.springframework.stereotype.Controller; 
    import org.springframework.ui.Model; 
    import org.springframework.web.bind.annotation.RequestMapping; 

    import com.kaushik.domain.Product; 
    import com.kaushik.form.ProductForm; 

    @Controller 
    public class ProductController 
    { 
     private static final Log logger = LogFactory.getLog(ProductController.class); 

    @RequestMapping(value="/product_input.action") 
    public String inputProduct() 
    { 
     logger.info("inputProduct called"); 
     return "ProductForm"; 
    } 

    @RequestMapping(value="/product_save.action") 
    public String saveProduct(ProductForm productForm, Model model) 
    { 
     logger.info("SaveProductController called"); 
     // create model 
     Product product = new Product(); 
     product.setName(productForm.getName()); 
     product.setDescription(productForm.getDescription()); 
     try 
     { 
     product.setPrice(Float.parseFloat(productForm.getPrice())); 
     } 
     catch (NumberFormatException num) 
     { 

     } 
     // TODO code to save product 
     // store model in a variable for the view 
     model.addAttribute("product",product); 
     return "/ProductDetails"; 
    } 

    } 

这里是我的ProductForm.jsp

<!DOCTYPE HTML> 
    <html> 
    <head> 
    <title>Add Product Form</title> 
    <link href="css/main.css" rel="stylesheet" > 

    </head> 
    <body> 

      <div id="global"> 
       <form action="product_save.action" method="post"> 
       <fieldset> 
        <legend>Add a product</legend> 
        <p> 
        <label for="name">Product Name: </label> 
        <input type="text" id="name" name="name" 
       tabindex="1"> 
        </p> 
        <p> 
        <label for="description">Description: </label> 
        <input type="text" id="description" 
       name="description" tabindex="2"> 
        </p> 
        <p> 
        <label for="price">Price: </label> 
        <input type="text" id="price" name="price" 
       tabindex="3"> 
        </p> 
        <p id="buttons"> 
        <input id="reset" type="reset" tabindex="4"> 
        <input id="submit" type="submit" tabindex="5" 
       value="Add Product"> 
        </p> 
       </fieldset> 
       </form> 
      </div> 
    <body> 
    </html> 

正如你可以看到我在我的servlet XML使用,但它仍然是不加载CSS。在浏览器中的错误是

无法加载资源:服务器与404(未找到)状态回应

,它试图访问的URL是http://localhost:8080/productCat/css/main.css

回答

2

您正在使用Spring的资源映射

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

,你有/css/**映射/css/。我认为这是错误&应该是这样的

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

欲了解更多信息,请查阅Spring MVC – How to include JS or CSS files in a JSP page

+1

不错的捕获+1,错过了它,只注意到相对路径 –

+0

@ OO7对不起,但我没有注意到它们之间的区别。 –

+0

你已经用'/ css /'创建了* mapping *,但理想情况下它应该用'/ css/**'。这意味着只要你请求模式'/ css/**'的URL,Spring就会用在*位置指定的路径* ie,'/ css /'。仔细阅读答案中给出的内容,我认为不需要写'/ css/main.css'而不是'/ main.css'。 – OO7

1

你的结构和映射看起来很好 ,纠正这种情况是正确的。 你的另外一个问题是,您已经通过相关链接访问你的CSS,添加页面指令到JSP

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 

的顶部,并链接你的css

<link href="<c:url value="/css/main.css"/>" rel="stylesheet" > 
+0

替换<链接HREF = “CSS/main.css的” 相对= “样式”>与<链接的rel = $ {pageContext.request.contextPath} /css/main.css> – Sunil

+0

'c:url'标记将正确处理上下文,但'$ {pageContext.request.contextPath}'也是一个可行的选择 –

+0

Upvote for JSTL。 – OO7