小编典典

在Apache Tomcat上运行JSF项目

tomcat

如何在Tomcat上午餐JSP项目?我将WebContent文件webapp夹复制到Apache的文件夹中,但是找不到我的jsp页面,但是如果我将jsp更改为jsf(index.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>

错误:输入状态报告

消息/Graph/index.jsp

描述所请求的资源(/Graph/index.jsp)不可用。


阅读 382

收藏
2020-06-16

共1个答案

小编典典

那不是问题。这是预期的行为。您只是误解了基本Servlet
API的工作方式。您已经配置了JSF标准FacesServlet来侦听URL匹配,/faces/*并且已经配置了Apache
MyFaces专门MyFacesServlet侦听URls匹配*.jsf*.faces

要运行JSF,您必须在浏览器中通过与的映射相匹配的URL打开页面FacesServlet。鉴于您已经有了一个index.jsp文件,并且上下文路径为Graph并且已经在三种不同的URL模式上配置了两个JSF
servlet,您可以通过以下URL打开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.jsf为,welcome-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可以映射FacesServleton just
*.xhtml,这与Facelets文件的默认扩展名相同)

2020-06-16