为什么WEB-INF文件夹里面的jsp文件有效,但放在WEB-INF下的文件夹下不行? [英] Why jsp files inside WEB-INF folder works , but placed under a folder under WEB-INF doesn't?

查看:127
本文介绍了为什么WEB-INF文件夹里面的jsp文件有效,但放在WEB-INF下的文件夹下不行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我的jsp文件在WEB-INF文件夹中时(作为/WEB-INF/file.jsp),我可以从localhost:8080 / ProjectCtxtRoot /访问它们,但如果它们被放入,我无法访问它们/WEB-INF/jsp/file.jsp?

When my jsp files are inside WEB-INF folder (as /WEB-INF/file.jsp) , I can access them from localhost: 8080/ProjectCtxtRoot/, but I can't access them, if they are placed in /WEB-INF/jsp/file.jsp?

我在web.xml中更改了welcome-list标记中的路径,如下所示

I changed the path in welcome-list tag in web.xml as follows

<welcome-file-list>
       <welcome-file>/JSP/fileName.jsp</welcome-file>
</welcome-file-list>

我还更改了dispatcher-servlet.xml,如下所示

I also changed the dispatcher-servlet.xml as follows

   <bean id="jspViewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:prefix="/WEB-INF/jsp/"
          p:suffix=".jsp" /> 

仍然不起作用。用于上述情况的网址是

Still it doesn't work. Url used for above case are

 localhost:8080/ContextRoot/jsp/
 localhost:8080/ContextRoot/jsp/fileName.jsp
 localhost:8080/ContextRoot/jsp/fileName 

并且它没有适用于上述任何网址。

and it doesn't work for any of the above urls.

但是当它工作时

 <welcome-file-list>
       <welcome-file>/fileName.jsp</welcome-file>
</welcome-file-list>

dispatcher-servlet.xml如下

dispatcher-servlet.xml as follows

<bean id="jspViewResolver"
      class="org.springframework.web.servlet.view.InternalResourceViewResolver"
      p:prefix="/WEB-INF/"
      p:suffix=".jsp" /> 

用于上述情况的网址是localhost:8080 / ContextRoot /并且可以使用。

Url used for above case is localhost:8080/ContextRoot/ and it works.

我使用的是tomcat v 7.0服务器。我在Eclipse IDE中刷新我的项目,然后清理它,构建它,使用mvn clean install构建war,然后从tomcat manager主页选择war并部署它。我每次都这样做。

I use tomcat v 7.0 server. I refresh my project in Eclipse IDE, then clean it, build it, build war using mvn clean install, then choose the war from tomcat manager homepage and deploy it. I do this each time.

这就是diapatcher-servlet.xml在整个过程中看起来的样子。我只是改变上面所说的特定部分

This is how the diapatcher-servlet.xml looks all through the process. I just change particular section as above said

<?xml version="1.0" encoding="UTF-8"?>


<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
                    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd     
                    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
                    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">

    <mvc:annotation-driven/>

    <context:component-scan base-package="com.projectName.www" />

    <!-- Factory bean that creates the Mongo instance -->
    <bean id="mongo" class="org.springframework.data.mongodb.core.MongoFactoryBean">
        <property name="host" value="localhost" />
    </bean>

    <!-- MongoTemplate for connecting and quering the documents in the database -->
    <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
        <constructor-arg name="mongo" ref="mongo" />
        <constructor-arg name="databaseName" value="tableName" />
    </bean>

    <!-- Use this post processor to translate any MongoExceptions thrown in @Repository annotated classes -->
    <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />



    <bean id="jspViewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:prefix="/WEB-INF/jsp/"
          p:suffix=".jsp" /> 

  <!--   <bean class="org.springframework.web.servlet.view.tiles2.TilesViewResolver"/>

    <bean class=
    "org.springframework.web.servlet.view.tiles2.TilesConfigurer"> -->
 <!--  <property name="definitions">
    <list>
      <value>/WEB-INF/views/views.xml</value>
    </list>
  </property> 
</bean> -->




</beans>

这就是我的web.xml的样子

This is how my web.xml looks like

<web-app>

<!--   <display-name>Archetype Created Web Application</display-name> -->

   <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/src/main/webapp/WEB-INF/dispatcher-servlet.xml</param-value>
        </init-param>-->
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
    </context-param> 

   <!--  <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener> -->

    <welcome-file-list>
       <welcome-file>/fileName.jsp</welcome-file>
    </welcome-file-list> 


</web-app>

好的。当我将整个jsp文件夹从/webapp/WEB-INF/jsp/fileName.jsp移动到/webapp/jsp/fileName.jsp时,它可以工作。我想知道
1.为什么现在有效?
2.这是正确的做事方式吗?
3.当url为localhost:8080 / CtxtRoot / jsp /或localhost:8080 / CtxtRoot / jsp / search.jsp时,它可以工作,但它不适用于localhost:8080 / AnnaUnivResults / jsp / search。为什么会这样?

Ok. It works when I moved the entire jsp folder from /webapp/WEB-INF/jsp/fileName.jsp to /webapp/jsp/fileName.jsp. I would like to know 1. why it works now? 2. Is this the correct way to do things? 3. It works when the url is localhost:8080/CtxtRoot/jsp/or localhost:8080/CtxtRoot/jsp/search.jsp, but it doesn't work for localhost:8080/AnnaUnivResults/jsp/search. Why is it so?

推荐答案

我认为这里有几个问题:

I think there are a couple problems here:


  1. 您对使用spring MVC的路径感到困惑

  2. 您没有正确配置您的web xml

不幸的是我不能为你详细介绍每个细节,很多弹簧都是可配置的,所以我的解释只是涵盖了最基本的场景。如果有人发现错误,请告诉我,我会解决它。

Unfortunately I can't cover every detail for you, a lot of spring is configurable, so my explanation is just going to cover the most basic scenario. If somebody finds a mistake please tell and I will fix it.

对于路径,可能有助于逐步思考问题。

For the paths, it might help to think of things step by step.


  1. 您从浏览器请求URL,浏览器查看协议,主机和端口,并使用DNS查找相应的要连接的IP地址。

  2. 在浏览器和主机之间建立连接。主机查找在您指定的端口上运行的进程,如果任何安全系统允许TCP连接,请求将流式传输到该端口(Web服务器)上运行的进程。

  3. Web服务器根据端口之后的内容做出决策,具体来说,它通过查看给定的路径来确定Web应用程序上下文的内容。一旦确定了应用程序上下文根,它就知道哪个Web应用程序应该处理该请求。该决定基于您如何配置Web服务器,您可以让Web应用程序处理没有​​上下文根的请求或特定的上下文根。例如,如果您请求 localhost:8080 / CtxtRoot / jsp / ,您可以在服务器上有一个Web应用程序,其上下文根为CtxtRoot,它将处理该请求。或者,您可以拥有一个具有上下文的应用程序,它可以处理该请求。这取决于您如何配置服务器,默认情况下,Tomcat将使用war名称作为上下文根。

  4. Web应用程序接收请求。虽然它知道所请求的完整URL,但它只根据上下文根之后的所有内容做出决策。因此,例如,对于 localhost:8080 / CtxtRoot / jsp / 的请求,Web应用程序将基于'jsp'作为路径路由事物。

  5. Web应用程序有一个过滤器链,它首先提交请求。如果过滤器的模式与请求匹配,则该过滤器可以评估请求。它可能会阻止请求,处理请求或传递请求。我不会说太多,因为你的问题不涉及过滤器。

  6. Web应用程序查找模式与请求匹配的资源,首先考虑servlet,然后是静态资源。在上下文之后出现的url部分是它尝试匹配的部分,因此如果请求是针对 localhost:8080 / CtxtRoot / jsp / ,则上下文根是'CtxtRoot ',然后Web应用程序将'/ jsp /'与所有servlet映射进行比较。对WEB-INF中的静态资源的请求将始终被拒绝,但servlet和过滤器可以并且确实从WEB-INF返回数据。

  7. 我将继续假设请求被发送到Spring DispatcherServlet,它接收请求,并考虑servlet路径之后的所有内容。 Spring的DispatcherServlet查找一个Controller,其路径与servlet路径之后的路径匹配。 servlet路径基本上就是你在web xml中的servlet映射中放置的内容。让我举一个例子,假设您有一个Web应用程序,其上下文为app,它有一个Spring MVC servlet,其servlet映射为'/ mvc',一个控制器处理路径'sales',那么你可以使用 http:// localhost:8080 / app / mvc / sales 访问该控制器。

  8. 如果DispatcherServlet找不到Controller,我相信它会把传入的请求看作是控制器返回的,所以如果子路径是'sales',那么它会通过作为视图解析器的参数。如果找不到它,则服务器返回一个未找到的错误。

  9. 通常,Controller在完成时返回一个字符串,这是资源的路径。它可以将流行作为字符串返回。然后Spring将它转发给ViewResolver,我假设你正在使用InternalResourceViewResolver。它将查看前缀和后缀,并基本上将它们包含在给定的内容中。因此,如果前缀是'/ WEB-INF / views /',后缀是'.jsp',参数是'热门',那么它将在'/WEB-INF/views/popular.jsp中查找资源。它实际上只是将这些字符串连接起来构成一条路径。该路径始终相对于Web应用程序根目录。如果生成的路径是jsp文件,它将在返回之前被解释。

  10. 然后它最终返回给用户。

  1. You request the url from your browser, The browser looks at the protocol, host, and port, and uses a DNS to find the appropriate IP address to connect with.
  2. A connection is established between your browser and the host. The host looks for a process running on the port you specified, and if a TCP connection is allowed by any security systems in place, the request is streamed to the process running on that port, the web server.
  3. The web server makes decisions based on what's after the port, specifically, it determines what the web application context is by looking at the path it was given. Once it determines the application context root, it knows which web application should handle that request. The decision is based on how you configure the web server, you could have a web application handle requests with no context root, or a specific context root. For example, if you requested localhost:8080/CtxtRoot/jsp/, you could have one web application on the server whose context root is "CtxtRoot", and it would handle that request. Alternatively, you could have an application that has "" for a context, and it could handle that request. It depends on how you configure the server, by default Tomcat will use the war name as a context root.
  4. The web application receives the request. While it knows the full URL requested, it only makes decisions based on everything after the context root. So for example, with the request to localhost:8080/CtxtRoot/jsp/, the web application would route things based on 'jsp' as the path.
  5. The web application has a filter chain that it submits the request to first. If a filter's pattern matches the request, that filter can evaluate the request. It might block the request, handle the request, or pass it on. I won't say much more because your question doesn't involve filters.
  6. The web app looks for a resource whose pattern matches the request, it considers servlets first, and then static resources. The url part that comes AFTER the context is what it tries to match, so if the request was for localhost:8080/CtxtRoot/jsp/, and the context root was 'CtxtRoot', then the web application is comparing '/jsp/' to all of the servlet mappings. Requests for static resources in WEB-INF will always be refused, but servlets and filters can and do return data from WEB-INF.
  7. I'm going to proceed assuming the request was sent to the spring DispatcherServlet, it receives the request, and considers everything after the servlet path. Spring's DispatcherServlet looks for a Controller whose path matches the path after the servlet path. The servlet path is basically what you put in the servlet mapping in your web xml. Let me give an example, let's say you have a web app whose context is 'app', and it has a spring MVC servlet whose servlet mapping is '/mvc', and a controller that handles the path 'sales', then you could reach that controller with http://localhost:8080/app/mvc/sales.
  8. If the DispatcherServlet cannot find a Controller, I believe it treats the incoming request as if it was returned by a controller, so if the sub-path is 'sales', then it would pass that as an argument to the view resolver. If it can't find it, then the server returns a not found error.
  9. Typically the Controller returns a string when it's done, which is the path to a resource. It could return 'popular' as a string. Spring then forwards this to the ViewResolver, and I will assume you're using the InternalResourceViewResolver. It will look at the prefix and suffix, and basically wrap those around what it was given. So if the prefix is '/WEB-INF/views/', the suffix is '.jsp', and the argument is 'popular', then it will look for a resource at '/WEB-INF/views/popular.jsp'. It is literally just concatenating those strings to make a path. The path is ALWAYS relative to the web application root here. If the path produced is a jsp file, it will be interpreted before being returned.
  10. Then it is finally returned to the user.

从你的例子中你要求localhost:8080 / ContextRoot / jsp / fileName,所以看起来'CtxRoot'是上下文根,你的servlet的路径是'/',所以它应该通过无论是那之后到控制器。当DispatcherServlet收到请求时,它正在搜索将'jsp'作为路径处理的控制器。由于你没有,它决定将其视为资源路径。它使用了视图解析器并形成了路径/WEB-INF/jsp/jsp/fileName.jsp,这显然不存在。

From your example you were requesting localhost:8080/ContextRoot/jsp/fileName, so it looks like 'CtxRoot' is the context root, your servlet's path is '/', so it should pass whatever is after that to a controller. By the time the DispatcherServlet receives the request, it is searching for a controller that handles 'jsp' as a path. Since you had none, it decided to treat that as a resource path. It used the view resolver and formed the path /WEB-INF/jsp/jsp/fileName.jsp, which obviously does not exist.

我们假设您已经请求了localhost:8080 / ContextRoot / fileName,请求将到达DispatcherServlet,它将找不到将'fileName'作为路径处理的Controller,因此会将其视为资源。它将形成路径/WEB-INF/jsp/fileName.jsp,这将返回结果。

Let's assume that you had instead requested localhost:8080/ContextRoot/fileName, the request would reach the DispatcherServlet, it would find no Controller that handles 'fileName' as a path, and hence would treat it as a resource. It would form the path /WEB-INF/jsp/fileName.jsp, and that would return the result.

但是,您的Web xml未配置为初始化spring。因此,您的Web应用程序实际上正在处理您的每个请求,就好像它们是针对Web应用程序根目录的资源一样。我相信,如果你已经正确地初始化了Spring的请求,它可能会有效。

However, your web xml was not configured to initialize spring. So your web application was actually treating every one of your requests as if they were for a resource relative to the web application root. I believe that if you had made that request with Spring properly initialized, it might have worked.

这是一个很好的例子:

http:// www.mkyong.com/spring3/spring-3-mvc-hello-world-example/

注意他的web xml有一个ContextLoaderListener,它是在你的网站中评论,这对于在网络应用中初始化spring非常重要。我还在调度程序中看到了路径/ src / main / resources的注释,但是web xml中的所有路径都应该相对于Web应用程序根目录。在运行时,Web服务器不了解您的项目,并且'src'不是Web应用程序根目录中的目录。另请注意,您的MVC内容可能与主弹簧上下文不同,这很常见。

notice that his web xml has a ContextLoaderListener, which is commented out in yours, and it's essential for initializing spring in a web app. I also see the comment in your dispatcher with the path /src/main/resources, but all paths in web xml are supposed to be relative to the web application root. At runtime, the web server has no knowledge of your project, and 'src' is not a directory in your web app's root. Also note that you can have a different application context for your MVC stuff than your main spring context, and this is common.

我认为如果你做这些事情它会起作用:

I think if you do these things it will work:


  1. 将您的jsp移动到/WEB-INF/jsp/fileName.jsp

  2. 更新您的app context,'/ WEB-INF / jsp /'是前缀,'。jsp'是后缀。

  3. 将上下文加载器侦听器添加到web xml,并设置contextConfigLocation相对于应用程序上下文根的路径。例如,它可能是/WEB-INF/appContext.xml

  4. 发出请求

  1. Move your jsp to /WEB-INF/jsp/fileName.jsp
  2. Update your app context so '/WEB-INF/jsp/' is the prefix, and '.jsp' is the suffix.
  3. Add the context loader listener to your web xml, and set the contextConfigLocation path relative to the app context root. For example, it could be /WEB-INF/appContext.xml
  4. make a request to

localhost:8080 / CtxtRoot / fileName

localhost:8080/CtxtRoot/fileName

另外,你一直在谈论欢迎文件,但是你提供了资源的完整路径。欢迎文件仅在用户向目录的根目录发出请求时才会起作用,如下所示:

Also, you kept talking about the welcome-file, but you were giving full paths to resources. The welcome file only comes into play if the user makes a request to the root of a directory, like this:

localhost:8080/CtxtRoot/

该请求将被转发到welcome-file。我认为你唯一一次尝试过,jsp碰巧在你的应用程序根目录中,并被配置为欢迎文件,所以它有效。虽然它有效,但它实际上并没有使用spring来返回它。

That request would be forwarded to the welcome-file. I think the only time you tried that, the jsp happened to be in your app's root, and was configured as the welcome file, so it worked. While it "worked", it did NOT actually use spring to return that.

祝你好运。

这篇关于为什么WEB-INF文件夹里面的jsp文件有效,但放在WEB-INF下的文件夹下不行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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