Spring mvc如何在控制器中添加变量并将其用作jsp中的条件

时间:2014-10-09 15:50:42

标签: java jsp spring-mvc jstl

嗨我有以下提交表单时执行的代码:

@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);
        }
    }

豆子是:

ViewDet.class

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

一旦showPayment方法完成,就会显示view.jsp,我在jsp中使用jstl。我在jsp中有一个视图按钮, 基本上我想在view.jsp中添加一个检查,如果颜色和设计为空,则隐藏视图按钮。 知道如何从控制器那里做到吗?

2 个答案:

答案 0 :(得分:1)

您需要在modelAndView对象中添加值,这相当于添加请求,您需要从控制器方法返回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是一个简单的例子。

答案 1 :(得分:0)

use c:if tag

test your variable in the c:if condition pass  your variables from controller and check

    <c:if test="${colors}==null&&${design}==null">
// if condition is true then it will display the button otherwise not
    <input type=button value="view"/>

    </c:if>