我正在使用Spring 3.2和Apache Tiles。我使用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);
}
}
Run Code Online (Sandbox Code Playgroud)
UserProfileService的“ ups”始终为null。我发现了这一点:http : //forum.springsource.org/showthread.php?48950-ViewPreparer-is-triggered-before-Session-starts
但我不明白回应。我可以通过在每次返回View时注入变量来解决此问题,但是我很好奇其他人如何解决了这个问题。