Saturday, February 25, 2017

Spring Boot MVC + Free Marker

package com.example;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
public class FTLHelloWorld {
public static void main(String[] args) {
//Freemarker configuration object
Configuration cfg = new Configuration();
try {
//Load template from source folder
Template template = cfg.getTemplate("src/helloworld.ftl");

// Build the data-model
Map(String, Object) data = new HashMap(String, Object)();
data.put("message", "Hello World!");

//List parsing
List(String) countries = new ArrayList(String)();
countries.add("India");
countries.add("United States");
countries.add("Germany");
countries.add("France");

data.put("countries", countries);

// Console output
Writer out = new OutputStreamWriter(System.out);
template.process(data, out);
out.flush();

// File output
File f=new File("d:\\FTL_helloworld.txt");
f.createNewFile();
Writer file = new FileWriter (f);
template.process(data, file);
file.flush();
file.close();

} catch (IOException e) {
e.printStackTrace();
} catch (TemplateException e) {
e.printStackTrace();
}
}

}


package com.example;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class HelloController {
    @RequestMapping("/hello")
    public String hello(Model model, @RequestParam(value="name", required=false, defaultValue="World") String name) {
        model.addAttribute("name", name);
        return "hello";
    }
}


package com.example;
public class User {
private String firstname;
private String lastname;
public User() {
super();
}
public User(String firstname, String lastname) {
super();
this.firstname = firstname;
this.lastname = lastname;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
@Override
public String toString() {
return "User [firstname=" + firstname + ", lastname=" + lastname + "]";
}
}


package com.example;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class UserController {
/**
* 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";
}
}


application.properties
spring.freemarker.template-loader-path: /
spring.freemarker.suffix: .ftl


(!DOCTYPE html)
(html lang="en")
(head)
    (meta charset="UTF-8")
    (title)Hello ${name}!(/title)
(/head)
(body)
    (h2)Hello ${name}!(/h2)
(/body)
(/html)

(html)
(head)(title) FreeMarker Spring MVC Hello World(/title)
(body)
(div id="header")
(H2)
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)

 (dependency)
 (groupId)org.springframework.boot(/groupId)
 (artifactId)spring-boot-starter-freemarker(/artifactId)
 (/dependency)

No comments:

Post a Comment

உப்பு மாங்காய்

சுருக்குப்பை கிழவி. சுருக்கங்கள் சூழ் கிழவி. பார்க்கும் போதெல்லாம் கூடையுடனே குடியிருப்பாள். கூடை நிறைய குட்டி குட்டி மாங்காய்கள். வெட்டிக்க...