SSM框架之ControllerToView三种传值方式
程序员文章站
2022-05-29 19:41:33
...
SSM 框架学习 (Spring MVC篇)
前面已经介绍了如何搭建一个简单的Spring MVC框架,下面就来看看这三个模块Model-View-Controller
之间是怎么实现数据交互的吧。
先我将介绍三种Controller->View
数据传输方式,只介绍主要代码,不介绍整个项目搭建过程,如不懂Spring MVC是如何搭建的请参考上一篇文章SSM框架之Spring MVC的搭建。
1、Map方式
新建一个Controller类:MapController 其内容如下:
package com.lsl.ssm.controller;
import java.util.Map;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class MapController {
/**
* 映射路径:/map
*
* 这里username是从View传到Controller,可写可不写。
*/
@RequestMapping("/map")
public String index(String username, Map<String, Object> map) {
System.out.println("Hello Spring MVC map " + username);
map.put("name", username + "六的一批");
return "index";
}
}
前端接收很简单,新建一个index.jsp
,这里index
命名要对应上面return "index"
,这样控制器才能找到页面.
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Controller向View传值</title>
</head>
<body>
<h1>hello SpringMVC!!!</h1>
<h1>Map传参</h1>
<h2>username(key:name)-->${name }</h2>
</body>
</html>
效果如下: 注意:pageEncoding
设为UTF-8
否则中文乱码
2、ModelAndView方式
新建控制器ModelAndViewController
package com.lsl.ssm.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class ModelAndViewController {
@RequestMapping("/mav")
public ModelAndView index(String username) {
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("username", username);
modelAndView.setViewName("index1");
return modelAndView;
}
}
新建一个index1.jsp
文件,与上面setViewName("index1")
中index1
对应
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Controller向View传值</title>
</head>
<body>
<h1>hello SpringMVC!!!</h1>
<h1>ModelAndView传参</h1>
<h2>username(key:username)-->${username }</h2>
</body>
</html>
部署运行:
3、Model方式
这回我们要传个对象到前端,所以我们要新建一个实体类User
如下:
package com.lsl.ssm.pojo;
public class User {
private String userName; // 用户名称
private String userPassword; // 用户密码
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPassword() {
return userPassword;
}
public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}
}
新建控制器ModelController
package com.lsl.ssm.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.ui.Model;
import com.lsl.ssm.pojo.User;
@Controller
public class ModelController {
/**
* 入参为Model类型 推荐使用
* @param username
* @param model
* @return
*/
@RequestMapping("/model")
public String index(String username, Model model) {
// 这里key为"username",value为username里的值,前端通过"username"找到这个值
model.addAttribute("username", username);
// 默认使用对象的类型作为key
// model.addAttribute("string", username);
// model.addAttribute("user", new User());
model.addAttribute(username); // 等价于("string", username)
User user = new User();
user.setUserPassword("admin");
user.setUserName(username + "咯咯咯咯");
model.addAttribute("currentUser", user);
model.addAttribute(user); // 等价于("user", user)
return "index2";
}
}
同理,新建一个index2.jsp
文件
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Controller向View传值</title>
</head>
<body>
<h1>hello SpringMVC!!!</h1>
<h1>Model传参</h1>
<h2>username(key:string)-->(value:${string })</h2>
<h2>username(key:username)-->(value:${username })</h2>
<h2>username(key:currentUser)-->(value:${currentUser.userName })</h2>
<h2>username(key:currentUser)-->(value:${currentUser.userPassword })</h2>
<h2>username(key:user)-->(value:${user.userName })</h2>
</body>
</html>
注意:Model方式中,若不指定key值,则默认使用该对象的类型作为key
部署运行:
@Author 瞌睡虫
@mybatis-3.2.2
@Database: mysql 5.7.15
@Tool: MyEclipse