1. 动态页面的渲染方式
1.1 服务器端渲染
HTML如下:
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>猜数字</title>
</head>
<body>
<div>
<form action="guessNum" method="post">
<input type="text" name="toGuess">
<input type="submit" value="猜">
</form>
</div>
<div>结果:</div>
</body>
</html>
Java代码如下:
package Thymeleaf;
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.util.Random;
@WebServlet("/guessNum")
public class GuessNumServlet extends HttpServlet {
Random random = new Random();
private int toGuess = 0;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html;charset=UTF-8");
toGuess = random.nextInt(100) + 1;
resp.getWriter().write("<!DOCTYPE html>\n" +
"<html lang=\"zh\">\n" +
"<head>\n" +
" <meta charset=\"UTF-8\">\n" +
" <title>猜数字</title>\n" +
"</head>\n" +
"<body>\n" +
"<div>\n" +
" <form action=\"guessNum\" method=\"post\">\n" +
" <input type=\"text\" name=\"toGuess\">\n" +
" <input type=\"submit\" value=\"猜\">\n" +
" </form>\n" +
"</div>\n" +
"<div>结果:</div>\n" +
"</body>\n" +
"</html>");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html;charset=UTF-8");
int user = Integer.parseInt(req.getParameter("toGuess"));
String result = "";
if (user < toGuess) {
result = "低了";
} else if (user > toGuess) {
result = "高了";
} else {
result = "答对了";
}
resp.getWriter().write("<!DOCTYPE html>\n" +
"<html lang=\"zh\">\n" +
"<head>\n" +
" <meta charset=\"UTF-8\">\n" +
" <title>猜数字</title>\n" +
"</head>\n" +
"<body>\n" +
"<div>\n" +
" <form action=\"guessNum\" method=\"post\">\n" +
" <input type=\"text\" name=\"toGuess\">\n" +
" <input type=\"submit\" value=\"猜\">\n" +
" </form>\n" +
"</div>\n" +
"<div>结果:</div>\n" + result +
"</body>\n" +
"</html>");
}
}
会发现在不使用模版引擎的情况下,当前的Java代码(代表着业务逻辑)和HTML代码(代表着用户界面)混合在一起,非常的不友好。尤其是HTML比较长,比较复杂的时候。
1.2 客户端渲染「Thymeleaf」
HTML文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>MessageWal</title>
</head>
<body>
<style>
* {
margin: 0px;
padding: 0px;
box-sizing: border-box;
}
.container {
width: 600px;
margin: 0 auto;
}
h1 {
text-align: center;
padding: 20px 0px;
}
p {
font-size: 12px;
color: grey;
padding: 10px 0px;
text-align: center;
}
.row {
display: flex;
height: 40px;
justify-content: center;
align-items: center;
}
.row span {
width: 100px;
}
.row .edit {
width: 200px;
height: 38px;
}
.row .submit {
width: 300px;
height: 40px;
background-color: orange;
border: none;
color: white;
}
.row .submit:active {
background-color: grey;
}
</style>
<div class="container">
<h1>表白墙</h1>
<p>输入后点击提交,将会吧信息显示在表格中</p>
<form action="message" method="post">
<div class="row">
<span>谁:</span>
<input type="text" class="edit" name="from">
</div>
<div class="row">
<span>对谁:</span>
<input type="text" class="edit" name="to">
</div>
<div class="row">
<span>说什么:</span>
<input type="text" class="edit" name="message">
</div>
<div class="row">
<input type="submit" value="提交" class="submit">
</div>
</form>
<div class="row" th:each="message:${messages}">
<span th:text="${message.from}"></span>
对
<span th:text="${message.to}"></span>
说:
<span th:text="${message.message}"></span>
</div>
</div>
<script>
</script>
</body>
</html>
Servlet代码
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.WebContext;
import org.thymeleaf.templateresolver.ServletContextTemplateResolver;
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.util.ArrayList;
class Message {
public String from;
public String to;
public String message;
}
@WebServlet("/message")
public class MessageServlet extends HttpServlet {
private TemplateEngine engine = new TemplateEngine();
private ArrayList<Message> messages = new ArrayList<>();
@Override
public void init() throws ServletException {
ServletContextTemplateResolver resolver = new ServletContextTemplateResolver(getServletContext());
resolver.setPrefix("/WEB-INF/template/");
resolver.setSuffix(".html");
resolver.setCharacterEncoding("UTF-8");
engine.setTemplateResolver(resolver);
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html;charset=UTF-8");
WebContext webContext = new WebContext(req, resp, getServletContext());
webContext.setVariable("messages", messages);
engine.process("MessageWall", webContext, resp.getWriter());
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("UTF-8");
Message message = new Message();
message.from = req.getParameter("from");
message.to = req.getParameter("to");
message.message = req.getParameter("message");
messages.add(message);
resp.setContentType("text/html;charset=UTF-8");
WebContext webContext = new WebContext(req, resp, getServletContext());
webContext.setVariable("messages", messages);
engine.process("MessageWall", webContext, resp.getWriter());
}
}
文件目录
|