这是在没有RestFul方式获取的
我们的控制器处理方法,
只需要参数列表的参数标识跟请求的参数标识一致的即可
如果不一致,我们可以对参数列表的参数注解上@RequestParam
写上和请求参数一样的标识即可
如果要处理方法的参数以一个封装的实体类实例作为接受的容器,也是可以的
只要请求的参数标识符和,实体类的字段标识符是11对应的,就可以被接收到
例如我的这个POST表单,就像正常方式一样提交,也不会把参数暴露在URL中
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form action="http://localhost:8080/SpringMVC02/getData" method="post"> <p>用户 <input type="text" name="user_name"></p> <p>密码 <input type="password" name="user_password"></p> <p><input type="submit"></p> </form> </body> </html>
然后这是我们的控制器方法
package cn.dai.controller; import cn.dai.pojo.User; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PostMapping; /** * @author ArkD42 * @file SpringMVC * @create 2020 - 05 - 07 - 13:55 */ @Controller public class FormController { @PostMapping("/getData") public void getFormData(User user){ System.out.println(user); } }
访问测试之后,被模板引擎跳转拼接了
这没有关系,我们的数据确实让处理的方法接收到了
- Model
- ModelMap
- ModelAndView
Model类源码,功能不多,一看是一个接口
实际对象应该是好几个实现类来完成
主要是存储数据功能,和原先的Request差不多
// // Source code recreated from a .class file by IntelliJ IDEA // (powered by Fernflower decompiler) // package org.springframework.ui; import java.util.Collection; import java.util.Map; import org.springframework.lang.Nullable; public interface Model { Model addAttribute(String var1, @Nullable Object var2); Model addAttribute(Object var1); Model addAllAttributes(Collection<?> var1); Model addAllAttributes(Map<String, ?> var1); Model mergeAttributes(Map<String, ?> var1); boolean containsAttribute(String var1); Map<String, Object> asMap(); }
ModelMap,继承LinkeHashMap
也就是具有该父类全部方法,功能很多
// // Source code recreated from a .class file by IntelliJ IDEA // (powered by Fernflower decompiler) // package org.springframework.ui; import java.util.Collection; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.Map; import org.springframework.core.Conventions; import org.springframework.lang.Nullable; import org.springframework.util.Assert; public class ModelMap extends LinkedHashMap<String, Object> { public ModelMap() { } public ModelMap(String attributeName, @Nullable Object attributeValue) { this.addAttribute(attributeName, attributeValue); } public ModelMap(Object attributeValue) { this.addAttribute(attributeValue); } public ModelMap addAttribute(String attributeName, @Nullable Object attributeValue) { Assert.notNull(attributeName, "Model attribute name must not be null"); this.put(attributeName, attributeValue); return this; } public ModelMap addAttribute(Object attributeValue) { Assert.notNull(attributeValue, "Model object must not be null"); return attributeValue instanceof Collection && ((Collection)attributeValue).isEmpty() ? this : this.addAttribute(Conventions.getVariableName(attributeValue), attributeValue); } public ModelMap addAllAttributes(@Nullable Collection<?> attributeValues) { if (attributeValues != null) { Iterator var2 = attributeValues.iterator(); while(var2.hasNext()) { Object attributeValue = var2.next(); this.addAttribute(attributeValue); } } return this; } public ModelMap addAllAttributes(@Nullable Map<String, ?> attributes) { if (attributes != null) { this.putAll(attributes); } return this; } public ModelMap mergeAttributes(@Nullable Map<String, ?> attributes) { if (attributes != null) { attributes.forEach((key, value) -> { if (!this.containsKey(key)) { this.put(key, value); } }); } return this; } public boolean containsAttribute(String attributeName) { return this.containsKey(attributeName); } }
ModelAndView
携带和转发视图,附加了集合类的功能
// // Source code recreated from a .class file by IntelliJ IDEA // (powered by Fernflower decompiler) // package org.springframework.web.servlet; import java.util.Map; import org.springframework.http.HttpStatus; import org.springframework.lang.Nullable; import org.springframework.ui.ModelMap; import org.springframework.util.CollectionUtils; public class ModelAndView { @Nullable private Object view; @Nullable private ModelMap model; @Nullable private HttpStatus status; private boolean cleared = false; public ModelAndView() { } public ModelAndView(String viewName) { this.view = viewName; } public ModelAndView(View view) { this.view = view; } public ModelAndView(String viewName, @Nullable Map<String, ?> model) { this.view = viewName; if (model != null) { this.getModelMap().addAllAttributes(model); } } public ModelAndView(View view, @Nullable Map<String, ?> model) { this.view = view; if (model != null) { this.getModelMap().addAllAttributes(model); } } public ModelAndView(String viewName, HttpStatus status) { this.view = viewName; this.status = status; } public ModelAndView(@Nullable String viewName, @Nullable Map<String, ?> model, @Nullable HttpStatus status) { this.view = viewName; if (model != null) { this.getModelMap().addAllAttributes(model); } this.status = status; } public ModelAndView(String viewName, String modelName, Object modelObject) { this.view = viewName; this.addObject(modelName, modelObject); } public ModelAndView(View view, String modelName, Object modelObject) { this.view = view; this.addObject(modelName, modelObject); } public void setViewName(@Nullable String viewName) { this.view = viewName; } @Nullable public String getViewName() { return this.view instanceof String ? (String)this.view : null; } public void setView(@Nullable View view) { this.view = view; } @Nullable public View getView() { return this.view instanceof View ? (View)this.view : null; } public boolean hasView() { return this.view != null; } public boolean isReference() { return this.view instanceof String; } @Nullable protected Map<String, Object> getModelInternal() { return this.model; } public ModelMap getModelMap() { if (this.model == null) { this.model = new ModelMap(); } return this.model; } public Map<String, Object> getModel() { return this.getModelMap(); } public void setStatus(@Nullable HttpStatus status) { this.status = status; } @Nullable public HttpStatus getStatus() { return this.status; } public ModelAndView addObject(String attributeName, @Nullable Object attributeValue) { this.getModelMap().addAttribute(attributeName, attributeValue); return this; } public ModelAndView addObject(Object attributeValue) { this.getModelMap().addAttribute(attributeValue); return this; } public ModelAndView addAllObjects(@Nullable Map<String, ?> modelMap) { this.getModelMap().addAllAttributes(modelMap); return this; } public void clear() { this.view = null; this.model = null; this.cleared = true; } public boolean isEmpty() { return this.view == null && CollectionUtils.isEmpty(this.model); } public boolean wasCleared() { return this.cleared && this.isEmpty(); } public String toString() { return "ModelAndView [view=" + this.formatView() + "; model=" + this.model + "]"; } private String formatView() { return this.isReference() ? "\"" + this.view + "\"" : "[" + this.view + "]"; } }
原文:https://www.cnblogs.com/mindzone/p/12842827.html