0

Im currently learning HTML/CSS/JSF and wrote a litte code snippet that doesnt run the way I want. So I have a JSF run WebApplication which includes only an image, an outputText and a commandLink.

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html">

    <h:head>
        <title>Facelet Title</title>
    </h:head>
    <h:body>
        <img src="logo.jpg" width="250" alt="LOGO"/>
        <br/>
        <h:outputText value="#{counter.counter}"/>
        <h:form>
        <h:commandLink value="increase Counter" action="#{counter.increaseCounter()}"/>      
        </h:form>

    </h:body>
</html>

So the image source is a logo.jpg, which is in the same directory as the index.xhtml file and the counter value is loaded and increased from a Managed Bean.

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ApplicationScoped;

@ManagedBean
@ApplicationScoped
public class Counter {
    private int counter = 0;
    public Counter() {
    }
    public void increaseCounter() {
        counter++;
    }
    public int getCounter() {
        return counter;
    }
}

When I load the page, everything is shown. The Image, the Counter with value 0 and the commandLink. As soon as I click on the commandLink the counter increases and the page reloads with counter value 1, but the Image isnt loaded anymore. After the reload it just shows the alt value.

Why doesnt it load the image again ? Do I make something wrong ? Thanks a lot for your help :)

Kukeltje
  • 12,223
  • 4
  • 24
  • 47
Carlos
  • 1
  • Off-topic You seem to be using JSF2.2 or up, please switch to using `@Named` CDI beans instead of JSF managed `@ManagedBean` On-topic: you get a 404 in browser for the image? Checked the path? Investigate as a developer, you now describe it as an end-user. – Kukeltje May 28 '20 at 10:08
  • Thanks for your comment. If I check the generated html code the path of the img is still logo.jpg. The image doesnt appear, just the alternative text (LOGO) – Carlos May 28 '20 at 10:13
  • But do you get a 404 on it? Is the img tag in the browser identical to one in the the original page? – Kukeltje May 28 '20 at 10:17
  • @Kukeltje: The symptoms are typical when a /faces/* URL pattern is used instead of *.xhtml. OP incorrectly used a page-relative URL in plain HTML image tag. – BalusC May 28 '20 at 10:19
  • @BalusC: ahhh... right... the /faces/* Did not use that for almost 10 years now... keep forgetting about the related issues.. Thanks for the reminder. – Kukeltje May 28 '20 at 10:23
  • @Kukeltje Thanks for your help. By changing the URL pattern in the web.xml to *.xhmtl and the welcome file to index.xhtml I could make it show the image after the refresh. – Carlos May 28 '20 at 14:03

0 Answers0