简体   繁体   English

ViewResolver使用Java注释

[英]ViewResolver using Java annotation

Is it possible in Spring 3.1.1 to configure a view resolver using Java annotations? Spring 3.1.1中是否可以使用Java注释配置视图解析器?

I am done with all configurations using Java annotations, but I am stuck at view resolver . 我完成了使用Java注释的所有配置,但我被卡在视图解析器中

Code

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;
    }
}

I used this code and ran the application, but it's not returning the appropriate view. 我使用此代码并运行应用程序,但它没有返回适当的视图。 However, if I configure a viewresolver in the app-servlet.xml file, it works fine. 但是,如果我在app-servlet.xml文件中配置了一个viewresolver ,它可以正常工作。

Your class should extend WebMvcConfigurerAdapter class. 您的类应该扩展WebMvcConfigurerAdapter类。 please have a look at below example 请看下面的例子

@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;
    }
}

I tested your scenario with Spring 4.3.4 and it is working ok. 我使用Spring 4.3.4测试了你的场景,它运行正常。 I would suggest double checking packages you scan and that AppConfig is properly provided. 我建议您仔细检查扫描包并正确提供AppConfig。 I am attaching all files starting from your AppConfig. 我将从AppConfig开始附加所有文件。 Yet it is good to extend WebMvcConfigurerAdapter. 然而,扩展WebMvcConfigurerAdapter是件好事。 The source code attached is not ideal, it s simplistic and only to try to reproduce the problem you have reported. 附带的源代码并不理想,它过于简单,只是为了尝试重现您报告的问题。

Starting from AppConfig: 从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;
    }

}

Then: 然后:

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[] { "/" };
        }
    }

Finally: 最后:

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");
    }
}

The problem with the above is that the DispatcherServlet.initViewResolvers get's called before the bean getViewResolver is defined and it cannot find the bean so it never adds the view resolver. 上面的问题是在定义bean getViewResolver之前调用了DispatcherServlet.initViewResolvers,它找不到bean,因此它永远不会添加视图解析器。

If you move the bean definition into an xml definition, it get's picked up. 如果将bean定义移动到xml定义中,它就会被拾取。 For some reason the MvcConfiguration class you have defined does not trigger a DispatcherServlet refresh if there are no ViewResolvers defined in the XML. 由于某种原因,如果XML中没有定义ViewResolvers,则您定义的MvcConfiguration类不会触发DispatcherServlet刷新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM