2014-12-09 3 views
76

मैं जावा और स्प्रिंग के लिए नया हूं। मैं अपने ऐप रूट http://localhost:8080/ को स्थिर index.html पर कैसे मैप कर सकता हूं? यदि मैं http://localhost:8080/index.html पर नेविगेट करता हूं तो यह ठीक काम करता है।जावा स्प्रिंग बूट: index_ में my app root ("/") को कैसे मैप करें?

मेरा ऐप संरचना होती है:

dirs

मेरे config\WebConfig.java इस तरह दिखता है:

@Configuration 
@EnableWebMvc 
@ComponentScan 
public class WebConfig extends WebMvcConfigurerAdapter { 

    @Override 
    public void addResourceHandlers(ResourceHandlerRegistry registry) { 
     registry.addResourceHandler("/**").addResourceLocations("/"); 
     } 
} 

मैं registry.addResourceHandler("/").addResourceLocations("/index.html"); जोड़ने की कोशिश की, लेकिन यह विफल रहता है।

+1

आप किसी भी नियंत्रक @Requestmapping("/") लिखा है हो सकता है कि इस मदद करता है: http://stackoverflow.com/questions/20405474/spring-boot- संदर्भ जड़ –

+3

यह @UdoKlimaschewski 'कैसे मैप करने के लिए http से पता चलता: // स्थानीय होस्ट: 8080/appName' लेकिन इसकी मैं क्या जरूरत नहीं है ... – Shoham

उत्तर

96

यह बॉक्स से बाहर काम किया है | यदि आप @EnableWebMvc एनोटेशन इस्तेमाल नहीं किया था। जब आप ऐसा करते हैं तो आप WebMvcAutoConfiguration में स्प्रिंग बूट की सभी चीजों को बंद कर देते हैं। आपको लगता है कि टिप्पणी निकालने सकता है, या आप दृश्य नियंत्रक है कि आप बंद वापस जोड़ सकते हैं:

@Override 
public void addViewControllers(ViewControllerRegistry registry) { 
    registry.addViewController("/").setViewName("forward:/index.html"); 
} 
+1

धन्यवाद .. मुझे एहसास हुआ नहीं था कि ... एक साधारण नहीं पा सके अपनी साइट में डेमो कि पता चलता है कि ... – Shoham

+7

[संदर्भ डॉक्स] से (http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-spring-mvc-auto कॉन्फ़िगरेशन): "यदि आप स्प्रिंग बूट एमवीसी फीचर्स रखना चाहते हैं, और आप बस अतिरिक्त एमवीसी कॉन्फ़िगरेशन जोड़ना चाहते हैं (इंटरसेस ptors, formatters, दृश्य नियंत्रक आदि) आप प्रकार के अपने खुद के @Bean 'WebMvcConfigurerAdapte'r जोड़ सकते हैं, लेकिन बिना' @ EnableWebMvc' " –

+1

इस पर'/'' index.html' में काम करेगा। लेकिन क्या ब्राउज़र को वास्तव में यूआरएल को '/' से '/ index.html' में बदलना संभव है? – asmaier

36

डेव Syer के जवाब का एक उदाहरण:

import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; 
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; 

@Configuration 
public class MyWebMvcConfig { 

    @Bean 
    public WebMvcConfigurerAdapter forwardToIndex() { 
     return new WebMvcConfigurerAdapter() { 
      @Override 
      public void addViewControllers(ViewControllerRegistry registry) { 
       // forward requests to /admin and /user to their index.html 
       registry.addViewController("/admin").setViewName(
         "forward:/admin/index.html"); 
       registry.addViewController("/user").setViewName(
         "forward:/user/index.html"); 
      } 
     }; 
    } 

} 
+0

स्प्रिंग 5. –

3

अगर यह एक स्प्रिंग बूट अनुप्रयोग है।

स्प्रिंग बूट स्वचालित रूप से सार्वजनिक/स्थिर/webapp फ़ोल्डर में index.html पता लगाता है। यह डिफ़ॉल्ट सुविधा को पार कर जाएगी और यह index.html नहीं दिखाया जाएगा, जब तक कि आप टाइप localhost:8080/index.html

+1

में पदावनत WebMvcConfigurerAdapter के बजाय WebMvcConfigurer उपयोग मैं एक src/मुख्य/संसाधन/सार्वजनिक/index.html फ़ाइल बनाया है और यह काम किया! धन्यवाद –

2
@Configuration 
@EnableWebMvc 
public class WebAppConfig extends WebMvcConfigurerAdapter { 

    @Override 
    public void addViewControllers(ViewControllerRegistry registry) { 
     registry.addRedirectViewController("/", "index.html"); 
    } 

} 
संबंधित मुद्दे