【问题标题】:org.springframework.web.servlet.PageNotFound noHandlerFound - No mapping found for HTTP request with URIorg.springframework.web.servlet.PageNotFound noHandlerFound - 没有为带有 URI 的 HTTP 请求找到映射
【发布时间】:2015-03-14 19:46:38
【问题描述】:

我知道有一千个这样的问题,但我是瞎子,找不到错误。对于 .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>

【问题讨论】:

  • 你的 servlet-context.xml 中有 &lt;mvc:annotation-driven/&gt; 吗?
  • 是的,我有。现在我更新了 servlet-context.xml,之前我无法发布,我知道为什么。感谢您的关注。
  • 您的网络环境是什么,您访问的网址是什么?
  • 所以您请求http://localhost:8080/spring/static/index.html 并期望HomeController.home() 被调用?
  • 网址:localhost:8082/spring。我不明白有关网络上下文的问题。另外,我尝试将 @RequestMapping(value="/") 更改为 value="/spring"。没用。

标签: java spring spring-mvc


【解决方案1】:

要了解问题出在哪里,需要了解一些事情

首先,您无法直接访问 WEB-INF 下的页面,这就是为什么当您直接尝试 http://localhost:8082/spring/WEB-INF/spring/static/index.jsp 时不会呈现您的 JSP 的原因

对于 html 文件,问题是当您点击控制器时,会构建视图名称并在请求转发中使用。您的视图名称将是/WEB-INF/spring/static/index.html,它将搜索应该处理具有此扩展名的请求的servlet,并且它将失败(如果它是.jsp,它将由JSPServlet 处理)。由于没有 servlet 能够处理该请求,默认的 servlet 将启动并尝试找到一个控制器方法来处理它,并将失败并显示消息 no mapping found for /WEB-INF/spring/static/index.html

最好的办法是将控制器的视图结果配置为 JSP。否则,配置似乎适用于以下 answer 或尝试注册要由 JSP servlet 处理的 html 文件,如 here 所述

【讨论】:

    【解决方案2】:

    首先 - 您的 web.xml 不仅仅是您放入文件中的内容。例如,在 Tomcat 中,您的 web.xml 文件将与 Tomcat 默认 web.xml 合并(请参阅 Tomcat 目录中的 conf/web.xml)。在那里你可以发现,*.jsp 形式的 URL 被映射到 JSP Servlet。

    Java Web 应用程序以 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 文件而不进行任何解析 - 然后考虑使用静态资源 - 您已经在 &lt;mvc:resources mapping="/resources/**" location="/resources/" /&gt; 中设置了它 - 这基本上将 /resources/ 目录的内容公开给世界而无需任何处理。

    【讨论】:

    • mvc:resources 和正确的映射资源 - 这是解决这个问题的正确途径。谢谢!
    猜你喜欢
    • 2015-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-04
    • 1970-01-01
    • 1970-01-01
    • 2011-12-08
    相关资源
    最近更新 更多