java.lang.IllegalArgumentException:JSF中的非法视图ID

时间:2011-04-25 14:12:02

标签: java jsf tomcat

当我在tomcat中运行jsf项目时,它给出了如下错误。

java.lang.IllegalArgumentException: Illegal view ID .jsp/Welcome.jsp.  The ID must begin with /
    com.sun.faces.application.ViewHandlerImpl.getActionURL(ViewHandlerImpl.java:629)
    com.sun.facelets.FaceletViewHandler.getActionURL(FaceletViewHandler.java:781)
    com.sun.facelets.FaceletViewHandler.handleFaceletNotFound(FaceletViewHandler.java:686)
    com.sun.facelets.FaceletViewHandler.renderView(FaceletViewHandler.java:637)
    com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:106)
    com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:251)
    com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:144)
    javax.faces.webapp.FacesServlet.service(FacesServlet.java:245)

faces-config.xml中

<?xml version='1.0' encoding='UTF-8'?>

<faces-config xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd"
    version="1.2">

    <application>
        <view-handler>com.sun.facelets.FaceletViewHandler</view-handler>
    </application></faces-config>

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    version="2.5"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <context-param>
        <param-name>javax.faces.DEFAULT_SUFFIX</param-name>
        <param-value>.jsp</param-value>
    </context-param>
    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.jsf</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    <listener>
        <listener-class>com.sun.faces.config.ConfigureListener</listener-class>
    </listener>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>WEB-INF/applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
</web-app>

的index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <base href="<%=basePath%>">
        <title>Index</title>
    </head>
    <body>
        This is my Index page.
        <a href="jsp/Welcome.jsf"> <<< here</a>
        <br>
    </body>
</html>

Welcome.jsp中

<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>

<html>
<head>
    <title>Welcome</title>
</head>
<body>
    <f:view>
        Welcome Page. <br>
    </f:view>
</body>
</html>

1 个答案:

答案 0 :(得分:2)

你告诉JSF使用Facelets作为视图技术,但你实际上使用JSP作为视图。这不会起作用。 Facelets是JSP的继承者,您应该选择使用其中一个。

假设您只想使用JSP,那么您应该删除来自<view-handler>的{​​{1}},同时删除 {{1} }来自faces-config.xml的参数,最好还删除来自javax.faces.DEFAULT_SUFFIX的{​​{1}}声明。

或者,如果实际想要使用Facelets(推荐!),那么您需要根据Facelets developer guide配置Facelets并将JSP重写为XHTML。