Java org.springframework.web.servlet.PageNotFound noHandlerFound - 没有找到带有 URI 的 HTTP 请求的映射
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29053410/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
org.springframework.web.servlet.PageNotFound noHandlerFound - No mapping found for HTTP request with URI
提问by Piotr M
I know there were a thousand questions like this, but I m blind and can not find error. For this setup for .jsp page everything works fine (after pointing http://localhost:8082/spring/Suprisingly, after http://localhost:8082/spring/WEB-INF/spring/static/index.jsp
page is not loading). I'm new in this, so probably I'm missunderstanding what I'm doing/
我知道有一千个这样的问题,但我是盲人,找不到错误。对于 .jsp 页面的此设置,一切正常(在指向http://localhost:8082/spring/之后,令人惊讶的是,http://localhost:8082/spring/WEB-INF/spring/static/index.jsp
页面未加载后)。我是新手,所以可能我误解了我在做什么/
When I switch to <property name="suffix" value=".html" />
(I have 2 files index.jsp and index.html) I can not load page neither from http://localhost:8082/spring/
or this http://localhost:8082/spring/WEB-INF/spring/static/index.html
link.
当我切换到<property name="suffix" value=".html" />
(我有 2 个文件 index.jsp 和 index.html)时,我无法从http://localhost:8082/spring/
或此http://localhost:8082/spring/WEB-INF/spring/static/index.html
链接加载页面。
mar 14, 2015 8:38:07 PM org.springframework.web.servlet.PageNotFound noHandlerFound
WARNING: No mapping found for HTTP request with URI [/spring/WEB-INF/spring/static/index.html] in DispatcherServlet with name 'appServlet'
servlet-context.xml:
servlet-context.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC @Controller programming model -->
<mvc:annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<mvc:resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/spring/static/" />
<property name="suffix" value=".html" />
</bean>
<context:component-scan base-package="net.codejava.spring" />
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/usersdb"/>
<property name="username" value="root"/>
<property name="password" value="1234"/>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:hibernate.cfg.xml" />
</bean>
<tx:annotation-driven />
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="userDao" class="net.codejava.spring.dao.UserDAOImpl">
<constructor-arg>
<ref bean="sessionFactory" />
</constructor-arg>
</bean>
</beans>
HomeController:
家庭控制器:
@Controller
public class HomeController {
@Autowired
private UserDAO userDao;
@RequestMapping(value="/")
public ModelAndView home() {
List<User> listUsers = userDao.list();
//ModelAndView model = new ModelAndView("home");
ModelAndView model = new ModelAndView("index");
model.addObject("userList", listUsers);
return model;
}
}
web.xml:
网页.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<!-- The definition of the Root Spring Container shared by all Servlets
and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
采纳答案by M4ks
First of all - your web.xml is more than you just put in your file. For example in Tomcat, your web.xml file gets merged with Tomcat default web.xml (see conf/web.xml
in Tomcat's dir). There you can find, that URLs in form of *.jsp
are mapped to JSP Servlet.
首先 - 您的 web.xml 不仅仅是您放入文件中的内容。例如,在 Tomcat 中,您的 web.xml 文件与 Tomcat 默认的 web.xml 合并(参见conf/web.xml
Tomcat 的目录)。在那里您可以发现,形式为 的 URL*.jsp
被映射到 JSP Servlet。
The Java Web Apps work in terms of servlets (you map them to URLs in web.xml). For example you mapped DispatchServlet
to an URL of /
.
This dispatch servlet "magically" works with Spring and loads your controllers - and process their mappings "inside". Thats why you used @RequestMapping(value="/")
.
Java Web 应用程序根据 servlet 工作(您将它们映射到 web.xml 中的 URL)。例如,您映射DispatchServlet
到/
. 这个调度servlet“神奇地”与Spring一起工作并加载你的控制器——并在“内部”处理它们的映射。这就是为什么你使用@RequestMapping(value="/")
.
Inside the controller you basically commands to dispatch your request to a view with name index
- which then gets resolved by InternalResourceViewResolver
to form a path of /WEB-INF/spring/static/
+ index
+ .jsp
- just like you configured in context's descriptor file.
在控制器内部,你基本上命令,派遣你的要求与名称的视图index
-然后把它通过解决InternalResourceViewResolver
,形成了一种路径/WEB-INF/spring/static/
+ index
+ .jsp
-就像你在上下文中的描述符文件进行配置。
This, basically, issues an internal dispatch request (not exactly, but you can think of it this way for now) - which then gets handled using exactly the same rules at the first request - for example being caught by *.jsp
servlet and then processed. However, in no way the request like /WEB-INF/spring/static/index.html
can be processed - no rules matching can be found.
这基本上会发出一个内部调度请求(不完全是,但您现在可以这样想) - 然后在第一个请求时使用完全相同的规则进行处理 - 例如被*.jsp
servlet捕获然后处理。但是,无论如何/WEB-INF/spring/static/index.html
都无法处理类似的请求- 找不到匹配的规则。
To answer your question - you can put any HTML in the JSP files - using any JSP tags its not mandatory.
If you, however, want to really use plain HTML files without any parsing - then think of using static resources - you already set it up in <mvc:resources mapping="/resources/**" location="/resources/" />
- this basically exposes content of your /resources/ directory to the world without any processing.
要回答您的问题 - 您可以将任何 HTML 放入 JSP 文件中 - 使用任何 JSP 标记都不是强制性的。但是,如果您想真正使用纯 HTML 文件而不进行任何解析 - 那么考虑使用静态资源 - 您已经将其设置<mvc:resources mapping="/resources/**" location="/resources/" />
- 这基本上将您的 /resources/ 目录的内容暴露给世界而无需任何处理。
回答by Master Slave
There are a few things to understand to realize where the problem is
有几件事需要了解才能意识到问题出在哪里
Firstly, you cannot access directly the pages under WEB-INFthat is why your JSPs are not rendered when you directly attempt the http://localhost:8082/spring/WEB-INF/spring/static/index.jsp
首先,你不能直接访问WEB-INF下的页面,这就是为什么当你直接尝试 http://localhost:8082/spring/WEB-INF/spring/static/index.jsp
For what concerns the html files, the problem is that when you hit the contoller a view name is constructed and used in the request forward. Your view name will be /WEB-INF/spring/static/index.html
and it will search for a servlet that should process the request with this extension and it will fail (if it were to be .jsp it would be process by JSPServlet). As no servlet will be able to handle the request, a default servlet will kick-in and try to find a controller method to handle it and will fail with the message no mapping found for /WEB-INF/spring/static/index.html
对于 html 文件,问题在于当您点击控制器时,会构造一个视图名称并在请求转发中使用。您的视图名称将是/WEB-INF/spring/static/index.html
,它将搜索应该使用此扩展名处理请求的 servlet,并且它将失败(如果它是 .jsp,它将由 JSPServlet 处理)。由于没有 servlet 能够处理请求,默认 servlet 将启动并尝试找到一个控制器方法来处理它,但会失败并显示消息no mapping found for /WEB-INF/spring/static/index.html
Your best bet is to configure the view results of your controllers as JSP. Otherwise, there's the configuration that seem to worked for the following answeror try to register html files to be handled by the JSP servlet, as explained here
最好的办法是将控制器的视图结果配置为 JSP。否则,有似乎适用于以下答案的配置或尝试注册由 JSP servlet 处理的 html 文件,如解释here