在千锋“逆战”学习第17天
SpringMVC运行流程:
SpringMVC基础配置:
1.web.xml:
dispatcherServlet
/
2.controller层方法返回值:
返回值类型有三种:ModelAndView、字符串、无返回值
a)返回ModelAndView
要求方法中创建ModelAndView对象,可以调用addObject方法用于向页面传值,调用setViewName方法用于设置跳转页面路径。
mv.addObject("uname",uname);
mv.setViewName("/success.jsp");
b)返回字符串,直接返回要跳转的页面url
return "/success.jsp";
返回字符串时数据传递:
利用HttpServletRequest对象:
@RequestMapping("lg2")
public String login2(String uname, String upwd, HttpServletRequest request){
//方法的参数用于接收数据
System.out.println("uname:" + uname);
System.out.println("upwd:" + upwd);
System.out.println("hello springmvc");
request.setAttribute("uname",uname);
return "/success.jsp";}
利用Model对象:
@RequestMapping("lg3")
public String login3(String uname, String upwd, Model model){
//方法的参数用于接收数据
System.out.println("uname:" + uname);
System.out.println("upwd:" + upwd);
System.out.println("hello springmvc");
model.addAttribute("uname",uname);
return "/success.jsp";}
利用session传值:
@RequestMapping("lg4")
public String login4(String uname, String upwd, HttpSession session){
//方法的参数用于接收数据
System.out.println("uname:" + uname);
System.out.println("upwd:" + upwd);
System.out.println("hello springmvc");
session.setAttribute("uname",uname);
return "/success.jsp";}
c)无返回值
无返回值时,使用servlet技术完成页面跳转。
@RequestMapping("lg5")
public void login5(String uname, String upwd, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//方法的参数用于接收数据
System.out.println("uname:" + uname);
System.out.println("upwd:" + upwd);
System.out.println("hello springmvc");
request.setAttribute("uname",uname);
request.getRequestDispatcher("success.jsp").forward(request,response);
}
每个人生阶段都需要努力去扮好自己的角色,越努力越轻松,越坚强越幸运!
加油!
点赞
收藏
分享
文章举报
空空如也DBC
发布了4 篇原创文章 · 获赞 0 · 访问量 69
私信
关注