【问题标题】:How to use static resources in spring mvc?如何在spring mvc中使用静态资源?
【发布时间】:2013-08-01 11:48:14
【问题描述】:

spring mvc中如何使用静态资源?我只想在jsp中插入一些图像。我必须在哪个地方找到我的资源?上次我曾经在 servlet-context 中添加 mvc:recources,但它显示了一些映射错误。

我还是有麻烦。我提出了另一个问题。拜托,哈普。 https://stackoverflow.com/questions/18020129/troubles-with-static-resources-and-mvcresources

我的配置:

<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:lang="http://www.springframework.org/schema/lang" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
        http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">



    <context:annotation-config />
    <context:component-scan base-package="net.babobka.blog" />


    <import resource="../../db/db-config.xml" />



    <bean id="tilesConfigurer"
        class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
        <property name="definitions">
            <list>
                <value>/WEB-INF/tiles.xml</value>
            </list>
        </property>
    </bean>

    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.tiles2.TilesView" />
    </bean>

</beans>

我的jsp:

<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%>
<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<strong><h1>
        <a href="${pageContext.request.contextPath}">Blog</a>
    </h1></strong>
<p>
    <small>With Spring MVC</small><img src='<c:url value="/static/img/logo.gif"/>' />

以及控制器的一部分:

import org.springframework.stereotype.Controller;
import net.babobka.blog.domain.Content;
import net.babobka.blog.service.ContentService;
import net.babobka.blog.domain.News;
import net.babobka.blog.service.NewsService;
import net.babobka.blog.domain.Tags;
import net.babobka.blog.service.TagsService;
import java.util.Map;
import net.babobka.blog.form.SearchForm;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class HomeController {

    @Autowired
    private ContentService contentService;
    @Autowired
    private NewsService newsService;
    @Autowired
    private TagsService tagsService;

    long HM = 3;

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String listContacts(Map<String, Object> map,@ModelAttribute("search") SearchForm query, BindingResult result) {
        long nums = newsService.getNumRows();
        map.put("old", HM);
        map.put("news", new News());
        map.put("nums", nums);
        map.put("newsList", newsService.getSomeNews(0, HM));

        return "content";
    }

    @RequestMapping(value = "/about", method = RequestMethod.GET)
    public String getAboutPage(Map<String, Object> map,@ModelAttribute("search") SearchForm query, BindingResult result) {

        map.put("news", new News());
        map.put("newsList", newsService.getAboutPage());

        return "about";
    }

Web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <!-- The definition of the Root Spring Container shared by all Servlets 
        and Filters -->
    <context-param>
        <param-name>contextConfigLocation</param-name>

        <param-value>
        /WEB-INF/spring/root-context.xml
        /WEB-INF/spring/application-security.xml
        </param-value>
    </context-param>



    <!-- Creates the Spring Container shared by all Servlets and Filters -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>



    <!-- Processes application requests -->
    <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/appServlet/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>




</web-app>

【问题讨论】:

  • 这里已经有答案了。检查一下。 stackoverflow.com/questions/14751597/…
  • 好的。但是在 servlet-context 中添加 &lt;mvc:resources mapping="/static/**" location="/static/" /&gt; &lt;mvc:default-servlet-handler /&gt; 后,我是 404 的钢铁
  • 在名称为“appServlet”的 DispatcherServlet 中找不到具有 URI [/blog/] 的 HTTP 请求的映射。正如我所说。

标签: java xml spring staticresource


【解决方案1】:

如果您收到 404 错误意味着请求的资源不适用,则意味着您有映射错误,在此映射之后还需要检查 controller-servlet.xml 映射。如果您收到错误,请提供您的代码。

试试这个

<servlet>
<servlet-name>app</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
 </servlet>

<servlet-mapping>
<servlet-name>app</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>

Controller-servlet.xml
<bean id="urlMapping" class="org.springframework.Handler.SimpleHandlerMapping">
<property name="mapping">
<props><prop key="app.do">hello</prop></props></property></bean>

【讨论】:

  • IOException 从 ServletContext 资源 [/WEB-INF/app-servlet.xml] 解析 XML 文档;嵌套异常是 java.io.FileNotFoundException: 无法打开 ServletContext 资源 [/WEB-INF/app-servlet.xml]
  • 正确检查您是否在 webcontent 下提供了视图文件,而不是在 web-inf 文件夹中。正确检查。我还在同一答案中提供了另一个映射文件。
  • 如果您仍然收到 404 错误意味着您在 web-inf 文件夹下提供了您的资源文件,这是不正确的方式。
  • @Rohit Jai 请检查这里有问题。
  • 我的图片在 webapp/static/img/logo.gif 中
【解决方案2】:

Envious 是对的,该链接包含一个您可以看到的示例。顺便说一句,你可以从下面的sn-p代码修改你的代码

    <?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: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-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">


    <context:annotation-config />
    <mvc:annotation-driven />

    <bean
        class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />

    <context:component-scan base-package="com.ashish.stack.ques.controller"
        annotation-config="true"
        scope-resolver="org.springframework.context.annotation.AnnotationScopeMetadataResolver">
        <context:include-filter type="annotation"
            expression="org.springframework.stereotype.Controller" />
    </context:component-scan>



    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass">
            <value>
                org.springframework.web.servlet.view.tiles2.TilesView
            </value>
        </property>
    </bean>
    <bean id="tilesConfigurer"
        class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
        <property name="definitions">
            <list>
                <value>/WEB-INF/tiles.xml</value>
            </list>
        </property>
    </bean>

</beans>

tiles.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE tiles-definitions PUBLIC
       "-//Apache Software Foundation//DTD Tiles Configuration 3.0//EN"
       "http://tiles.apache.org/dtds/tiles-config_3_0.dtd">
<tiles-definitions>
    <definition name="base.definition"
        template="/WEB-INF/jsp/layout.jsp">
        <put-attribute name="title" value="" />
        <put-attribute name="header" value="/WEB-INF/jsp/header.jsp" />
        <put-attribute name="menu" value="/WEB-INF/jsp/menu.jsp" />
        <put-attribute name="body" value="" />
        <put-attribute name="footer" value="/WEB-INF/jsp/footer.jsp" />
    </definition>

    <definition name="show" extends="base.definition">
        <put-attribute name="title" value="Show Image" />
        <put-attribute name="body" value="/WEB-INF/jsp/show.jsp" />
    </definition>

</tiles-definitions>

show.jsp

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ 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>
  Image:
  <img src='<c:url value="/static/image/radar-icon.png"/>' />
</body>
</html>

控制器

    package com.ashish.stack.ques.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class IndexController {

    @RequestMapping(value="/show.do",method=RequestMethod.GET)
    public void index(){
        System.out.println("Got an image");
    }
}

我已经修改了我的代码,请按照代码告诉我,你有没有得到你的解决方案:)

【讨论】:

  • 不,我不知道。我仍然有 404
  • 我的图片在 webapp/static/img/logo.gif 中
  • 能否请您向我们提供您的控制器类的代码、jsp{您尝试显示图像的位置} 和 spring-config 文件。这将帮助我们解决您的问题。
  • 好的。它在主题问题中
  • 您好,我根据您的要求制作了我的应用程序,它在我身边运行良好..我正在发布我的代码,请按照我的要求进行操作,希望您能得到您的问题解决方案。
猜你喜欢
  • 2015-04-12
  • 2023-04-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-20
相关资源
最近更新 更多