ViewResolver使用Java注释

时间:2013-01-20 17:05:58

标签: java spring model-view-controller spring-mvc annotations

是否可以在Spring 3.1.1中使用Java注释配置视图解析器?

我完成了使用Java注释的所有配置,但我被困在视图解析器

代码

package com;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import javax.sql.DataSource;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import com.*;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.UrlBasedViewResolver;
import org.springframework.web.servlet.view.JstlView;

@Configuration
@ComponentScan("com")
public class AppConfig
{
    {
          //Other bean declarations
    }

    @Bean
    public UrlBasedViewResolver urlBasedViewResolver()
    {
        UrlBasedViewResolver res = new InternalResourceViewResolver();
        res.setViewClass(JstlView.class);
        res.setPrefix("/WEB-INF/");
        res.setSuffix(".jsp");

        return res;
    }
}

我使用了这段代码并运行了应用程序,但它没有返回适当的视图。但是,如果我在app-servlet.xml文件中配置 viewresolver ,则可以正常使用。

3 个答案:

答案 0 :(得分:33)

您的类应该扩展WebMvcConfigurerAdapter类。请看下面的例子

@Configuration
@ComponentScan(basePackages="com")
@EnableWebMvc
public class MvcConfiguration extends WebMvcConfigurerAdapter{

    @Bean
    public ViewResolver getViewResolver(){
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".jsp");
        return resolver;
    }
}

答案 1 :(得分:1)

我使用Spring 4.3.4测试了你的场景,它运行正常。 我建议您仔细检查扫描包并正确提供AppConfig。 我将从AppConfig开始附加所有文件。然而,扩展WebMvcConfigurerAdapter是件好事。附带的源代码并不理想,它过于简单,只是为了尝试重现您报告的问题。

从AppConfig开始:

package com;

import org.springframework.context.annotation.*;
import org.springframework.web.servlet.view.*;

@Configuration
@ComponentScan("com")
public class AppConfig {
    @Bean
    public UrlBasedViewResolver getViewResovler() {
        UrlBasedViewResolver urlBasedViewResolver = new UrlBasedViewResolver();
        urlBasedViewResolver.setViewClass(JstlView.class);
        urlBasedViewResolver.setPrefix("/WEB-INF/jsp/");
        urlBasedViewResolver.setSuffix(".jsp");
        return urlBasedViewResolver;
    }

}

然后:

package com;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

    public class WebInit extends AbstractAnnotationConfigDispatcherServletInitializer {

        @Override
        protected Class<?>[] getRootConfigClasses() {
            return new Class<?>[] { };
        }

        @Override
        protected Class<?>[] getServletConfigClasses() {
            return new Class<?>[] { AppConfig.class };
        }

        @Override
        protected String[] getServletMappings() {
            return new String[] { "/" };
        }
    }

最后:

package com;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class MainController {

    @RequestMapping("/")
    public ModelAndView asdf() {
        return new ModelAndView("ABC");
    }
}

答案 2 :(得分:0)

上面的问题是在定义bean getViewResolver之前调用了DispatcherServlet.initViewResolvers,它无法找到bean,因此它永远不会添加视图解析器。

如果将bean定义移动到xml定义中,它就会被拾取。由于某种原因,如果XML中没有定义ViewResolvers,则您定义的MvcConfiguration类不会触发DispatcherServlet刷新。