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

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

问题描述

是否可以在 Spring 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;
    }
}

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

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.

推荐答案

你的类应该扩展 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;
    }
}

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

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