org.springframework.web.servlet.PageNotFound noHandlerFound-未找到具有URI的HTTP请求的映射
问题内容:
我知道有上千个这样的问题,但是我是盲目的,找不到错误。对于.jsp页面的此设置,一切正常(指向http:// localhost:8082 /
spring
/后,
令人惊讶的是,http://localhost:8082/spring/WEB- INF/spring/static/index.jsp
页面未加载后)。我是新手,所以可能我误会了我在做什么/
当我切换到<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:
<?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>
家用控制器:
@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合并(请参阅conf/web.xml
Tomcat的目录)。您可以在其中找到以形式的URL
*.jsp
映射到JSP Servlet。
Java Web Apps按照servlet的方式工作(您将它们映射到web.xml中的URL)。例如,您映射DispatchServlet
到的URL
/
。这个分派servlet“神奇地”与Spring一起工作并加载您的控制器-并“在内部”处理它们的映射。那就是为什么你使用过@RequestMapping(value="/")
。
在控制器内部,您基本上会命令将请求分派到具有名称的视图index
-然后解析该名称InternalResourceViewResolver
以形成/WEB- INF/spring/static/
+ index
+ 的路径.jsp
-就像您在上下文的描述符文件中配置的一样。
基本上,这发出一个内部调度请求(不完全是,但您现在可以这样考虑)-然后在第一个请求时使用完全相同的规则对其进行处理-例如,被*.jsp
servlet
捕获然后进行处理。但是,无论如何/WEB-INF/spring/static/index.html
都无法处理类似的请求-找不到匹配的规则。
要回答您的问题-您可以将任何HTML放在JSP文件中-使用任何非必需的JSP标签。但是,如果您想真正地使用纯HTML文件而不进行任何分析-
然后考虑使用静态资源-您已经在其中进行了设置<mvc:resources mapping="/resources/**" location="/resources/" />
-这基本上会将/ resources /目录的内容暴露给世界,而无需进行任何处理。