singapore saravanan
singapore saravanan

Reputation: 83

@ModelAttribute at method level. Access this from other controller. Is it possible?

@Controller
@RequestMapping("lookup")
public class LookupController {

    @ModelAttribute("status")
    public List<String> status() {
        List<String> lstStatus = new ArrayList<String>();
        lstStatus.add("Open");
        lstStatus.add("Reserved");
        lstStatus.add("Parked");
        lstStatus.add("Inactive");
        return lstStatus;
    }

}



@Controller
@RequestMapping("parkingSlot")
public class ParkingSlotController   {

    @RequestMapping("edit/{slotId}")
    public String edit(@PathVariable Integer slotId, Map<String, Object> model)     {
        ParkingSlot parkingSlot = parkingSlotDao.get(slotId);
        model.put("parkingSlot", parkingSlot);

        return "ParkingSlotForm";
    }


}

ParkingSlotForm.jsp

<form:form action= "${root}parkingSlot/save"   modelAttribute="parkingSlot" method="post" >

            <tr>
                <td>Status:</td>
                <td>
                    <form:select path="status" >
                        <form:options items="${status}"    />
                    </form:select>
                    </td>
            </tr>

I am unable to see status dropdownlist here. I just see empty dropdownlist without populating items. If I move that method @ModelAttribute("status") into ParkingSlotController, then it works fine. But that is not the one I wanted. But my idea is to keep all the common dropdownlists in one centralized Controller. How do I do that?

Upvotes: 2

Views: 185

Answers (1)

Si mo
Si mo

Reputation: 989

Write a controlleradvice and add there the methods with modelattributes.

@ControllerAdvice is a specialization of a Component that is used to define @ExceptionHandler, @InitBinder, and @ModelAttribute methods that apply to all @RequestMapping methods.

Upvotes: 1

Related Questions