Project address: github.com/chywx/sprin…

background

As the company’s business is oriented towards the African market, there are some countries where smartphones are not popular, such as Uganda, which is still dominated by feature phones. In order to support feature phones, we need to create a new WAP website to support feature phones (holy shit!).

Technology selection

Because the functional machine does not support JS, it is not practical to use Vue at the front end. JSP, Freemarker, Thymeleaf and other engines can only be used in the form of templates. However, springboot is used at the back end of the final selection of Velocity template

# Simple project architecture

# springboot consolidation velocity

Version selection

Select Spring-boot-starter-Velocity. Springboot 1.4.7 is not supported by earlier versions of Springboot

Pom.xml adds dependencies

<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-velocity</artifactId>
    </dependency>
Copy the code

Application.properties Adds the configuration

server.port=2828
spring.velocity.cache=false
spring.velocity.charset=UTF-8
spring.velocity.check-template-location=true
spring.velocity.content-type=text/html
spring.velocity.enabled=true
spring.velocity.prefix=/templates/
spring.velocity.suffix=.vm
Copy the code

Add demo tests to the Controller layer

@Controller
@RequestMapping("/velocity") public class TestController {// Demo test @requestMapping ("/demo")
    public String demo1(Map map) {
        map.put("message"."This is the test...");
        map.put("time", System.currentTimeMillis());
        return "index"; }}Copy the code

Templates folder, new home index.vm

<html> <body> $! {message} $! {time} </body> </html>Copy the code

Visit the Index page

The simplest springboot http://localhost:2828/velocity/demo ok, finished consolidation velocity

Date and time processing

How do I customize the timestamp format

1. Add the toolbox. XML

The following

<? xml version="1.0" encoding="UTF-8"? > <toolbox> <tool> <key>DateTool</key> <scope>application</scope> <class>org.apache.velocity.tools.generic.DateTool</class> </tool> </toolbox>Copy the code

Of course, if you need to deal with numbers, you can also introduce a tool

2. Specify the location of toolbox. XML

Application. The properties to add spring. Velocity. The toolbox – config – location = / toolbox. The XML

3. Use it on the VM page

< H1 > Date processing </ H1 > Before processing:$time<br> After processing: $! DateTool.format($! time)Copy the code

Unified abnormal page processing

New VelocityExceptionHander

@ControllerAdvice
public class VelocityExceptionHander {

    @ExceptionHandler(value = Exception.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public String exceptionHandler(Exception e, HttpServletRequest request) {
        System.out.println("Unknown exception! The reason is:" + e);
        request.setAttribute("msg", e);
        return "500"; }}Copy the code

Create a 500. VM template page

<html> <body> error <br> $! msg </body> </html>Copy the code

If an exception occurs later, page 500 is redirected.

Finally, summarize the common basic syntax tags

// RequestMapping("/allDemo")
    public String demo3(Map map) {
        map.put("amount", 100);
        map.put("msg"."dahai");
        map.put("sex"."man");
        putString(map);
        putSportList(map);
        map.put("time", System.currentTimeMillis());
        return "allDemo";
    }

    private void putSportList(Map map) {
        List<Sport> sportList = new ArrayList<Sport>() {{
            add(new Sport(1, "Football"));
            add(new Sport(2, "Basketball"));
            add(new Sport(3, "tennis"));
            add(new Sport(4, "rugby"));
            add(new Sport(5, "cricket"));
        }};
        map.put("sportList", sportList);
        Map<Integer, Sport> sportMap = sportList.stream().collect(Collectors.toMap(Sport::getId, s -> s));
        map.put("sportMap", sportMap);
    }

    private void putString(Map map) {
        List<String> strings = new ArrayList<>();
        strings.add("a");
        strings.add("b");
        strings.add("c");
        map.put("strings", strings);
    }
Copy the code

Vm Template page

<! DOCTYPE HTML PUBLIC"- / / / / W3C DTD HTML 4.01 Transitional / / EN"
    "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title>velocity test</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> </head> <body> <h1> String type </h1> $! msg <h1>ifTag < / h1 >#if($! sex == 'man')#else#end

<h1>setAction (define variables)</h1>#set($hw = 'ok')
    $hw< h1 > generalforCycle < / h1 >#foreach($! s in $! strings)
        $s &nbsp;
    #end< h1 > objectforCycle < / h1 >#foreach($! sport in $! sportList)$! sport.name &nbsp;#end

<h1>map forCycle < / h1 >#foreach($! sp in $! sportMap.keySet())
        $sp&nbsp; $! sportMap.get($sp).name &nbsp; <br>#end< H1 > Date processing </ H1 > Before processing:$time<br> After processing: $! DateTool.format($! Time) <h1> Calculate addition, subtraction, multiplication, and division </h1>#set($jia = $amount + 10)+ 10 =$jia   <br>
    #set($jian = $amount - 10)- 10 =$jian  <br>
    #set($cheng = $amount * 10)X = 10$cheng <br>
    #set($chu = $amount / 10)Present 10 =$chu   <br>


</body>
</html>
Copy the code