简体   繁体   中英

Bean not used in JSF page

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

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:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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