2011-02-15 81 views
3

如何在Tomcat上共享午餐JSP項目?我複製WebContent文件夾到webapp文件夾的Apache,但它無法找到我的jsp頁面,但如果我將jsp更改爲jsf(index.jsf)可以正常工作。我怎麼解決這個問題?在Apache Tomcat上運行JSF項目

的web.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> 
    <display-name>Graph</display-name> 
    <welcome-file-list> 
    <welcome-file>index.jsp</welcome-file> 
    </welcome-file-list> 
    <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>/faces/*</url-pattern> 
    </servlet-mapping> 
    <context-param> 
    <param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name> 
    <param-value>resources.application</param-value> 
    </context-param> 
    <context-param> 
    <description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description> 
    <param-name>javax.faces.STATE_SAVING_METHOD</param-name> 
    <param-value>client</param-value> 
    </context-param> 
    <context-param> 
    <description> 
    This parameter tells MyFaces if javascript code should be allowed in 
    the rendered HTML output. 
    If javascript is allowed, command_link anchors will have javascript code 
    that submits the corresponding form. 
    If javascript is not allowed, the state saving info and nested parameters 
    will be added as url parameters. 
    Default is 'true'</description> 
    <param-name>org.apache.myfaces.ALLOW_JAVASCRIPT</param-name> 
    <param-value>true</param-value> 
    </context-param> 
    <context-param> 
    <description> 
    If true, rendered HTML code will be formatted, so that it is 'human-readable' 
    i.e. additional line separators and whitespace will be written, that do not 
    influence the HTML code. 
    Default is 'true'</description> 
    <param-name>org.apache.myfaces.PRETTY_HTML</param-name> 
    <param-value>true</param-value> 
    </context-param> 
    <context-param> 
    <param-name>org.apache.myfaces.DETECT_JAVASCRIPT</param-name> 
    <param-value>false</param-value> 
    </context-param> 
    <context-param> 
    <description> 
    If true, a javascript function will be rendered that is able to restore the 
    former vertical scroll on every request. Convenient feature if you have pages 
    with long lists and you do not want the browser page to always jump to the top 
    if you trigger a link or button action that stays on the same page. 
    Default is 'false' 
</description> 
    <param-name>org.apache.myfaces.AUTO_SCROLL</param-name> 
    <param-value>true</param-value> 
    </context-param> 
    <servlet> 
    <servlet-name>faces</servlet-name> 
    <servlet-class>org.apache.myfaces.webapp.MyFacesServlet</servlet-class> 
    <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet> 
    <servlet-name>UploadServlet</servlet-name> 
    <servlet-class>controler.UploadServlet</servlet-class> 
    </servlet> 
    <servlet-mapping> 
    <servlet-name>faces</servlet-name> 
    <url-pattern>*.jsf</url-pattern> 
    </servlet-mapping> 
    <servlet-mapping> 
    <servlet-name>faces</servlet-name> 
    <url-pattern>*.faces</url-pattern> 
    </servlet-mapping> 
    <servlet-mapping> 
    <servlet-name>UploadServlet</servlet-name> 
    <url-pattern>/Upload</url-pattern> 
    </servlet-mapping> 
    <listener> 
    <listener-class>org.apache.myfaces.webapp.StartupServletContextListener</listener-class> 
    </listener> 
</web-app> 

錯誤: 型狀態報告

message /Graph/index.jsp

description The requested resource (/Graph/index.jsp) is not available.

+1

您需要添加更多信息。 – 2011-02-15 10:06:28

回答

10

這不是問題。這是預期的行爲。你只是誤解了基本的Servlet API的工作原理。你已經配置了JSF標準FacesServlet聽匹配/faces/*網址,並已配置的Apache MyFaces的具體MyFacesServlet聽匹配*.jsf*.faces網址。

要獲得JSF運行,必須通過匹配FacesServlet的映射的URL打開瀏覽器頁面。既然你已經一個index.jsp文件和你的上下文路徑是Graph,並且已經在三個不同的URL模式配置了兩個JSF的servlet,您可以通過以下網址打開JSP的事實:


表示您的配置不必要的過於複雜。擺脫MyFacesServlet條目及其所有關聯的URL映射(servlet名稱爲faces)。只要堅持標準FacesServlet並使用其映射,或改爲改爲它。我個人推薦使用*.jsf

<servlet> 
    <servlet-name>facesServlet</servlet-name> 
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> 
    <load-on-startup>1</load-on-startup> 
</servlet> 
<servlet-mapping> 
    <servlet-name>facesServlet</servlet-name> 
    <url-pattern>*.jsf</url-pattern> 
</servlet-mapping> 

然後你可以通過http://localhost:8080/Graph/index.jsf打開頁面。


無關的具體問題,您welcome-file不會這樣的。 Tomcat會在該頁面上發出HTTP 404錯誤(頁面/資源未找到)。你需要指定index.jsfwelcome-file和在同一文件夾提供一個具體的,但index.jsf文件作爲index.jsp。通過這種方式,Tomcat將被愚弄,該文件存在並通過調用http://localhost:8080/Graph來顯示頁面。


如果您關注的是,它是可以打開通過其*.jsp擴展JSF頁面,這將導致一個RuntimeException: FacesContext not found,你反倒沒有一個JSP文件,該文件是送達普通的香草,那麼你可以限制直接訪問JSP文件由以下安全約束在web.xml

<security-constraint> 
    <display-name>Restrict direct access to JSP files</display-name> 
    <web-resource-collection> 
     <web-resource-name>JSP files</web-resource-name> 
     <url-pattern>*.jsp</url-pattern> 
    </web-resource-collection> 
    <auth-constraint /> 
</security-constraint> 

(在JSF 2.0,這是順便說不需要了,默認視圖技術的Facelets有可能在FacesServlet上只是*.xhtml地圖,這與Facelets文件的默認擴展名相同)

0

您可以使用Tomcat管理器應用程序部署

http://tomcatIP:8080/manager/html

那裏你可以上傳你的應用程序,它應該運行開箱即用 如果您不知道應輸入什麼用戶名和密碼,則必須配置您的tomcat-users.xml

相關問題