artTemplate的简单用法

在实际开发中,我们会使用模板引擎来提高开发效率。而众多的模板引擎中,artTemplate无论就速度,兼容性还是使用人数上,都占绝对优势。这里我将简单地介绍下artTemplate的常见用法。

需要先使用一个type="text/html"的script标签存放模板

1.简单的json数据



    
        
        
        
    
    
        

2.带数组的json数据



    
        
        
        
    
    
        

3.数组中包含对象的json数据



    
        
        
        
    
    
        

一个ajax+artTemplate+servlet+fastjson的综合小实例

前台:


<%@ page contentType="text/html;charset=UTF-8" language="java" %>

  
    $Title$
    
    
  
  
  

后台:

public class Person {
    public String name;
    public int    age;
}

public class Persons {
    private List list;

    public List getList() {
        return list;
    }

    public void setList(List list) {
        this.list = list;
    }
}

import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by ttc on 17-5-26.
 */
@WebServlet(name = "Servlet",urlPatterns = "/ajax.do")
public class Servlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Person person = new Person();
        person.name = "huochai";
        person.age = 29;

        String strJson = JSON.toJSONString(person);
        System.out.println(strJson);

        Person person2 = new Person();
        person2.name = "huochai2";
        person2.age = 292;

        List lst = new ArrayList();
        lst.add(person);
        lst.add(person2);

        Persons persons = new Persons();
        persons.setList(lst);

        String strJson2 = JSON.toJSONString(persons);
        System.out.println(strJson2);

        PrintWriter pw = response.getWriter();
        pw.println(strJson2);
    }
}

你可能感兴趣的:(java,javascript)