0

I created a webpage, which checks the view-parameter from the URL, and calls the init-method of a bean to retrieve that user. The fields on that page are then filled with the information of that user.

But something is going wrong.

My Facelets page:

<?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://java.sun.com/jsf/html"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:f="http://java.sun.com/jsf/core">
    <h:head>
        <title>Tweetpage of #{userBean.getName()}</title>
    </h:head>
    <h:body>
        <f:metadata>
            <f:viewParam name="user" value="#{userBean.name}" />
            <f:event type="preRenderView" listener="#{userBean.init}" />
        </f:metadata>
        <h:commandButton value="Login" action="#{loginBean.login()}" id="login" />
        <div class="namebox">
            <label>User: #{userBean.name} </label> <br/>
            <br/>
        </div>
</h:body>
</html>

And the UserBean.java:

package beans;

import ...

@Named
@RequestScoped
public class UserBean implements Serializable {
    private String userName;
    private String name;
    private String bio;
    private String web;
    private Collection<Tweet> tweets;
    private Collection<User> followers;
    private User user;
    @Inject
    private @Named(value = "kwetterService")
    KwetterService service;

    @PostConstruct
    public void init(ComponentSystemEvent event) throws AbortProcessingException {
        System.out.println(name);
        user = service.find(name);
        if (user != null)
        {
            name = user.getName();
            bio = user.getBio();
            web = user.getWeb();
            tweets = user.getTweets();
            followers = user.getFollowing();
        }
    }
}

Since the System.out.println(name) isn't called, I don't think the webpage is calling the init. If I start the webpage without URL-adaptions (http://localhost:8080/KwetterJSF/), I get the following error message:

WELD-000049 Unable to invoke [method] @PostConstruct public beans.UserBean.init(ComponentSystemEvent) on beans.UserBean@3c836d3d

And if I add parameters (http://localhost:8080/KwetterJSF/index.xhtml?user=Sjaak), I get the following:

User: #{userBean.name}  

I'm not too experienced with this, and I can't figure it out even though I researched a bit myself. Does anybody know the solution?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Joetjah
  • 6,292
  • 8
  • 55
  • 90

1 Answers1

1

There are 2 problems.


First, you're mixing <f:event> with @PostConstruct.

The @PostConstruct can only be a method which does not take arguments. That explains the exception. Get rid of that annotation. It runs too early anyway when view parameters play a role.

See also:


Second, in order to properly execute a JSF page, you need to make sure that the URL pattern as appears in browser's address bar matches the one of the FacesServlet as registered in /WEB-INF/web.xml. So if it's for example *.jsf, then you need to make sure that you open the page by /page.jsf URL, not /page.xhtml. If you don't do that, JSF tags/components and EL expressions won't be recognized and thus be treated as "plain text".

Much better is however to just map the FacesServlet directly on an URL pattern of *.xhtml. This saves you from virtual URL headache.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • My page is indeed called index.xhtml. But I've found out another problem (probably why everything is going wrong). When I initially open the page on `http://localhost:8080/KwetterJSF/`, all values are `null`. This is correct since no viewParam is given. Although, when I try to navigate to `http://localhost:8080/KwetterJSF/index.xhtml', everything is going wrong. So with or without parameters, I'm guessing the web URL is wrong. I'm working in Netbeans on this project. Any help on how I can solve this??? – Joetjah Oct 08 '12 at 19:22
  • Did you understand the part about the FacesServlet URL pattern? Given this comment you don't seem to understand anything about it nor have attempted to do so. Please open your `web.xml` and look for the `` entry of the `FacesServlet` and let me know what you're seeing. As to opening the page on `/`, you should of course make sure that your `` is set right, but that's a different story. – BalusC Oct 08 '12 at 19:25
  • I'm sorry, I guess my mind was busy on the next problem while you were actually talking about the same problem. The url-pattern value is `/faces/*`. And indeed, after visiting `http://localhost:8080/KwetterJSF/faces/index.xhtml`, I got to the exact same page. Placing `?user=Sjaak` behind it gives me another error, but this is because my beans aren't set up correctly yet (it tries to set the name of the user, although I need that value to GET the name of the user so I can use the init-method to set up the required parameters which can be GETTED again on the page). Thanks for the help! – Joetjah Oct 08 '12 at 19:36