viewParam event going wrong

旧时模样 提交于 2019-12-12 03:56:56

问题


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?


回答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:

  • ViewParam vs @ManagedProperty(value = "#{param.id}")

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.

  • JSF Facelets: Sometimes I see the URL is .jsf and sometimes .xhtml. Why?


来源:https://stackoverflow.com/questions/12787325/viewparam-event-going-wrong

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!