Controller方法的返回值有三种
1.返回ModeAndView 注意:此时形参可以为空,
2.返回String
(1)返回逻辑视图名 如:“items/editItem” 真正的视图(JSP路径)= 前缀+逻辑视图名+后缀(通过设置视图解析器)
(2)redirect重定向 如:“redirect:queryItem.action”
(3)forward页面转发 如:"forward:queryItem.action"
redirect 与 forward 配置的是action,不是jsp路径,若在同一个类中不需要配置全路径
3.返回void 注意:此时需要设置HttpServletRequest request, HttpServletResponse response 两个形参,此时类似于原始的Servlet 开发。
1.返回ModeAndView 注意:此时形参可以为空,
示例:
//形参为空, 返回值为ModeAndView的Controller
@RequestMapping("/queryItems")
public ModelAndView queryItems() throws Exception {
ItemsEx itemsEx = new ItemsEx();
ItemsExVo itemsExVo = new ItemsExVo();
itemsExVo.setItemsEx(itemsEx);
List<ItemsEx> itemsList = itemsService.findItemsExList(itemsExVo);
// 新建ModelAndView
ModelAndView modelAndView = new ModelAndView();
// 相当于request 的 setAttribute, 在 jsp 页面中通过 itemList 取数据
modelAndView.addObject("itemsList", itemsList);
// 指定视图
modelAndView.setViewName("items/itemsList");
return modelAndView;
}
2.返回String
(1)返回逻辑视图名 如:“items/editItem” 真正的视图(JSP路径)= 前缀+逻辑视图名+后缀(通过设置视图解析器)
// 形参为Model, 返回值为String的ModeAndView的Controller
@RequestMapping("/queryItems")
public String queryItems(Model model) throws Exception {
ItemsEx itemsEx = new ItemsEx();
// itemsEx.setName("本");
// itemsEx.setPrice(20.0f);
ItemsExVo itemsExVo = new ItemsExVo();
itemsExVo.setItemsEx(itemsEx);
List<ItemsEx> itemsList = itemsService.findItemsExList(itemsExVo);
model.addAttribute("itemsList", itemsList);
return "items/itemsList";
}
(2)redirect重定向 如:“redirect:queryItem.action”
// 返回String,利用redirect:queryItems.action,进行redirect进行跳转的形式
@RequestMapping("/editItemsSubmit")
public String editItemSubmit() throws Exception {
// 调用service更新商品信息。页面需要将商品信息传到此方法中
// ...
// 相当于HttpServletRequest中的redirect方法,地址栏的信息不会发生变化
// 如此例中变为http://localhost:8080/Web_SSM_test/items/queryItems.action
return "redirect:queryItems.action";
}
(3)forward页面转发 如:"forward:queryItem.action"
// 返回String,利用forward:queryItems.action,进行forward进行跳转的形式
@RequestMapping("/editItemsSubmit")
public String editItemSubmit() throws Exception {
// 调用service更新商品信息。页面需要将商品信息传到此方法中
// ...
// 相当于HttpServletRequest中的request方法,地址栏的信息不会发生变化
// 如此例中仍为http://localhost:8080/Web_SSM_test/items/editItemsSubmit.action
return "forward:queryItems.action";
}
redirect 与 forward 配置的是action,不是jsp路径,若在同一个类中不需要配置全路径
3.返回void
注意:此时需要设置HttpServletRequest request, HttpServletResponse response 两个形参,此时类似于原始的Servlet 开发。
(1)使用request 转向页面,如下
request.getRequestDispatcher("页面路径").forward(request,response)
(2)通过response 页面重定向
response.sendRedirect("url")
(3)也可以通过response指定响应结果,例如响应Json数据如下
response.setCharacterEncoding("utf-8");
response.setContentType("application/json;charset=utf-8");
response.getWriter().write("json串")
整体的Controller代码
package cn.itcast.ssm.controller;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import cn.itcast.ssm.po.ItemsEx;
import cn.itcast.ssm.po.ItemsExVo;
import cn.itcast.ssm.service.ItemsService;
//限制Http请求方法
//@RequestMapping(value = "/items", method = { RequestMethod.POST,RequestMethod.GET })
@Controller
@RequestMapping(value = "/items", method = { RequestMethod.POST,
RequestMethod.GET })
public class ItemsController {
@Autowired
ItemsService itemsService;
// //形参为空, 返回值为ModeAndView的Controller
// @RequestMapping("/queryItems")
// public ModelAndView queryItems() throws Exception {
//
// ItemsEx itemsEx = new ItemsEx();
// ItemsExVo itemsExVo = new ItemsExVo();
// itemsExVo.setItemsEx(itemsEx);
// List<ItemsEx> itemsList = itemsService.findItemsExList(itemsExVo);
//
// // 新建ModelAndView
// ModelAndView modelAndView = new ModelAndView();
// // 相当于request 的 setAttribute, 在 jsp 页面中通过 itemList 取数据
// modelAndView.addObject("itemsList", itemsList);
// // 指定视图
// modelAndView.setViewName("items/itemsList");
//
// return modelAndView;
// }
// 形参为Model, 返回值为String的ModeAndView的Controller
@RequestMapping("/queryItems")
public String queryItems(Model model) throws Exception {
ItemsEx itemsEx = new ItemsEx();
// itemsEx.setName("本");
// itemsEx.setPrice(20.0f);
ItemsExVo itemsExVo = new ItemsExVo();
itemsExVo.setItemsEx(itemsEx);
List<ItemsEx> itemsList = itemsService.findItemsExList(itemsExVo);
model.addAttribute("itemsList", itemsList);
return "items/itemsList";
}
@RequestMapping("/editItems")
public ModelAndView editItems(HttpServletRequest request) throws Exception {
Integer id = Integer.parseInt(request.getParameter("id"));
System.out.println(id);
ItemsEx itemsEx = itemsService.findItemsById(id);
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("itemsEx", itemsEx);
// 相当于froward(response,request)
modelAndView.setViewName("items/editItems");
return modelAndView;
}
// //返回ModeAndView的Controller
// @RequestMapping("/editItemsSubmit")
// public ModelAndView editItemSubmit(HttpServletRequest request)
// throws Exception {
//
// // 得到form表单的数据
// System.out.println(request.getParameter("name"));
// System.out.println(request.getParameter("price"));
// System.out.println(request.getParameter("detail"));
//
// // 调用service更新商品信息。页面需要将商品信息传到此方法中
// // ...
// ModelAndView modelAndView = new ModelAndView();
// // 若修改成功,返回成功的页面
// modelAndView.setViewName("success");
//
// return modelAndView;
// }
// // 返回String,利用forward:queryItems.action,进行forward进行跳转的形式
// @RequestMapping("/editItemsSubmit")
// public String editItemSubmit() throws Exception {
// // 调用service更新商品信息。页面需要将商品信息传到此方法中
// // ...
//
// // 相当于HttpServletRequest中的request方法,地址栏的信息不会发生变化
// // 如此例中仍为http://localhost:8080/Web_SSM_test/items/editItemsSubmit.action
// return "forward:queryItems.action";
// }
// 返回String,利用redirect:queryItems.action,进行redirect进行跳转的形式
@RequestMapping("/editItemsSubmit")
public String editItemSubmit() throws Exception {
// 调用service更新商品信息。页面需要将商品信息传到此方法中
// ...
// 相当于HttpServletRequest中的redirect方法,地址栏的信息不会发生变化
// 如此例中变为http://localhost:8080/Web_SSM_test/items/queryItems.action
return "redirect:queryItems.action";
}
}