org.springframework.web.servlet.PageNotFound noHandlerFound - 找不到带URI的HTTP请求的映射 [英] org.springframework.web.servlet.PageNotFound noHandlerFound - No mapping found for HTTP request with URI
问题描述
我知道有这样的一千个问题,但我失明了,找不到错误。对于.jsp页面的此设置,一切正常(指向 http:// localhost:8082 / spring / 之后) , http:// localhost:8082 / spring / WEB-INF / spring / static / index.jsp
页面未加载)。我是新手,所以我可能会误解我在做什么/
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/
当我切换到< property name =后缀value =。html/>
(我有2个文件index.jsp和index.html)我无法从 http:// localhost加载页面: 8082 / spring /
或者此 http:// localhost:8082 / spring / WEB-INF / spring / static / index.html
链接。
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.
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:
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 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>
推荐答案
首先 - 你的web.xml不仅仅是你只需要放入你的文件。例如,在Tomcat中,您的web.xml文件与Tomcat默认web.xml合并(请参阅Tomcat的目录中的 conf / web.xml
)。在那里你可以发现, * .jsp
形式的URL被映射到JSP Servlet。
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.
Java Web应用程序在servlet方面工作(您将它们映射到web.xml中的URL)。例如,您将 DispatchServlet
映射到 /
的URL。
这个调度servlet神奇地与Spring一起工作并加载你的控制器 - 并在内部处理它们的映射。这就是你使用 @RequestMapping(value =/)
的原因。
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="/")
.
在控制器内你基本上命令将你的请求发送到一个名为 index
的视图 - 然后通过 InternalResourceViewResolver
形成路径 / WEB-INF / spring / static /
+ index
+ .jsp
- 就像你在上下文的描述符文件中配置一样。
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.
这,基本上发出内部调度请求(不完全如此,但你现在可以这样思考) - 然后在第一次请求时使用完全相同的规则处理它 - 例如被 *抓住.jsp
servlet然后处理。但是,无法处理像 /WEB-INF/spring/static/index.html
这样的请求 - 无法找到匹配的规则。
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文件中放置任何HTML - 使用任何不是必需的JSP标记。
但是,如果你想真正使用普通HTML文件而不进行任何解析 - 那么考虑使用静态资源 - 你已经在< mvc:resources mapping =/ resources /中设置了它**location =/ resources //>
- 这基本上会将您的/ resources /目录的内容暴露给世界,而不进行任何处理。
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.
这篇关于org.springframework.web.servlet.PageNotFound noHandlerFound - 找不到带URI的HTTP请求的映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!