【问题标题】:why I cannot get the url parameter with jsf?为什么我无法使用 jsf 获取 url 参数?
【发布时间】:2011-04-20 15:01:02
【问题描述】:

我使用 jsf 1.x。我不明白我的代码哪里错了。

faces-config.xml

<managed-bean>
  <managed-bean-name>mainNavigationParametersHandlerBean</managed-bean-name>
  <managed-bean-class>com.test.MainNavigationParametersHandlerBean</managed-bean-class>
  <managed-bean-scope>request</managed-bean-scope>
  <managed-property>
   <property-name>userType</property-name>
   <property-class>java.lang.String</property-class>
   <value>#{param.userType}</value>
  </managed-property>
 </managed-bean>

我的豆子:

包 com.test;

public class MainNavigationParametersHandlerBean {
    // url parameters
    private String userType = "";

    public String getUserType() {
        return userType;
    }

    public void setUserType(String userType) {
        this.userType = userType;
    }
}

还有我的jsp页面(实际上它使用了jsf文件中的内容):

<jsp:root
    xmlns:jsp="http://java.sun.com/JSP/Page"
    xmlns:c="http://java.sun.com/jstl/core"
    version="1.2">
    <head>
    </head>
    <body>   
        <jsp:forward page="/WEB-INF/jsf/userapp-main.jsf" />        
    </body>
</jsp:root>

在 userapp-main.jsf 中,我有一个标题,我想在其中打印出该 url 的 userType 参数值:

http://localhost:8080/index-userapp.jsp?userType=grav

但是,在我的 xhtml 页面中,#{mainNavigationParametersHandlerBean.userType} 似乎是空的...

你明白为什么吗?

谢谢。

【问题讨论】:

  • 你的 faces servlet 映射到什么模式?您是否检查了托管 bean 本身是否已初始化(例如通过在其构造函数中放置一个 println)?

标签: java jsf


【解决方案1】:

右键单击浏览器中打开的页面并选择查看源代码。您是否在 HTML 中看到生成的 JSF 标记或未解析的 JSF 标记的 HTML 输出?我敢打赌是后者。

您的 JSP 方法有点奇怪。我只需将userapp-main.jsp 页面的内容放在index-userapp.jsp 页面中,并通过与FacesServlet 的URL 模式匹配的URL 调用该页面,如web.xml 中定义的那样。例如*.jsf,则需要http://localhost:8080/index-userapp.jsf?userType=grav打开页面。

如果有空间,我也会考虑升级。 JSP 1.2 相当古老(已经有 10 年历史了),它目前是 2.2,它的继任者 Facelets 已经在大约 1.5 年前发布的 Java EE 6 中取代了它的位置。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-27
    • 1970-01-01
    • 2013-12-06
    • 2021-01-17
    • 2014-06-13
    • 1970-01-01
    • 2018-12-24
    相关资源
    最近更新 更多