Spring MVC之ModelAndView分析
来源:互联网 发布:网络歌手思念是一把刀 编辑:程序博客网 时间:2024/05/22 14:33
前台表单
<form class="form-horizontal" role="form" action="user/login" method="post">账号:<input type="text" class="form-control" name="username" id="username">密码:<input type="password" class="form-control" name="userpwd" id="userpwd"><button type="submit" class="btn btn-default"> 登录</button></form>
controller
package com.music.controller;import java.util.HashMap;import java.util.Map;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.servlet.ModelAndView;import com.music.model.User;import com.music.service.UserService;@Controller@RequestMapping("/user")public class UserController { @Autowired private User user; @Autowired private UserService userService; @RequestMapping("/login") public ModelAndView login(String username,String userpwd,ModelAndView modelAndView) { Map<String,Object> map=new HashMap<>(); map.put("username", username); map.put("userpwd", userpwd); User user=userService.selectByNameAndUserpwd(map); if (user!=null) { modelAndView.setViewName("success");字符串方式//视图在web-inf下面为success.jsp文件 modelAndView.addAllObjects(map); return modelAndView ; } return modelAndView;}}
modelAndView.setViewName("/user/success");路径方式//视图在web-inf下的user文件夹下的success.jsp文件
mv.setViewName("redirect:/user/success.jsp");重定向方式//视图在web-inf下的user文件夹下的success.jsp
以上三种modelAndView.setViewName,都可以返回视图
如果想用ModelAndView返回,就public ModelAndView login(){}这样写方法
modelAndView.setViewName("success");跟return “success”一样,由视图解析跳转到你配置的页面,一般是 /success.jsp 这个页面
第二种
controller中的写法
package com.music.controller;import java.util.HashMap;import java.util.Map;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.servlet.ModelAndView;import com.music.model.User;import com.music.service.UserService;@Controller@RequestMapping("/user")public class UserController { @Autowired private User user; @Autowired private UserService userService; @RequestMapping("/login") public String login(String username,String userpwd) { Map<String,Object> map=new HashMap<>(); map.put("username", username); map.put("userpwd", userpwd); User user=userService.selectByNameAndUserpwd(map); ModelAndView mv =new ModelAndView(); if (user!=null) { mv.setViewName("success"); return mv.getViewName() ; } return mv.getViewName();}}
主要针对新手上手代码。
阅读全文
1 0
- Spring MVC之ModelAndView分析
- spring MVC之构造ModelAndView对象
- spring MVC 之构造ModelAndView对象
- spring MVC 之构造ModelAndView对象
- spring MVC之构造ModelAndView对象
- spring MVC之构造ModelAndView对象
- spring MVC之构造ModelAndView对象
- spring MVC之构造ModelAndView对象
- spring MVC之构造ModelAndView对象
- Spring MVC ModelAndView
- spring mvc ModelAndView
- Spring MVC ModelAndView
- Spring MVC ModelAndView 简述
- Spring MVC ModelAndView
- Spring MVC ModelAndView详解
- Spring MVC ---ModelAndView
- spring mvc ModelAndView向前台传值
- spring mvc ModelAndView 404 错误
- fsafds
- Java 基础 —— 注解
- js 事件对象
- VBA学习3_教你快速入门Excel-宏与VBA(续)
- Netty开发中间件:高并发性能优化
- Spring MVC之ModelAndView分析
- [LintCode]68.二叉树的后序遍历
- ubuntu 16 svn搭建 阿里云实测OK
- BSON 与 JSON 的区别
- 第一个Spring项目
- 开发Android逆向工具
- 挖财笔试:求数列的和
- (转)挖矿的数学题
- ubuntu下安装virtualbox虚拟机中win7识别U盘