gpt4 book ai didi

java - Spring mvc如何在 Controller 中添加变量并将其用作jsp中的条件

转载 作者:行者123 更新时间:2023-12-01 12:26:37 25 4
gpt4 key购买 nike

您好,我提交表单时执行以下代码:

@ActionMapping(params = "ConController=showPayment")
public void showPayment(ActionResponse response, @ModelAttribute("conPayForm") ConsiPayForm consiForm, Model model) {
PaymentDetailsResponse paymentDetailsResponse = stand.getDet();

ViewDet view = stand.getView();
...
PaymentDetails paymentDetails = paymentDetailsResponse.getPayDetails();
....
model.addAttribute("paymentDetails", paymentDetails);
}
}

该 bean 是:

ViewDet.class

List <String> colours;
List <String> design;
...

showPayment 方法完成后,将显示 view.jsp,并且我在 jsp 中使用 jSTL。我在jsp中有一个查看按钮,基本上我想在 view.jsp 中添加一个检查,如果颜色和设计为空,则隐藏 View 按钮。请问您知道如何从 Controller 中执行此操作吗?

最佳答案

您需要在modelAndView对象中添加值,这相当于在请求中添加,并且您需要从 Controller 方法返回modelAndView。

     @ActionMapping(params = "ConController=showPayment")
public ModelAndView showPayment(ActionResponse response, @ModelAttribute("conPayForm") ConsiPayForm consiForm, Model model) {
PaymentDetailsResponse paymentDetailsResponse = stand.getDet();

ViewDet view = stand.getView();
...
PaymentDetails paymentDetails = paymentDetailsResponse.getPayDetails();
....
model.addAttribute("paymentDetails", paymentDetails);
ModelAndView modelAndView = new ModelAndView("view"); //as per view resolver
modelAndView.addObject("",colorsList);
modelAndView.addObject("design",designList);
return modelAndView;
}
}

在jsp中你可以使用jSTL检查这些属性

<c:if test="${not empty colorsList}">
... display button
</c:if>

Here是一些简单的例子。

关于java - Spring mvc如何在 Controller 中添加变量并将其用作jsp中的条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26282747/

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