当我在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>
<<p> index . jsp/strong> <%@ 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>
您告诉JSF使用Facelets作为视图技术,但是您实际上使用jsp作为视图。这是行不通的。Facelets是JSP的继承者,您应该选择使用其中一个。
假设您只想使用jsp,那么您应该从faces-config.xml
中删除 <view-handler>
,也从web.xml
中删除 javax.faces.DEFAULT_SUFFIX
参数,最好还从web.xml
中删除 ConfigureListener
声明。
或者如果你实际上想要使用Facelets(推荐!),那么你需要配置Facelets并按照Facelets开发指南将jsp重写为xhtml。