Package com. Servlet;
Import the Java. IO. IOException;
Import the Java. IO. PrintWriter;
Import the Java. IO. Writer;
import java.util.ArrayList;
Import the Java. Util. HashMap;
import java.util.List;
Import the Java. Util. The Map;
The import javax.mail. Servlet. ServletException;
The import javax.mail. Servlet. The annotation. WebServlet;
The import javax.mail. Servlet. HTTP. HttpServlet;
The import javax.mail. Servlet. HTTP. It;
The import javax.mail. Servlet. HTTP. HttpServletResponse;
The import com. The entity. The Food;
Import the entity. The News;
The import com. Services. FoodService;
The import com. Services. NewsService;
The import com. Services. Impl. FoodServiceImpl;
The import com. Services. Impl. NewsServiceImpl;
. Import freemarker template. *;
@ WebServlet (urlPatterns={} "/Index" and name="IndexServlet")
Public class IndexServlet extends the HttpServlet {
Private Configuration CFG.
Public void init () {
CFG=new Configuration ();
CFG. SetDefaultEncoding (" utf-8 ");
CFG. SetServletContextForTemplateLoading (getServletContext (),
"WEB - INF/templates");
}
Public void doGet (it request, HttpServletResponse response)
Throws ServletException, IOException {
FoodService FoodService=new FoodServiceImpl ();
NewsService NewsService=new NewsServiceImpl ();
The Map root=new HashMap ();
List
Root. The put (" foods ", a list);
//Map
//List
//root1. Put (" list1, "list1);
The Template t=CFG. GetTemplate (" index. The FTL ");
response.setContentType("text/html; Charset="+ t.g etEncoding ());
Writer out=response. GetWriter ();
Try {
T.p rocess (root, out, ObjectWrapper BEANS_WRAPPER);
} the catch (TemplateException e) {
Throw new ServletException (" process template wrong ", e);
} the finally {
If (out!=null) {
out.close();
}
out.flush();
}
}
}
Template code
<body>
<# list of foods as food>
The ${food. Id}
${food. ChineseName}
# list>