如何在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>
Run Code Online (Sandbox Code Playgroud)
错误:输入状态报告
消息/Graph/index.jsp
description请求的资源(/Graph/index.jsp)不可用.
Bal*_*usC 10
那不是问题.这是预期的行为.您只是误解了基本的Servlet API的工作原理.你已经配置了JSF标准FacesServlet来监听匹配的网址/faces/*,你已经配置了Apache MyFaces的具体MyFacesServlet听上匹配的网址*.jsf和*.faces.
要使JSF运行,您必须在浏览器中通过与该映射匹配的URL打开该页面FacesServlet.鉴于您有一个index.jsp文件并且您的上下文路径是Graph并且您已经在三种不同的URL模式上配置了两个JSF servlet,您可以通过以下URL打开JSP:
FacesServlet)MyFacesServlet)MyFacesServlet)说,您的配置不必要地过于复杂.摆脱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>
Run Code Online (Sandbox Code Playgroud)
然后你可以通过http:// localhost:8080/Graph/index.jsf打开页面.
与具体问题无关,你welcome-file不会这样做.Tomcat会在其上发出HTTP 404错误(找不到页面/资源).您需要指定index.jsfas welcome-file并在与您的文件夹相同的文件夹中提供具体但空的 index.jsf文件index.jsp.这样Tomcat就会被文件存在,并通过调用http:// localhost:8080/Graph来显示页面.
如果您担心可以通过*.jsp扩展来打开JSF页面,这会导致a 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>
Run Code Online (Sandbox Code Playgroud)
(在JSF 2.0中,这是不再需要的方式,使用默认的视图技术Facelets可以映射FacesServleton *.xhtml,这与Facelets文件的默认扩展名相同)