In a Servlet
, you can include an @Override service
method which gets called before the doGet
or doPost
, is there a way to achieve the same in a Spring @Controller
?
Or more precisely, in each method in the Controller, I need to make sure an Entity (in this case, a Product) exists and redirect otherwise, like so, so how would one achieve that in Spring? Note that I also need the Product available in each Method.
@Controller
@RequestMapping("/product/{prod_id}/attribute")
public class AttributeController {
@Autowired
private AttributeService attributeService;
@RequestMapping(value = "/add", method = RequestMethod.GET)
public String add(Model model, @PathVariable Long prod_id) {
Product product = attributeService.getProduct(prod_id);
if (product == null) {
return "products/products";
}
model.addAttribute("product", product);
model.addAttribute("attribute", new Attribute());
return "products/attribute_add";
}
@RequestMapping(value = "/add", method = RequestMethod.POST)
public String save(Model model, @PathVariable Long prod_id, @Valid Attribute attribute, BindingResult result) {
Product product = attributeService.getProduct(prod_id);
if (product == null) {
return "products/products";
}
// ...
}
// ...
}