我是Spring MVC的新手。我正在创建一个项目,其中包含一些输入表单,其中包含一些详细信息,然后在另一页上显示。我有一个控制器类,它有两个请求映射方法。一个进入输入页面,另一个进入显示页面。应用程序工作正常,但我无法加载像CSS这样的静态资源。我的css位于名为css的文件夹中的WebContent下。下面是我在eclipse中的项目结构
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(未找到)
答案 0 :(得分:2)
您正在使用Spring的资源映射
/css/
您使用/css/**
映射<mvc:resources mapping="/css/**" location="/css/" />
。我认为这是错误的应该是这样的
{{1}}
有关详细信息,请查看Spring MVC – How to include JS or CSS files in a JSP page
答案 1 :(得分:1)
您的结构和映射显示正常,纠正此 007 是正确的。
您的其他问题是您通过相关链接访问您的CSS,将页面指令添加到您的jsp顶部
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
并链接您的css
<link href="<c:url value="/css/main.css"/>" rel="stylesheet" >