2013-08-01 2 views
2

spring mvc에서 정적 리소스를 사용하는 방법은 무엇입니까? jsp에 몇 개의 이미지를 삽입하고 싶습니다. 어떤 곳에서 자원을 찾아야합니까? 마지막으로 서블릿 컨텍스트에서 mvc : recources를 추가했지만 맵핑에 오류가 거의 없음을 보여줍니다.봄 mvc 정적 리소스를 사용하는 방법?

아직 문제가 있습니다. 나는 또 다른 질문을했다. 제발, 해프. 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> 
+0

여기에 이미 답변이 있습니다. 확인해보십시오. http://stackoverflow.com/questions/14751597/springmvc-and-static-resources – Sheel

+0

확인. 하지만 저는 servlet-context에서 \t '를 추가 한 후 404를 가지고 있습니다. – babobka

+0

'appServlet'이라는 이름의 DispatcherServlet에서 URI [/ blog /]가있는 HTTP 요청에 대한 매핑이 없습니다. 내가 말했듯이. – babobka

답변

0

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> 
+0

ServletContext 자원에서 XML 문서를 파싱하는 IOException [/WEB-INF/app-servlet.xml]; 중첩 예외가 java.io.FileNotFoundException : ServletContext 리소스 [/WEB-INF/app-servlet.xml]를 열 수 없습니다. – babobka

+0

web-inf 폴더가 아닌 webcontent에서보기 파일을 제공했는지 제대로 확인하십시오. 올바르게 확인하십시오. 또한 같은 대답으로 anoyther 매핑 파일을 제공합니다. – Sheel

+1

여전히 404 오류가 발생하면 잘못된 방법으로 web-inf 폴더 아래에 파일을 제공한다는 의미입니다. – Sheel

0

부러워 옳다고 봅니다. 그건 그렇고, 당신은 코드

<?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"); 
    } 
} 

의 아래 조각에서 코드를 수정할 수 있습니다 내 코드를 수정했습니다. C를 따르십시오. 오드 그리고 내게 알려주세요, 당신의 해결책을 얻지 못하겠습니다 :)

+0

아니요. 404가 여전히 있습니다. – babobka

+0

내 이미지가 webapp/static/img/logo에 있습니다.gif – babobka

+0

컨트롤러 클래스, jsp {이미지를 표시하려고하는 곳} 및 spring-config 파일의 코드를 제공해 주시겠습니까? 그러면 문제를 해결하는 데 도움이됩니다. – Ashish

관련 문제