在我的控制器中,输入一个值MySQL
数据库成ModelAndView object
有一个单独的程序可以更新表和MVC应该获取该值,因此没有表单可以更新该表。
当。。。的时候表已更新,当我在浏览器上点击刷新,值不会在页面上更新。
控制器
@SuppressWarnings("unchecked")
@Secured({ "ROLE_USER", "ROLE_ADMIN" })
@RequestMapping(value = { "/", "/welcome" }, method = RequestMethod.GET)
public ModelAndView defaultPage(@ModelAttribute("user") User user) {
Collection<SimpleGrantedAuthority> authorities = (Collection<SimpleGrantedAuthority>) SecurityContextHolder
.getContext().getAuthentication().getAuthorities();
ModelAndView view = new ModelAndView("/hello");
// Redirects to admin page if user has admin role
if (authorities.toString().contains("ROLE_ADMIN")) {
return new ModelAndView("redirect:/admin");
}
/////
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
String userName = auth.getName();
User userInfo = userDAO.getUserInfo(userName);
System.out.println("Here's the thing " + userInfo.getLastname() + " " + userInfo.getUserDetails());
Details details = userDAO.getDetailsInfo(userInfo.getUserDetails().getLastname(),
userInfo.getUserDetails().getPostcode());
Plugs plugs = userDAO.getPlugInfo(details.getMacAddress());
String json = plugs.getJson();
JSONObject obj = new JSONObject(json); //this is the value that is not updating
String name = obj.getJSONObject("meta").getJSONObject("locate").getString("value");
System.out.println(name);
view.addObject("json", obj);
return view;
}
我知道这很受人轻视,但我把这个价值放在JavaScript。
像这样:
<c:set var="json" value="${json}"/>
var __data__ = ${json};
为什么当数据库更新时MVC不会显示正确的值?
我希望它在刷新时更新
编辑:我已禁用缓存并清除缓存仍然有问题。有什么帮助吗?
确实PersistenceContextType.EXTENDED
范围是您所确定的问题的根本原因。原因如下:
With PersistenceContextType.TRANSACTION
范围内,Spring框架负责管理注入的生命周期entitymanager
。的生活TRANSACTION
范围与底层相关联transaction
并且在提交/回滚事务时entityManager
由 spring 框架封闭。
但与PersistenceContextType.EXTENDED
范围内,Spring框架只负责注入entitymanager
。的生命周期EXTENDED
作用域实体管理器不与用户绑定transaction
并且它可以跨越多个事务。由应用程序/用户明确关闭entityManager
一旦我们完成了它。如果没有,它将永远保持打开状态(或直到 Spring 容器关闭)。
我假设在你的userDAO
你没有关闭entityManager
明确地。而且“userDAO”也可以是单例。所以同样的entityManager
仅注入一次并在多个调用(或 http 请求)中使用
有了这个entityManager' remains open forever and so when you try to access any object (User/Plugin/UserDetails) the
EntityManager`检查其一级缓存,它将检查在其中找到的一级缓存(第一次加载)并从其一级缓存(已过时)返回该对象,而不是访问数据库数据。
显然与TRANSACTION
范围entityManager
每当事务完成(或方法退出)时,Spring 框架就会关闭它。这导致创建一个entityManager
对于每个请求(在您的情况下是网络请求)都会访问包含更新数据的数据库。
看看这个链接是否有帮助。http://forum.spring.io/forum/other-spring-lated/ejb/18445-injection-of-persistencecontext-with-persistencecontexttype-extended http://forum.spring.io/forum/other-spring-related/ejb/18445-injection-of-persistencecontext-with-persistencecontexttype-extended
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)