小编Pet*_*ter的帖子

Apache Tiles-无法在Spring MVC中的自定义ViewPreparer中访问bean

我正在使用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时注入变量来解决此问题,但是我很好奇其他人如何解决了这个问题。

java spring spring-mvc javabeans apache-tiles

5
推荐指数
1
解决办法
1116
查看次数

标签 统计

apache-tiles ×1

java ×1

javabeans ×1

spring ×1

spring-mvc ×1