ViewResolver的使用Java注释 [英] ViewResolver using Java annotation

查看:152
本文介绍了ViewResolver的使用Java注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的3.1.1配置使用Java注释的视图解析器这可能吗?

Is it possible in Spring 3.1.1 to configure a view resolver using Java annotations?

我用Java注释所有配置完成,但我坚持在视图解析器

I am done with all configurations using Java annotations, but I am stuck at view resolver.

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

我用这个code和运行应用程序,但它没有返回相应的视图。但是,如果我配置的视图解析器 APP-servlet.xml中文件,它工作正常。

推荐答案

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

Your class should extend WebMvcConfigurerAdapter class. 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;
    }
}

这篇关于ViewResolver的使用Java注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆