简体   繁体   English

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

[英]How to call a Spring MVC controller in jsp

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 header.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}是空的

I think, if you call an MVC controller from an MVC view, your application will brake common MVC principles. 我认为,如果从MVC视图调用MVC控制器,您的应用程序将制定常见的MVC原则。 Your code will be hard to debug, hard to test and hard to understand by others. 您的代码将难以调试,难以测试且难以被其他人理解。

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 @ModelAttribute annotated method to supply attributes to a few controller methods at once. 您可以使用@ModelAttribute注释方法一次为几个控制器方法提供属性。

You could use JSP tags instead of JSP pages to define your header and footer (at least that's how I do it in my application). 您可以使用JSP标记而不是JSP页面来定义页眉和页脚(至少我在应用程序中是这样做的)。

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. 其次,我看到你正在使用Spring框架,而在Spring中,从任何控制器到JSP,只有一个模型对象被传递给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. 因此,如果在标题中需要初始化另一个模型,则必须调用URL到控制器而不是调用header.jsp。

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中丰富模型,而不是每个控制器方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将Spring Mvc Controller 5连接到jsp文件? - How to connect Spring Mvc Controller 5 to jsp file? 如何在spring中从jsp调用控制器类 - How to call controller class from jsp in spring Spring MVC和JSP:如何将参数从控制器传递给JSP? - Spring MVC & JSP: How to pass a parameter from the controller to JSP? 从Spring MVC中的JSP按钮调用控制器方法 - Call controller method from JSP button in Spring MVC 如何使用onchange方法从Spring MVC中的JSP调用控制器方法 - How to call a controller method from jsp in Spring mvc using onchange method 如何通过从jsp中的下拉列表中选择值来调用Spring MVC Portlet Controller方法 - How to call Spring MVC Portlet Controller method by selecting value from a dropdown in jsp 如何检查JSP是否在Spring MVC中转到控制器,以及如何显示从控制器到JSP的列表? - How to check if jsp went to the controller in spring mvc and how to display list from controller to jsp? 如何将列表从控制器传递到jsp并在Spring MVC中的javascript中对其进行迭代 - how to pass a list from controller to jsp and iterate it in javascript in spring mvc Spring MVC:如何将表单数据从JSP传递到控制器 - Spring mvc : How to pass the form data from jsp to controller 如何将 java 对象从 jsp 发送到 Spring MVC 3 控制器 - How to send java object from jsp to Spring MVC 3 Controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM