在Apache Tomcat上运行JSF项目 [英] Run JSF project on Apache Tomcat

查看:115
本文介绍了在Apache Tomcat上运行JSF项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Tomcat上进行午餐JSP项目?我将 WebContent 文件夹复制到Apache的 webapp 文件夹,但是找不到我的jsp页面,但如果我更改了jsp到jsf(index.jsf)工作正常。我该如何解决这个问题?

How can lunch JSP project on Tomcat? I copy WebContent folder to webapp folder of Apache but it can't find my jsp page, but if I change jsp to jsf (index.jsf) works fine. How can I solve this problem?

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>

错误:
类型状态报告

Error: type Status report


message /Graph/index.jsp

message /Graph/index.jsp

description请求的资源
(/Graph/index.jsp)不是可用。

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


推荐答案

这不是问题。这是预期的行为。您只是误解了基本的Servlet API的工作原理。您已配置JSF标准 FacesServlet 以侦听与 / faces / * 匹配的URL,并且您已配置Apache MyFaces特定 MyFacesServlet 收听符合 *。jsf * .faces

That's not a problem. That's expected behaviour. You're just misunderstanding how the basic Servlet API works. You have configured the JSF-standard FacesServlet to listen on URLs matching /faces/* and you have configured Apache MyFaces specific MyFacesServlet to listen on URls matching *.jsf and *.faces.

要让JSF运行,你必须在浏览器中用一个与 FacesServlet的映射相匹配的URL打开页面。鉴于您有一个 index.jsp 文件,并且您的上下文路径是 Graph 并且您已配置在三种不同的URL模式上有两个JSF servlet,您可以通过以下URL打开JSP:

To get JSF to run, you have to open the page in browser by an URL which matches the mapping of the FacesServlet. Given the fact that you've an index.jsp file and that your context path is Graph and that you have configured two JSF servlets on three different URL patterns, you can open the JSP by the following URLs:

  • http://localhost:8080/Graph/faces/index.jsp (invokes FacesServlet)
  • http://localhost:8080/Graph/index.jsf (invokes MyFacesServlet)
  • http://localhost:8080/Graph/index.faces (invokes MyFacesServlet)

说,您的配置不必要地过于复杂。摆脱 MyFacesServlet 条目及其所有相关的URL映射(servlet名称为 faces )。只需坚持标准 FacesServlet 并改为使用其映射,或改为改变它。我个人建议使用 *。jsf

Said that, your configuration is unnecessarily overcomplicated. Get rid of the MyFacesServlet entry and all of its associated URL mappings (with the servlet name of faces). Just stick to the standard FacesServlet and use its mapping instead, or alter it instead. I personally recommend using *.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.jsf 指定为 welcome-file 并提供具体但 index.jsf index.jsp 相同的文件夹中的文件。这样Tomcat就会被文件存在,并通过调用 http:// localhost:8080 / Graph 来显示页面。

Unrelated to the concrete problem, your welcome-file won't work that way. Tomcat would give a HTTP 404 error on that (page/resource not found). You need to specify index.jsf as welcome-file and supply a concrete but empty index.jsf file in the same folder as your index.jsp. This way Tomcat will be fooled that the file exist and show the page by just calling http://localhost:8080/Graph.

如果您担心可以通过 *打开JSF页面。*。jsp 扩展,这将导致 RuntimeException:未找到FacesContext 并且您实际上没有一个JSP文件可以提供普通服务,那么您可以限制直接访问JSP文件在 web.xml中的以下安全约束

If your concern is that it is possible to open JSF pages by their *.jsp extension which would result in a RuntimeException: FacesContext not found and you have actually no one JSP file which is to be served plain vanilla, then you can restrict direct access to JSP files by the following security constraint in 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可以在 *。xhtml 上映射 FacesServlet ,这与Facelets文件的默认扩展名相同)

(in JSF 2.0 this is by the way not needed anymore, with the default view technology Facelets it's possible to map the FacesServlet on just *.xhtml, which is the same as the default extension of Facelets files)

这篇关于在Apache Tomcat上运行JSF项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆