简体   繁体   English

如何使用JSP使用Spring MVC显示图像

[英]How to display an image with Spring MVC using JSP

I'm having a hard time getting an image to display using Spring MVC with JSP. 我很难使用带有JSP的Spring MVC来显示图像。 I know there are other very similar SO questions, but none of the answers on those questions seemed to work. 我知道还有其他类似的SO问题,但这些问题的答案似乎都没有效果。

I have a very simple project with only two java files and a single JSP file to display. 我有一个非常简单的项目,只显示两个java文件和一个JSP文件。 This is my WebMvcConfigurerAdapter class: 这是我的WebMvcConfigurerAdapter类:

@Configuration
@ComponentScan(basePackages="com.rigatron.rigs4j")
@EnableWebMvc
public class MvcConfiguration extends WebMvcConfigurerAdapter{

@Bean
public ViewResolver getViewResolver(){
    InternalResourceViewResolver resolver = new InternalResourceViewResolver();
    resolver.setPrefix("/WEB-INF/views/");
    resolver.setSuffix(".jsp");
    return resolver;
}

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/resources/**")
            .addResourceLocations("/resources/");
}

This is my home page's controller class: 这是我的主页控制器类:

@Controller
public class HomeController {

@RequestMapping(value="/")
public ModelAndView test(HttpServletResponse response) throws IOException {
    return new ModelAndView("home");
}

Here is the line in the JSP file where I am trying to display my image: 这是JSP文件中我试图显示图像的行:

<img src="/resources/old.jpeg" alt="Photo of Youthful William" id="pic" />

I have old.jpeg in my /resources/ folder. 我的/ resources /文件夹中有old.jpeg。 The rest of the page is displayed fine, but only the alt text is displayed here. 页面的其余部分显示正常,但此处仅显示alt文本。

I am deploying this app using Tomcat8, and I have this line is my Maven pom file which copies the produced WAR file into Tomcat's webapps directory every time I build it: 我正在使用Tomcat8部署这个应用程序,我有这个行是我的Maven pom文件,它每次构建时都会将生成的WAR文件复制到Tomcat的webapps目录中:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-war-plugin</artifactId>
  <configuration>
    <outputDirectory>C:\my\local\dir\tomcat8\webapps</outputDirectory>
  </configuration>
</plugin>

So when I navigate using my browser to localhost:8080/myprojectname the rest of the JSP file is displayed correctly, but the photo is not, only the alt text. 因此,当我使用浏览器导航到localhost:8080 / myprojectname时,JSP文件的其余部分正确显示,但照片不是,只有alt文本。 Any help with this issue would be appreciate. 任何有关此问题的帮助将不胜感激。

Ok, I figured out where I was going wrong. 好的,我弄清楚我哪里出错了。 This SO post pointed me in the right direction: 这篇SO帖子指出了我正确的方向:

Can't display images in JSP 无法在JSP中显示图像

So basically, I was trying to load resources from the root of the web server instead of the root of my webapp. 所以基本上,我试图从Web服务器的根目录而不是我的webapp的根目录加载资源。 So, in order to reference the root of the webapp I included these two lines: 所以,为了引用webapp的根,我包括以下两行:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

... ...

<img src="<c:url value='/resources/old.jpeg'/>" alt="Photo of Youthful William" id="pic" />

In order to use this JSTL tag I had to include this Maven dependency: 为了使用这个JSTL标记,我必须包含这个Maven依赖项:

<dependency>
    <groupId>jstl</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
</dependency>

I apologize for perhaps posting this question prematurely, but I'm going to leave it up in hopes that it can help someone else. 我为过早地发布这个问题而道歉,但我会留下它希望它可以帮助其他人。

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

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