1

I have a page called index.xhtml, in which I use variables from a bean class to fill the page with information. But when I start the file, it looks like it's not using the bean.

My index.xhtml:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.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>
        <script language="JavaScript" type="text/javascript" src="resources/tab-panel.js"></script>
        <link rel="stylesheet" href="resources/style.css" type="text/css" />
        <title>Tweetpage of #{userBean.name}</title>
    </h:head>

    <f:metadata>
        <f:viewParam name="user" value="#{userBean.name}" />
        <f:event type="preRenderView" listener="#{userBean.init()}" />
    </f:metadata>

    <h:body onload="bodyOnLoad()" onResize="raisePanel(currentMenuIndex)">
        <div class="loginbox">
            <h:link value="Login" outcome="user.xhtml" />
        </div>
        <div class="namebox">
            <h:outputLabel>User: #{userBean.name} </h:outputLabel>
        </div>
        <div class="detailsbox"> 
            <h:outputText>Name: #{userBean.getName()} </h:outputText>
            <h:outputText>Web: #{userBean.getWeb()} </h:outputText>
            <h:outputText>Bio: #{userBean.getBio()} </h:outputText>
        </div>

My UserBean.java:

@ManagedBean
@SessionScoped
public class UserBean implements Serializable {

    @Inject @Named(value = "userService")
    private UserService service;

    private String name;

    private User user;

    public UserBean() {

    }

My webpage looks like this:

User: #{userBean.name} 
Name: #{userBean.getName()}  

As you can see, it doesn't say null or Dude, but I get the code in the page instead. I navigate to the site using this URL: http://localhost:8080/Kwetter/index.xhtml?user=Dude

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Joetjah
  • 6,292
  • 8
  • 55
  • 90
  • are you initializing the bean? check this out this link: http://en.wikipedia.org/wiki/JavaBeans – İsmet Alkan Jan 19 '13 at 14:58
  • 3
    If you're just starting with JSF, I recommend to peek around in our JSF wiki page. Hover the `[jsf]` tag which you placed on the question until a black popbox shows up and click therein the *info* link. There's a Hello World and some links to sane tutorials. – BalusC Jan 19 '13 at 15:07
  • Hmm.. Deja vu? http://stackoverflow.com/questions/12787325/viewparam-event-going-wrong You had exactly the same problem once 2 months ago. You was apparently using `/faces/*` mapping instead of `*.xhtml`. – BalusC Jan 19 '13 at 15:09
  • I knew I encountered this problem before, haha. I searched my asked questions but I couldn't find it. The main problem this time was that I was missing the `web.xml` file completely. NetBeans doesn't automatically generate that, so I didn't even think about mapping it in the file. – Joetjah Jan 19 '13 at 15:17
  • The answer given to you provides some usefull links and information. The wiki-page of JSF made me some things clear. Thank you! – Joetjah Jan 19 '13 at 15:18

1 Answers1

4

That will happen when the FacesServlet is not invoked. It's the one responsible for performing all the JSF and EL works. You need to make sure that the request URL as you see in browser's address bar matches the URL pattern of the FacesServlet as definied in web.xml. If you took the effort to view the HTML source code by rightclick, View Source in browser, then you should have noticed that all JSF tags are still unparsed instead of that their HTML representation is generated.

So, if you've mapped it on *.jsf, then you should open it by http://localhost:8080/Kwetter/index.jsf?user=Dude instead.

An alternative is to just remap the FacesServlet on an URL pattern of *.xhtml.
This way you never need to worry about virtual URLs.

See also:


Unrelated to the concrete problem, the way how you're using <h:outputText> is not right. Just get rid of them. You should also preferably not use the method expression syntax but just the value expression syntax.

<div class="namebox">
    <h:outputLabel value="User: #{userBean.name}" />
</div>
<div class="detailsbox"> 
    Name: #{userBean.name}
    Web: #{userBean.web}
    Bio: #{userBean.bio}
</div>

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555