SORU
27 EYLÜL 2009, Pazar


Spring MVC 3 ve statik içerik işleme - bir şey mi kaçırdım?

Bir web MVC 3 Bahar ve DispatcherServlet tüm istekleri yakalamak için '' gibi (web.xml): . / kullanarak geliştiriyorum

  <servlet>
    <servlet-name>app</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  </servlet>

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

Şimdi bu reklamı olarak çalışır, ancak ne kadar statik içerik işleyebilir miyim? Daha önce, Dinlendirici URL kullanmadan önce, tüm *yakalayabilirdim.örneğin html ve ** 9, ama şimdi farklı bir top oyunu için gönderdi.

/Styles/, /js/, /içeren klasör görüntüleri/ /statik/ etc ve DispatcherServlet/* /statik çıkarmak istiyorum.

Şimdi ben ne zaman bu statik kaynakları çalışma alabilirim:

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

Ama güzel bir URL (beni " Spring MVC 3 ile) açılış sayfası www.domain.com/app/ olmamak istiyorum

Ben de bir çözüm tomcat birleştiğinde veya başka bir sunucu uygulaması konteyner istemiyorum, ve bu (nispeten) düşük trafik (çünkü apache gibi bir Web sunucusu önünde ihtiyacım yok.

Bu temiz bir çözüm var mı?

CEVAP
12 Aralık 2010, Pazar


Bu konuda çok zaman geçirdim beri, benim çözüm paylaşayım dedim. Beri bahar 3.0.4, bir yapılandırma parametre adı <mvc:resources/> (o reference documentation website) için kullanılabilir hizmet statik kaynakları ise hala kullanarak DispatchServlet üzerinde sitenizin kök.

Bunu kullanmak için, aşağıdaki gibi bir dizin yapısı kullanın:

src/
 springmvc/
  web/
   MyController.java
WebContent/
  resources/
   img/
    image.jpg
  WEB-INF/
    jsp/
      index.jsp
    web.xml
    springmvc-servlet.xml

Dosyaların içeriğini gibi görünmelidir:

src/springmvc/web/HelloWorldController.java:

package springmvc.web;

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

@Controller
public class HelloWorldController {

 @RequestMapping(value="/")
 public String index() {
  return "index";
 }
}

WebContent/WEB-INF/web.xml:

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

 <servlet>
  <servlet-name>springmvc</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
 </servlet>

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

WebContent/WEB-INF/springmvc-servlet.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:context="http://www.springframework.org/schema/context"
 xmlns:mvc="http://www.springframework.org/schema/mvc"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
 http://www.springframework.org/schema/mvc
 http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
 http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <!-- not strictly necessary for this example, but still useful, see http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-ann-controller for more information -->
 <context:component-scan base-package="springmvc.web" />

    <!-- the mvc resources tag does the magic -->
 <mvc:resources mapping="/resources/**" location="/resources/" />

    <!-- also add the following beans to get rid of some exceptions -->
 <bean      class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
 <bean
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
 </bean>

    <!-- JSTL resolver -->
 <bean id="viewResolver"
  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="viewClass"
   value="org.springframework.web.servlet.view.JstlView" />
  <property name="prefix" value="/WEB-INF/jsp/" />
  <property name="suffix" value=".jsp" />
 </bean>

</beans>

WebContent/jsp/index.jsp:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<h1>Page with image</h1>
<!-- use c:url to get the correct absolute path -->
<img src="<c:url value="/resources/img/image.jpg" />" />

Umarım bu yardımcı olur :-)

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • AutoHotkey Tutorials

    AutoHotkey T

    29 Mayıs 2010
  • atKristaBradford

    atKristaBrad

    4 Aralık 2010
  • fufko

    fufko

    27 ŞUBAT 2006