gpt4 book ai didi

java - 如果您在 View 上使用 JSTL,Spring MVC 会给您带来什么?

转载 作者:行者123 更新时间:2023-11-30 05:58:38 25 4
gpt4 key购买 nike

刚刚开始学习 Java 方面的东西,如果这是显而易见的,抱歉。但是,我正在阅读有关 Java 6 中的 Spring 的教程,发现他们在 View 上仅使用常规的旧 JSTL。

如果我使用 JSTL 作为 View ,Spring 会给我什么?如果我使用“just java”(我知道都是 Java,但我的意思是除了像 Spring 或 Struts 这样的框架之外),我会使用什么?

谢谢。

编辑:

我猜 JSTL 就是 V。这就是我的观点。如果我已经有了不带 Spring 的 V,那么我能得到什么是 Java 所没有的呢?如果没有 Spring 我必须提供什么(请不要说 MC!)

编辑:也许我在问。如果我不使用 Spring、Struts 或类似的东西,我会在 Java 中使用什么来实现 MVC。对于 Java 6,我有哪些开箱即用的 MVC 和 JSTL。还有哪些其他组件(我保留 JSTL 是有原因的)。谢谢。

最佳答案

这是一个非常简单的例子。当在普通 Vanilla 时Servlet model 2你写这样的东西:

public class AdditionServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException {
String x = request.getParameter("x");
String y = request.getParameter("y");
if (x == null || y == null) throw new ServletException();
try {
request.setAttribute("result",
Integer.parseInt(x) + Integer.parseInt(y));
} catch (NumberFormatException ex) {
throw new ServletException(ex);
}
request.getRequestDispatcher("/WEB-INF/jsp/calc.jsp")
.forward(request, response);
}
}

在 Spring MVC 中你这样写:

@Controller
public class ArithmeticController {
@RequestMapping("/add")
public String add(@RequestParam("x") int x, @RequestParam("y") int y,
Map<String, Object> model) {
model.put("result", x + y);
return "calc";
}
}

两个 Controller 都使用相同的 JSTL View ,但样板代码的数量不同。

关于java - 如果您在 View 上使用 JSTL,Spring MVC 会给您带来什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4125079/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com