如何在 JSP(基于注释的 Spring Web MVC 项目)中显示图像?

How do I display images in my JSP (Annotation based Spring Web MVC Project)?

我需要在我的项目中显示图像。我正在使用 NetBeans IDE。我不能在这个项目中使用 XML,因为一切都是基于注释的,因此这就是我在 AppInit.java class:
中调用*我的调度程序 servlet 的方式 *(如果 "call" 是恰当的术语)

public class AppInit implements WebApplicationInitializer{

    @Override
    public void onStartup(javax.servlet.ServletContext container) throws javax.servlet.ServletException {
        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
        ctx.register(MVCConfigurer.class);
        ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher", new DispatcherServlet(ctx));
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("/");
    }

}

我已经检查了该网站上的其他一些答案,但它们都是基于 XML 的,建议在 dispatcher-servlet.xml 文件中包含类似以下代码的内容:

<mvc:resources mapping="/resources/**" location="/WEB-INF/resources/**"> </mvc:resources>

我负担不起使用 XML 重新配置我的整个项目。

我的 bean 在 MVCConfigurer.java 文件中定义如下:

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.lfa.emsys")
@PropertySource(value="classpath:db.properties")
class MVCConfigurer extends WebMvcConfigurerAdapter{
    @Value("${jdbc.driverClassName}")
    private String jdbcDriver;

    @Value("${jdbc.url}")
    private String jdbcUrl;

    @Value("${jdbc.username}")
    private String jdbcUsername;

    @Value("${jdbc.password}")
    private String jdbcPassword;

    @Value("${view.prefix}")
    private String viewPrefix;

    @Value("${view.suffix}")
    private String viewSuffix;

    @Bean
    public InternalResourceViewResolver getViewResolver(){
        InternalResourceViewResolver vr = new InternalResourceViewResolver(viewPrefix, viewSuffix);
        return vr;
    }

    @Bean
    public DataSource getDataSource(){
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(jdbcDriver);
        dataSource.setUrl(jdbcUrl);
        dataSource.setUsername(jdbcUsername);
        dataSource.setPassword(jdbcPassword);
        return dataSource;
    }

    @Bean
    public JdbcTemplate getJdbcTemplate(){
        JdbcTemplate template = new JdbcTemplate(getDataSource());
        return template;
    }

    @Bean
    public LocalSessionFactoryBean getSessionFactory(){
        LocalSessionFactoryBean bean = new LocalSessionFactoryBean();
        bean.setDataSource(getDataSource());
        bean.setPackagesToScan("com.lfa.emsys.entity");
        Properties properties = new Properties();
        properties.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
        properties.put("hibernate.show_sql", "true");
        bean.setHibernateProperties(properties);
        return bean;
    }

}

如何在我的 index.jsp 网页中显示 stack.jpg 图像?

有什么理由不能在 JSP 上使用图像标签,如下所示:

<img src="${pageContext.request.contextPath}/images/stack.JPG"/>

我找到了解决办法!我将 stack.jpg 文件放在 src/main/resources/assets 文件夹中,如下所示:

注意:我手动创建了 resourcesassets 文件夹。

我在 MVCConfigurer.java class:

中添加了这段代码
@Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**")
                .addResourceLocations("classpath:assets/");
    }

我像这样在 index.jsp 文件中插入图像:

<img src = "${pageContext.request.contextPath}/static/stack.jpg"/>

成功了。我正在寻找将以下 XML 代码行翻译成基于注释的形式:

<mvc:resources mapping="/static/**" location="/resources/assets/" />