Java 如何在jsp中调用Spring MVC控制器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20756121/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 04:06:58  来源:igfitidea点击:

How to call a Spring MVC controller in jsp

javaspringjspspring-mvc

提问by Ajouve

I'd like to include a jsp page for exemple

我想包含一个jsp页面作为例子

<ui:include src="/WEB-INF/jsp/header.jsp" />

but my header.jsp have variables and I need a controller to initialise theses variables, is there a way to call a controller and include the controller method jsp in an other jsp ?

但是我的 header.jsp 有变量,我需要一个控制器来初始化这些变量,有没有办法调用控制器并将控制器方法 jsp 包含在另一个 jsp 中?

For exemple;

举个例子;

<%@tag description="Overall Page template" pageEncoding="UTF-8"%>
<%@attribute name="header" fragment="true"%>
<%@attribute name="footer" fragment="true"%>
<html>
<body>
    <div id="pageheader">
        //include my header controller
        <ui:include src="/WEB-INF/jsp/header.jsp" />
    </div>
    <div id="body">
        <jsp:doBody />
    </div>
    <div id="pagefooter">
        //include my footer controller
    </div>
</body>
</html>

header.jsp

头文件.jsp

Header
${test}

my header method

我的标题方法

public String header(Map<String, Object> model){
    model.put("test", "test");
    return "header";
}

But the controller is not used and ${test}is empty

但是控制器没有使用并且${test}是空的

采纳答案by Alexey

I think, if you call an MVC controller from an MVC view, your application will brake common MVC principles. Your code will be hard to debug, hard to test and hard to understand by others.

我认为,如果您从 MVC 视图调用 MVC 控制器,您的应用程序将破坏常见的 MVC 原则。您的代码将难以调试、难以测试且难以被其他人理解。

It, probably, would be a better idea to prepare model attributes for all parts of your view (the main part of the page, header and footer) in one controller. The model that you pass to a JSP view is available in included custom JSP tags and JSPs.

在一个控制器中为视图的所有部分(页面的主要部分、页眉和页脚)准备模型属性可能是一个更好的主意。您传递给 JSP 视图的模型在包含的自定义 JSP 标记和 JSP 中可用。

You can use a @ModelAttributeannotated method to supply attributes to a few controller methods at once.

您可以使用@ModelAttribute注释方法一次为几个控制器方法提供属性。

You could use JSP tagsinstead of JSP pages to define your header and footer (at least that's how I do it in my application).

您可以使用JSP 标记而不是 JSP 页面来定义页眉和页脚(至少我在我的应用程序中是这样做的)。

回答by Wins

First of all, your JSP should not "call" directly to any controller.

首先,您的 JSP 不应直接“调用”任何控制器。

Secondly, I see you're using Spring framework, and in Spring, from any controller to JSP, there is only 1 model object being passed to the JSP. Therefore if in the header you need to initialise another model, you have to call the URL to the controller instead of calling the header.jsp.

其次,我看到您使用的是 Spring 框架,在 Spring 中,从任何控制器到 JSP,只有 1 个模型对象被传递给 JSP。因此,如果您需要在标头中初始化另一个模型,则必须调用控制器的 URL,而不是调用 header.jsp。

回答by Ralph

The ther answers already explained that the model should be populated by an controller. ... If you need the same model variables in all pages, for example in the header or footer, the a common way is to enrich the model in a single HandlerInterceptor, instead of every controller method.

答案已经解释了模型应该由控制器填充。...如果您需要在所有页面中使用相同的模型变量,例如在页眉或页脚中,常见的方法是在单个 HandlerInterceptor 中丰富模型,而不是每个控制器方法。