jsf常见错误

本文针对JSF(JavaServer Faces)开发过程中常见的错误进行了详细分析,包括如何解决配置文件问题、表达式错误、实例化失败等问题,并提供了具体的修改建议。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 JSF常见错误分析

    * According to TLD or attribute directive in tag file, attribute value does not accept any expressions 
    * Can't instantiate class:xxx.xxx.xxx
    * java.lang.IllegalStateException: No thread-bound request: use RequestContextFilter
    * 导航出现问题,不工作
    * value="#{userBean.user.userName}": Target Unreachable, 'user' returned null

    * According to TLD or attribute directive in tag file, attribute value does not accept any expressions

    1. 修改web.xml.

    <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
    改为2.3版本的

    <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
    <web-app>
    2. 使用JSTL core RT库

    JSTL core库的有两种taglib伪指令, 其中RT库即是依赖于JSP传统的请求时属性值, 而不是依赖于EL来实现(称为EL库.JSP2.0将支持EL)

    JSP中使用<%@ taglib uri=http://java.sun.com/jstl/core prefix="c"%>在2.3版本都可以,在2.4就不行了, 难道是版本不兼容吗?

    只要将

    <%@ taglib uri="http://java.sun.com/jstl/core" prefix="c"%>
    改为

    <%@ taglib uri=http://java.sun.com/jstl/core_rt prefix="c"%>
    另外:
    JSF expressions can access stuff in the http session fine.

    When expression #{foo} is encountered, JSF does this:
    (1) look in the request-scope vars for name "foo"
    (2) look in the http-session vars for name "foo"
    (3) look in the app-scope vars for name "foo"
    (4) look for a managed-bean declaration for "foo" and if found create an instance and put it in whatever scope was declared.
    (5) report an error

    The first 3 steps are identical to the JSP variable lookup. The var scopes are the same ones used by JSP. If there is an entry for "foo" in the http session then it will be found, regardless of what code put it there.

    Note however that JSF does not have any equivalent to the JSP "page scope". That's a JSP-specific feature that is not in the servlet spec and is not accessable to anything other than JSP code.

    The JSTL tags do NOT work well with JSF in general,
    还有,就是换一个应用服务器,从Tomcat换到JBoss试试。

    * Can't instantiate class:xxx.xxx.xxx

    比如:de.mindmatters.faces.component.html
    修改Web.xml中配置,把参数verifyObjects设置成false。(感觉不过只是个表面的修改方案。)
    <context-param>
       <param-name>com.sun.faces.verifyObjects</param-name>
       <param-value>false</param-value>
    </context-param>
    修改后不再出现类似问题,不过会出现其他问题。

    * java.lang.IllegalStateException: No thread-bound request: use RequestContextFilter

    是因为在Web.xml中配置没有增加Spring的相应Listener, 将下面代码贴如Web.xml中即可。
    <listener>
           <listener-class>
        org.springframework.web.context.request.RequestContextListener
       </listener-class>
    </listener>

    * 导航出现问题,不工作

    这可能是你的faces-config.xml出现问题,比如你在配置<from-view-id>的时候没有给路径“/test.xhtml”,而是直接写的“test.xhtml”,是不行的。

    * value="#{userBean.user.username}": Target Unreachable, 'user' returned null

    主要是因为在faces-config.xml中对应的managed bean配置中,没有把user属性定义出来。
    还会出现value="#{userBean.user.username}": Target Unreachable, 'user' returned null
    主要是因为在Backing Bean的getUser方法中,直接返回了user,没有判断user是否为空,如果为空需要new一个User出来:
    public User getUser(){
       if (user == null) user = new User();
       return user;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值