gpt4 book ai didi

java - 无法在spring mvc应用程序中加载静态资源

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:43:57 25 4
gpt4 key购买 nike

我是 Spring MVC 的新手。我正在创建一个项目,该项目有一个输入表单,其中包含一些详细信息,然后将它们显示在另一个页面上。我有一个 Controller 类,它有两个请求映射方法。一个转到输入页面,另一个转到显示页面。该应用程序运行良好,但我无法加载 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>

下面是我的 Controller 类

    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。浏览器中的错误是

Failed to load resource: the server responded with a status of 404 (Not Found)

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

最佳答案

你正在使用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

关于java - 无法在spring mvc应用程序中加载静态资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29939031/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com