2015-07-07 19 views
7

मैं स्प्रिंग बूट और फ्रीमार्कर एकीकरण (वेब ​​पर मिले ट्यूटोरियल्स के आधार पर) के एक साधारण उदाहरण के माध्यम से काम करने की कोशिश कर रहा हूं। किसी कारण से मेरा विचार FreeMarker टेम्पलेट को हल नहीं किया जा रहा है (मुझे लगता है कि यह मुद्दा है)।स्प्रिंग बूट और फ्रीमार्कर

ब्राउज़र में लॉन्च होने पर परिणाम केवल टीएफएल व्यू फ़ाइल यानी "इंडेक्स" का नाम वापस करने के लिए है। इसलिए नियंत्रक को स्ट्रिंग "इंडेक्स" कहा जा रहा है और वापस लौटा रहा है, लेकिन ऐसा लगता है कि एफटीएल फ़ाइल में खुद को खींचने के लिए कोई ट्रिगर नहीं है। किसी भी मदद की सराहना की जाएगी ...

मेरे पास निम्न कॉन्फ़िगरेशन क्लास है जहां मैं व्यू रिज़ॉल्यूवर और फ्री मेकर कॉन्फ़िगरेशन को परिभाषित करता हूं।

@Configuration 
public class MvcConfigurer extends WebMvcConfigurerAdapter { 
    @Bean 
    public ViewResolver viewResolver() { 
     FreeMarkerViewResolver resolver = new FreeMarkerViewResolver(); 
     resolver.setCache(true); 
     resolver.setPrefix(""); 
     resolver.setSuffix(".ftl"); 
     resolver.setContentType("text/html; charset=UTF-8"); 
     return resolver; 
    } 

    @Bean 
    public FreeMarkerConfigurer freemarkerConfig() throws IOException, TemplateException { 
     FreeMarkerConfigurationFactory factory = new FreeMarkerConfigurationFactory(); 
     factory.setTemplateLoaderPaths("classpath:templates", "src/main/resource/templates"); 
     factory.setDefaultEncoding("UTF-8"); 
     FreeMarkerConfigurer result = new FreeMarkerConfigurer(); 
     result.setConfiguration(factory.createConfiguration()); 
     return result; 
    } 
} 
फिर

मैं निम्नलिखित नियंत्रक है

@RestController 
public class HelloController { 

    /** 
    * Static list of users to simulate Database 
    */ 
    private static List<User> userList = new ArrayList<User>(); 

    //Initialize the list with some data for index screen 
    static { 
     userList.add(new User("Bill", "Gates")); 
     userList.add(new User("Steve", "Jobs")); 
     userList.add(new User("Larry", "Page")); 
     userList.add(new User("Sergey", "Brin")); 
     userList.add(new User("Larry", "Ellison")); 
    } 

    /** 
    * Saves the static list of users in model and renders it 
    * via freemarker template. 
    * 
    * @param model 
    * @return The index view (FTL) 
    */ 
    @RequestMapping(value = "/index", method = RequestMethod.GET) 
    public String index(@ModelAttribute("model") ModelMap model) { 

     model.addAttribute("userList", userList); 

     return "index"; 
    } 

    /** 
    * Add a new user into static user lists and display the 
    * same into FTL via redirect 
    * 
    * @param user 
    * @return Redirect to /index page to display user list 
    */ 
    @RequestMapping(value = "/add", method = RequestMethod.POST) 
    public String add(@ModelAttribute("user") User user) { 

     if (null != user && null != user.getFirstname() 
       && null != user.getLastname() && !user.getFirstname().isEmpty() 
       && !user.getLastname().isEmpty()) { 

      synchronized (userList) { 
       userList.add(user); 
      } 
     } 
     return "redirect:index.html"; 
    } 
} 

फिर अंत में मैं निम्नलिखित FTL फ़ाइल "src/मुख्य/संसाधन/टेम्पलेट्स" में संग्रहीत किए गए

<html> 
<head><title>ViralPatel.net - FreeMarker Spring MVC Hello World</title> 
<body> 
<div id="header"> 
<H2> 
    <a href="http://viralpatel.net"><img height="37" width="236" border="0px" src="http://viralpatel.net/blogs/wp-content/themes/vp/images/logo.png" align="left"/></a> 
    FreeMarker Spring MVC Hello World 
</H2> 
</div> 

<div id="content"> 

    <fieldset> 
    <legend>Add User</legend> 
    <form name="user" action="add.html" method="post"> 
    Firstname: <input type="text" name="firstname" /> <br/> 
    Lastname: <input type="text" name="lastname" /> <br/> 
    <input type="submit" value=" Save " /> 
    </form> 
    </fieldset> 
    <br/> 
    <table class="datatable"> 
    <tr> 
     <th>Firstname</th> <th>Lastname</th> 
    </tr> 
    <#list model["userList"] as user> 
    <tr> 
     <td>${user.firstname}</td> <td>${user.lastname}</td> 
    </tr> 
    </#list> 
    </table> 

</div> 
</body> 
</html> 
+1

आप स्प्रिंग बूट के किन संस्करणों का उपयोग कर रहे हैं से मिला? क्या आपने देखा है कि फ्रीमार्कर टेम्पलेट्स के लिए स्प्रिंग बूट समर्थन ऑटो कॉन्फ़िगरेशन? [यह] देखें (http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#boot-features-spring-mvc-template-engines) और [यह] (https: //github.com/spring-projects/spring-boot/blob/master/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/freemarker/FreeMarkerAutoConfiguration.java) – geoand

+0

हाय, मैं हूं वसंत बूट v1.2.5.RELEASE का उपयोग कर। धन्यवाद, मैं अब लिंक देख लूंगा। –

उत्तर

10

समस्या है कि आपके नियंत्रक में गलत एनोटेशन है। आप @RestController के बजाय @Controller का उपयोग करना चाहिए

@RestController बताने के लिए कि प्रतिक्रिया अपने नियंत्रक से भेजे गए, ब्राउज़र को भेजा जाना चाहिए आमतौर पर एक वस्तु json को मैप किया जाता है। यह @ResponseBody जोड़ने जैसा ही है।

+0

धन्यवाद! वह था ... अब बदल गया और ठीक काम कर रहा था। अच्छी तरह से देखा गया। –

1

हालांकि आपको अभी जवाब मिला है। हालांकि, आपकी पोस्ट में दो अंक हैं।

सबसे पहले, स्प्रिंग बूट में फ्रीमार्कर टेम्पलेट को कॉन्फ़िगर करना काफी आसान है। WebMvcConfigurerAdapter का उपयोग करने की कोई आवश्यकता नहीं है। तुम बस नीचे

spring.freemarker.template-loader-path: /templates 
spring.freemarker.suffix: .ftl 

दूसरे सामग्री के साथ अपने वर्ग के रास्ते पर अपनी संपत्ति जगह की जरूरत है, के रूप में @Controller वसंत MVC नियंत्रक एनोटेट वर्गों के लिए प्रयोग किया जाता है। @RestController एनोटेटेड कक्षाएं @ नियंत्रक के समान हैं लेकिन हैंडलर विधियों पर @ResponseBody को निहित किया गया है। तो आपको अपने मामले में @ नियंत्रक का उपयोग करना होगा।

पद Spring Boot FreeMarker Hello World Example

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