SpingMVC和hibernate:jsp中未声明的变量在保存时变为null



我刚刚使用Spring框架和hibernate创建了我的简单web应用程序。但是,当我的数据库发生事务时,我会面临关键问题。当我尝试更新我的对象时,未声明的变量在保存到数据库时变为null。

我将举例说明。

.java型号:

public class Model implements Serializable {
private int id;
private String firstName;
private String lastName;
private String description;
//setter getter
}

ModelDaoImpl.java:

@Repository("modelDao")
public class ModelDaoImpl implements ModelDao {
@Autowired
SessionFactory sessionFactory;
public Model create (Model model) throws Exception {
this.sessionFactory.getCurrentSession().save(model);
return model;
}
public Model update (Model model) throws Exception {
this.sessionFactory.getCurrentSession().update(model);
return model;
}
public Model get (Serializable id) throws Exception {
return (Model) this.sessionFactory.getCurrentSession().get(Model.class, id);
}
}

ModelServiceImpl.java:

@Service("modelService")
public class ModelServiceImpl implements ModelService {
@Autowired
modelDao modelDao;
@Transactional
public Model create (Model model) throws Exception {
return modelDao.create(model);
}
@Transactional
public Model update (Model model) throws Exception {
return modelDao.udpdate(model);
}
@Transactional
public Model get (Serializable id) throws Exception {
return modelDao.get(id);
}

}

ModelController.java:

@Controller
public class ModelController {
@Autowired
ModelService modelService
@RequestMapping(value="/editModel", method.RequestMethod.GET)
public String formCreator(ModelMap model, HttpServletRequest request) {
Integer id = new Integer(request.getParameter("id"));
Model modelData = new Model();
if (id != null) {
modelData = modelService.get(id);
}
model.addAttribute("modelData", modelData);
return "editModel"
}
@RequestMapping(value="/saveModel", method.RequestMethod.POST)
public String saveData(@ModelAttribute("modelData") Model modelData) {
if (modelData.getId() == null) {
modelService.create(modelData);
} else {
modelService.update(modelData);
}
return "redirect:/modelList";
}
//SKIP FOR GET MODEL LIST AND GET MODEL DETAIL
}

editModel.jsp:

<form:form action="${pageContext.request.contextPath}/saveModel" method="POST" modelAttribute="modelData">
<table>
<form:hidden path="id"/>
<tr>
<td>First Name:</td> 
<td><form:input path="firstName"/></td> 
</tr>
<tr>
<td>Last Name:</td>
<td><form:input path="lastName"/></td> 
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="Save"></td>
</tr>
</table>
</form:form>

从jsp页面中,我们看到我没有声明变量"description",但当更新现有数据时,变量"description"变为null。我尝试过使用@DynamicUpdate或与动态更新过程相关的,但结果仍然相同,变量"description"仍然变为null。

有什么建议吗?

问题在于控制器的编写方式:创建了一个新的Model实例并在执行saveData时填充。

您可以将代码更新为如下所示。现在POST模型实例将是从getModel((返回的,而不是一个新实例,并且只有将更新传入请求中指定的值。我们还可以更新GET以使用相同的代码来填充模型。

本质上:

GET上的
  • -将通过调用@ModelAttribute为UI填充模型
  • POST时-将填充模型,返回的model实例将传递给saveData((

参见:

https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc-ann modelattrib方法args

@Controller
public class ModelController {
@Autowired
ModelService modelService
@RequestMapping(value="/editModel", method.RequestMethod.GET)
public String formCreator(HttpServletRequest request) {
return "editModel"
}
@RequestMapping(value="/saveModel", method.RequestMethod.POST)
public String saveData(@ModelAttribute("modelData") Model model) {
//you can have 1 method in your service and DAO 
//DAO can use saveOrUpdate() method of session;
modelService.persist(model);
return "redirect:/modelList";
}
@ModelAttribute("modelData")
public Model getModel(@RequestParam(name = "id", required = false)Integer id){
return id != null ? modelService.get(id) : new Model();
}
}

最新更新