我正在使用带有 Apache Tiles 的 Spring 3.2。我使用 Roo 生成了很多服务类。我正在尝试一个简单的过程,将变量注入到 jsp 模板中。这部分工作正常,但我被困在需要引用服务 bean 的地方。
@Component
public class CustomViewPreparer implements ViewPreparer {
@Autowired
UserProfileService ups;
@Override
public void execute(TilesRequestContext tilesContext,
                    AttributeContext attributeContext) {
       Authentication a = SecurityContextHolder.getContext().getAuthentication();
       String name = a.getName(); //get logged in username
       UserProfile up = ups.findByUsername(name);
       //request.setAttribute("isLoggedIn", up!=null);
    }
}
UserProfileService“ups”始终为空。我发现了这个:http: //forum.springsource.org/showthread.php?48950-ViewPreparer-is-triggered-before-Session-starts
但我不明白回应。我可以通过每次返回视图时注入我的变量来解决这个问题,但我很好奇其他人是如何解决这个问题的。