简体   繁体   中英

Spring mvc how to add a variable in controller and use it as a condition in jsp

Hi i have the following code that is executed when i submit form:

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

The bean is:

ViewDet.class

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

Once showPayment method is completed view.jsp is displayed and i am using jstl in the jsp. I have a view button in the jsp, and basically I want to add a check in the view.jsp that if colours and design are empty then hide the view button. Any idea how to do it from the controller please?

You need to add values in modelAndView object which is equivalent to adding in request and you need to return modelAndView from controller method.

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

In jsp you can check these attributes using jstl

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

Here is some simple example.

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM