Spring MVC传输对象属性

今天搬砖时遇到一个问题,前端使用JSP+form传输数据,后台使用Spring MVC接收,但是接收到的对象属性一直是null,找了好久才发现原因,代码如下

前端代码

后端代码

需要注意一点

User对象属性需要与表单中name属性中一致(不是id属性,之前接收到的一直为null就是由于只写了id,为预防错误最好id和name都写上且为同一值)

原文地址:https://www.cnblogs.com/lovetq520/p/11655750.html

时间: 2024-07-30 02:05:12

Spring MVC传输对象属性的相关文章

spring -mvc 将对象封装json返回时删除掉对象中的属性注解方式

spring -mvc 将对象封装json返回时删除掉对象中的属性注解方式 在类名,接口头上注解使用在 @JsonIgnoreProperties(value={"comid"}) //希望动态过滤掉的属性 例 @JsonIgnoreProperties(value={"comid"}) public interface 接口名称{ } @JsonIgnoreProperties(value={"comid"}) public class 类名{

spring mvc:日志对象logger的复用

在采用Spring mvc+org.slf4j.Logger开发项目时,发现几乎每个controller或者manager都有的一个标配: private final static Logger LOGGER = LoggerFactory.getLogger(Test.class); 看起来并没有什么问题,但是毫无疑问的是,每新建一个controller或者manager就需要写一次几乎相同的代码会间接降低效率,另外因为每个controller或者manager都有一个这样的日志对象,无疑会增

【Spring学习笔记-MVC-7】Spring MVC模型对象-模型属性讲解

作者:ssslinppp       来自为知笔记(Wiz) 附件列表 处理模型数据.png

spring mvc绑定对象String转Date解决入参不能是Date的问题

使用spring的mvc,直接将页面参数绑定到对象中,对象中有属性为Date时会报错,此时需要处理下. 同样的,其他的需要处理的类型也可以用这种方法. 在controller中加入代码 @InitBinder protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception { //对于需要转换为Date类型的属性,使用DateEditor进行处理 bin

spring mvc绑定对象String转Date(來自http://blog.csdn.net/whumr1/article/details/8056285)

使用spring的mvc,直接将页面参数绑定到对象中,对象中有属性为Date时会报错,此时需要处理下. 同样的,其他的需要处理的类型也可以用这种方法.(其他类型沒有時間研究,歡迎技術牛人補充) 1.在controller中加入代码 @InitBinderprotected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception { //对于需要转换为Date类型的属

解决Spring Mvc中对象绑定参数重名的问题

html页面 <form method='post' action='url'> 用户名 <input type='text' name='name'> 用户id <input type='text' name='id'> 食品名 <input type='text' name='name'> 食品id <input type='text' name='id'> <input type='text' name='age'> <i

Spring MVC返回对象JSON

@RestController 用于返回对象,会自动格式化为JSON @RequestMapping("/user2") public User2 user2(Model model){ model.addAttribute("content", hello + "(第二种)"); User2 user=new User2(); user.setId(1); user.setName("tomas"); user.setPwd

spring学习——注入静态对象属性

spring注入静态对象属性时,因为虚拟机类加载问题,直接在属性上使用@Autowired 是不可以的.需要在属性对应的set方法上@Autowired,并且,set方法不能定义为static. 1.创建静态对象属性对应的类 package com.bluej.springj.service.impl; import org.springframework.stereotype.Service; import com.bluej.springj.service.LogService; @Serv

spring学习(一)—— spring mvc

目的 了解Spring mvc 学习对象 https://github.com/spring-projects/spring-mvc-showcase 学习收获 1. 自定义注解 // 以下是注解接口Target(value={ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface MaskFor