so i have this controller as
package org.aman.controller;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.aman.model.Brands;
import org.aman.service.BrandsService;
@Controller
public class BrandsController {
@Autowired
private BrandsService brandsService;
@RequestMapping("/index")
public String setupForm(Map<String, Object> map) {
Brands brands = new Brands();
map.put("brands", brands);
map.put("brandsList", brandsService.getAllBrands());
return "brands";
}
@RequestMapping("/inde")
public String setupFor(Map<String, Object> map) {
Brands brands = new Brands();
map.put("brands", brands);
map.put("brandsSelectedList", brandsService.getAllBrands());
return "query";
}
@RequestMapping(value = "/brands.do", method = RequestMethod.POST)
public String doActions(@ModelAttribute Brands brands, BindingResult result, @RequestParam String action,
Map<String, Object> map) {
Brands brandsResult = new Brands();
switch (action.toLowerCase()) {
case "add":
brandsService.add(brands);
brandsResult = brands;
break;
case "edit":
brandsService.edit(brands);
brandsResult = brands;
break;
}
map.put("brands", brandsResult);
map.put("brandsList", brandsService.getAllBrands());
return "brands";
}
}
The function setup form returns "brands" which in turn maps to brands.jsp
in the spring servlet. I want to return a JSON object of the solution instead of the whole JSP page.
How can I do that? Any kind of help will be appreciated.
Since you're adding your resultant data to a map make the return type as Map<String, Object>
and annotate with @ResposeBody
as @jb nizet commented,
@RequestMapping(value = "/brands.do", method = RequestMethod.POST)
public @ResponseBody Map<String, Object> map doActions(@ModelAttribute Brands brands,
BindingResult result, @RequestParam String action, Map<String, Object> map)
{
Brands brandsResult = new Brands();
switch (action.toLowerCase()) {
case "add":
brandsService.add(brands);
brandsResult = brands;
break;
case "edit":
brandsService.edit(brands);
brandsResult = brands;
break;
}
map.put("brands", brandsResult);
map.put("brandsList", brandsService.getAllBrands());
return map;
}
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.