springmvc form表单提交后台对象获取不到值(解决)
使用springmvc进行表单提交时,在controller action层实体对象获取不到值。
在struts中使用user.username方式传递参数,在spring中不需要对象点直接使用对象属性名称
解决获取不了表单值的方法,需要在action控制层 提供实体类对象的get set方法。
下面详细说明:
jsp页面写法
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %> <div> <form action="${pageContext.request.contextPath}/user/insert" method="POST"> <!-- 直接使用属性名不需要.对象 --> name:<input id="" name="name" value=""><br> password:<input id="" name="password" type="password" value=""><br> <input type="submit"> </form> </div>
controller类写法
@Controller public class UserController { private static final Log log = LogFactory.getLog(UserController.class); private User user = new User(); private UserService userService; @RequestMapping(value = "/user/insert" ,method = RequestMethod.POST) public String insertRoute(@ModelAttribute("user") User user,ModelMap model) throws Exception{ //需要提供user的get set 方法,user对象里面才有值 try { userService.create(user); } catch (NoRowsAffectedException e) { log.error("failed", e); throw e; } return "userList"; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } public UserService getUserService() { return userService; } @Resource public void setUserService(UserService userService) { this.userService = userService; } }
来源://作者:admin/更新时间:2013-12-11
顶
踩
相关文章: