制作Spring 3 MVC控制器方法Transactional [英] Making Spring 3 MVC controller method Transactional

查看:156
本文介绍了制作Spring 3 MVC控制器方法Transactional的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



然而,在一个特殊情况下,为了避免一个懒惰的初始化异常,我必须使用Spring 3.1和我的DAO和服务层(事务)一个spring mvc请求处理器方法@transactional。但它没有将交易附加到该方法。方法名称是ModelAndView home(HttpServletRequest请求,HttpServletResponse响应)。
http://forum.springsource.org/showthread .php?46814-Transaction-in-MVC-Controller
从这个链接看来,不可能将事务(默认)附加到mvc方法。在该链接中建议的解决方案似乎是为Spring 2.5(重写handleRequest)。任何帮助将得到真正的赞赏。谢谢

  @Controller 
public class AuthenticationController {
@Autowired
CategoryService categoryService;
@Autowired
BrandService brandService;
@Autowired
ItemService itemService;

@RequestMapping(value =/ login.html,method = RequestMethod.GET)
ModelAndView login(){
返回新的ModelAndView(login.jsp);
}
@RequestMapping(value =/ home.html,method = RequestMethod.GET)
@Transactional
ModelAndView home(HttpServletRequest请求,HttpServletResponse响应){
列出<类别> categories = categoryService.readAll();
request.setAttribute(categories,categories);
列表<品牌>品牌= brandService.readAll();
request.setAttribute(品牌,品牌);
列表<项目> items = itemService.readAll();
request.setAttribute(items,items);
设置<图片> images = items.get(0).getImages();
(Image i:images){
System.out.println(i.getUrl());
}
返回新的ModelAndView(home.jsp);


解决方案

您需要执行一个接口,以便Spring可以使用它作为代理接口:

  @Controller 
public interface AuthenticationController {
ModelAndView home(HttpServletRequest请求,HttpServletResponse响应);
}

@Controller
public class AuthenticationControllerImpl implements AuthenticationController {

@RequestMapping(value =/ home.html,method = RequestMethod.GET )
@Transactional
@Override
ModelAndView home(HttpServletRequest请求,HttpServletResponse响应){
.....
}
}


I am using Spring 3.1 and have my DAO and service layer(transactional) written.

However in a special case to avoid a lazy init exception I have to make a spring mvc request handler method @transactional. But It is failing to attach transaction to that method. Method name is ModelAndView home(HttpServletRequest request, HttpServletResponse response). http://forum.springsource.org/showthread.php?46814-Transaction-in-MVC-Controller From this link it seems it is not possible to attach transaction (by default ) to mvc methods. Solution suggested in that link seems to be for Spring 2.5 (overriding handleRequest ). Any help will be reallyappreciated. Thanks

@Controller
public class AuthenticationController { 
@Autowired
CategoryService categoryService;    
@Autowired
BrandService brandService;
@Autowired
ItemService itemService;

@RequestMapping(value="/login.html",method=RequestMethod.GET)
ModelAndView login(){       
    return new ModelAndView("login.jsp");       
}   
@RequestMapping(value="/home.html",method=RequestMethod.GET)
@Transactional
ModelAndView home(HttpServletRequest request, HttpServletResponse response){
    List<Category> categories = categoryService.readAll();
    request.setAttribute("categories", categories);     
    List<Brand> brands = brandService.readAll();
    request.setAttribute("brands", brands);     
    List<Item> items = itemService.readAll();
    request.setAttribute("items", items);
    Set<Image> images = items.get(0).getImages();
    for(Image i : images ) {
        System.out.println(i.getUrl());
    }
    return new ModelAndView("home.jsp");    
}

解决方案

You'll need to implement an interface so that Spring has something it can use as a Proxy interface:

@Controller
public interface AuthenticationController {
  ModelAndView home(HttpServletRequest request, HttpServletResponse response);
}

@Controller
public class AuthenticationControllerImpl implements AuthenticationController {

@RequestMapping(value="/home.html",method=RequestMethod.GET)
@Transactional
@Override
ModelAndView home(HttpServletRequest request, HttpServletResponse response){
.....
}
}

这篇关于制作Spring 3 MVC控制器方法Transactional的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆