JSP是否为超级系统初始,如果是,是如何设置的?

pkbketx9  于 2022-12-07  发布在  其他
关注(0)|答案(1)|浏览(151)

我已经有一段时间没有使用JSP和servlet了。现在我发现一些材料说JSP已经过时了,不应该使用。是这样吗?为什么?如果我们用Spring Framework开始一个新的项目,并希望呈现网页或类似的输出,我们应该用什么来代替模板呢?

0x6upsns

0x6upsns1#

Spring (as well as Struts, Apache Wicket and other frameworks) is based on servlets. When you're using Spring for web, you're using servlet as an underlying technology.
JSP is really a little bit outdated. And there are some inconveniences in it. For example, JSP is a real headache for web designers. Designers cannot just open a JSP file, make some changes, and check the result in the browser, because the JSP file contains tags that are invalid for HTML. The only way to see how the page will look like in the browser is to deploy the application and let the server render it.
Another inconvenience in JSP is that you can't externalize common layout into a dedicated file. All you can do is import one page into another with <jsp:include> . And if you have hundreds of files, you have to repeat the same <jsp:include> in all of them to copy common parts.
There are template engines that are more suitable for big projects when you have hundreds of complex dynamical pages. One popular template engine is Thymeleaf .
Thymeleaf's templates contain just valid HTML. That means designers and programmers can work in parallel. Also it has a good layout system. Moreover, Thymeleaf has much more readable and elegant syntax in contrast to JSP. Here is an example of the code for generating a simple table in Thymeleaf:

<html>
....
<table>
  <tr>
    <th>Name</th>
    <th>Price</th>
    <th>In stock</th>
  </tr>
  <tr th:each="prod : ${prods}">
    <td th:text="${prod.name}">Onions</td>
    <td th:text="${prod.price}">2.41</td>
    <td th:text="${prod.inStock}? #{true} : #{false}">yes</td>
  </tr>
</table>
....
</html>

There are many other additional features like build-in support of internationalization, passing parameters to fragments and so on.
You can find more details on the comparison of Thymeleaf and JSP here and here .

相关问题