viewParam事件出错 [英] viewParam event going wrong

查看:129
本文介绍了viewParam事件出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个网页,该网页从URL检查视图参数,并调用bean的init方法来检索该用户.然后,该页面上的字段将填充该用户的信息.

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.

但是出了点问题.

我的Facelets页面:

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>

还有UserBean.java:

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();
        }
    }
}

由于未调用System.out.println(name),因此我不认为该网页正在调用init.如果我在没有URL适应(http://localhost:8080/KwetterJSF/)的情况下启动网页,则会收到以下错误消息:

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无法调用[方法] @PostConstruct public beans.UserBean@3c836d3d上的beans.UserBean.init(ComponentSystemEvent)

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

如果我添加参数(http://localhost:8080/KwetterJSF/index.xhtml?user=Sjaak),则会得到以下信息:

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?

推荐答案

有2个问题.

首先,您将<f:event>@PostConstruct混合.

@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.

第二,为了正确执行JSF页面,您需要确保浏览器地址栏中显示的URL模式与在/WEB-INF/web.xml中注册的FacesServlet之一匹配.因此,如果是例如*.jsf,则需要确保通过/page.jsf URL(而不是/page.xhtml)打开页面.如果不这样做,JSF标记/组件和EL表达式将不会被识别,因此将被视为纯文本".

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".

更好的方法是直接将FacesServlet映射到*.xhtml的URL模式上.这使您免于虚拟URL的困扰.

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

这篇关于viewParam事件出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆