ApplicationObjectSupport实例[org.springframework.web.servlet.view.InternalResourceViewResolver@6d5f2797]不在ApplicationContext中运行

时间:2017-06-21 16:46:04

标签: spring spring-mvc servlets

我想实现不同的视图分辨率策略,所以我这样做

public class MultiViewResolver implements ViewResolver {
    private Map<String,ViewResolver> resolverMap = new HashMap<>();

public void setResolverMap(Map<String, ViewResolver> resolverMap) {
    this.resolverMap = resolverMap;
}

@Override
public View resolveViewName(String viewName, Locale locale) throws Exception {
    try {
        String[] viewNameAndSuffix = viewName.split("\\.");
        ViewResolver resolver = resolverMap.get(viewNameAndSuffix[1]);
        return resolver.resolveViewName(viewNameAndSuffix[0], locale);
    }catch (Exception e) {
        System.out.println(e.getMessage());
        return null;
    }
}
}

@Configuration
@EnableWebMvc
@ComponentScan(basePackageClasses = ControllerMarkInterface.class)
public class ServletConfig extends WebMvcConfigurerAdapter{
    @Bean
    public ViewResolver viewResolver(){
        MultiViewResolver multiViewResolver = new MultiViewResolver();
        InternalResourceViewResolver jspViewResolver = new InternalResourceViewResolver();
        jspViewResolver.setPrefix("/WEB-INF/JSP/");
        jspViewResolver.setSuffix(".jsp");
        jspViewResolver.setExposeContextBeansAsAttributes(true);
        InternalResourceViewResolver htmlViewResolver = new InternalResourceViewResolver();
        htmlViewResolver.setPrefix("/WEB-INF/HTML/");
        htmlViewResolver.setSuffix(".html");

        Map<String,ViewResolver> resolverMap = new HashMap<>();
        resolverMap.put("jsp", jspViewResolver);
        resolverMap.put("html", htmlViewResolver);
        multiViewResolver.setResolverMap(resolverMap);
        return multiViewResolver;
    }

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer){
        configurer.enable();
    }
}

@Controller
@RequestMapping("/home")
public class HomeController {
    @RequestMapping(value = "/profile", method = RequestMethod.GET)
    public String toProfile(){
        return "profile.jsp";
    }
}

当我这样做时,控制台输出“ApplicationObjectSupport instance [org.springframework.web.servlet.view.InternalResourceViewResolver@39d8e214] does not run in an ApplicationContext

然后,我使用.xml配置viewResolver,它工作得很好,为什么?为什么? 如果我想用java配置viewResolver,我该怎么办?

<bean id="viewResolver" class="com.demo.config.MultiViewResolver">
     <property name="resolverMap">
         <map>
             <entry key="jsp">
                 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                     <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
                     <property name="contentType" value="text/html" />
                     <property name="prefix" value="/WEB-INF/JSP/" />
                     <property name="suffix" value=".jsp" />
                 </bean>
             </entry>

         <entry key="html">
             <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
                <property name="contentType" value="text/html" />
                <property name="prefix" value="/WEB-INF/HTML/" />
                <property name="suffix" value=".html" />
             </bean>
         </entry>
     </map>
 </property>

1 个答案:

答案 0 :(得分:0)

问题似乎是因为InternalResourceViewResolver jspViewResolver = new InternalResourceViewResolver();InternalResourceViewResolver htmlViewResolver = new InternalResourceViewResolver();

由于InternalResourceViewResolver由spring管理,您可以直接在ServletConfig类中自动装配它,如下所示。 Spring将注入实例。

@Configuration
@EnableWebMvc
@ComponentScan(basePackageClasses = ControllerMarkInterface.class)
public class ServletConfig extends WebMvcConfigurerAdapter{

    @Autowired
    private InternalResourceViewResolver jspViewResolver;

    @Autowired
    private InternalResourceViewResolver htmlViewResolver;

    @Bean
    public ViewResolver viewResolver(){
        MultiViewResolver multiViewResolver = new MultiViewResolver();
        jspViewResolver.setPrefix("/WEB-INF/JSP/");
        jspViewResolver.setSuffix(".jsp");
        jspViewResolver.setExposeContextBeansAsAttributes(true);
        htmlViewResolver.setPrefix("/WEB-INF/HTML/");
        htmlViewResolver.setSuffix(".html");

        Map<String,ViewResolver> resolverMap = new HashMap<>();
        resolverMap.put("jsp", jspViewResolver);
        resolverMap.put("html", htmlViewResolver);
        multiViewResolver.setResolverMap(resolverMap);
        return multiViewResolver;
    }

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer){
        configurer.enable();
    }
}

希望这有帮助!