Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring中@ModelAttribute注释的替代方案_Java_Spring_Spring Mvc - Fatal编程技术网

Java Spring中@ModelAttribute注释的替代方案

Java Spring中@ModelAttribute注释的替代方案,java,spring,spring-mvc,Java,Spring,Spring Mvc,像在Spring中一样,我们有httpServeletRequest.getParameter(“key”)作为@RequestParam(“key”)的替代方案, 除了@modeldattribute(“key”),我们还有其他选择吗 model.addAttribute(“学生”,new student()); ... ... @请求映射(“/processStudentForm”) 公共字符串处理格式( @模型属性(“学生”)学生 ) { ... } 我们有类似的吗 公共

像在
Spring
中一样,我们有
httpServeletRequest.getParameter(“key”)
作为
@RequestParam(“key”)
的替代方案, 除了
@modeldattribute(“key”)
,我们还有其他选择吗

model.addAttribute(“学生”,new student());
...
...
@请求映射(“/processStudentForm”)
公共字符串处理格式(
@模型属性(“学生”)学生
) 
{       
...
}
我们有类似的吗

公共字符串处理表单(
模型
) 
{       
学生=model.getAtribute(“键”);
...
}
更新1

@RequestMapping(“/showStudentForm”)
公共字符串显示格式(模型){
model.addAttribute(“key”,new Student());
返回“学生表格”;
}

... 

我正在将表单值绑定到student对象中。如何通过请求对象访问此模型属性???

不能通过请求对象直接访问学生模型对象,但可以通过与请求对象不同的方式访问此对象 默认情况下,spring在幕后做了大量工作,它使用FormHttpMessageConverter将其转换为应用程序上的模型映射/x-www-form-urlencoded请求您可以参考文档

你可以像spring内部做的那样做。我尝试了这个方法,但你可以用多种方式来做。其他答案请参阅堆栈溢出帖子

       Map map = httpServletRequest.getParameterMap().entrySet()
            .stream().collect(Collectors.toMap(e -> e.getKey(), e -> Arrays.stream(e.getValue()).findFirst().get()));
       Student student = new ObjectMapper().convertValue(map, Student.class);


但是使用默认实现总是一个很好的做法。

先生,请您参考我添加的更新。spring boot中有@RequestBody注释