所以我把这个控制器作为
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";
}
}
函数设置表单返回“brands”,而“brands”又映射到spring servlet中的brands.jsp
。我想返回解决方案的JSON对象而不是整个JSP页面。
我该怎么做?任何形式的帮助将不胜感激。
答案 0 :(得分:1)
由于您将结果数据添加到地图中,因此将返回类型设为Map<String, Object>
并使用@ResposeBody
注释为@jb nizet注释,
@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;
}