【发布时间】:2013-01-03 19:37:18
【问题描述】:
是否可以在Spring 3.1.1 中使用 Java 注释配置视图解析器?
我已经使用 Java 注释完成了所有配置,但我被困在 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,它可以正常工作。
【问题讨论】:
-
到底是什么问题?视图解析器是普通的bean,如果你使用
@Configuration,使用@Bean来声明它们。 -
@axtavt 请看看我更新的问题。
标签: java spring model-view-controller spring-mvc annotations