【问题标题】:WARNING: No mapping found for HTTP request with URI [/projectShaun/home] in DispatcherServlet with name 'DispatcherServlet' [duplicate]警告:在名称为“DispatcherServlet”的 DispatcherServlet 中找不到具有 URI [/projectShaun/home] 的 HTTP 请求的映射 [重复]
【发布时间】:2015-07-25 18:23:17
【问题描述】:

当我启动控制台时,我没有收到任何错误,但是当我尝试访问以下网址时:http://localhost:8080/projectShaun/home 我收到以下错误:

在名称为“DispatcherServlet”的 DispatcherServlet 中找不到带有 URI [/projectShaun/] 的 HTTP 请求的映射

DispatcherServlet-servlet:

<?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:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc 
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>
</beans>

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"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>WEB-INF/applicationContext.xml</param-value>
</context-param>
    <servlet>
        <servlet-name>DispatcherServlet</servlet-name>
        <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>DispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

应用上下文:

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

  <context:component-scan base-package="com.projectShaun.controller" />

  <mvc:annotation-driven />

  <tx:annotation-driven/>
 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/projectshaun" />
    <property name="username" value="root" />
    <property name="password" value="" />
  </bean>

  <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource"></property>
     <property name="annotatedClasses">
            <list>
                <value>com.projectShaun</value>
            </list>
        </property>
    <property name="hibernateProperties">
      <props>
        <prop 
         key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
        <prop key="hibernate.show_sql">true</prop>
      </props>
    </property>
  </bean>

  <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager" 
    p:sessionFactory-ref="sessionFactory">
  </bean>
</beans>

家庭控制器:

package com.projectShaun.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.projectShaun.service.AccountService;

@Controller
public class HomeController {

    @Autowired
    AccountService accountService;

    @RequestMapping(value = "/home")
    public ModelAndView welcome() {
        ModelAndView modelAndView = new ModelAndView("welcome");
        modelAndView.addObject("greeting", "Welcome to projectShaun!");
        return modelAndView;
    }
}

【问题讨论】:

  • 在web.xml中添加&lt;display-name&gt;projectShaun&lt;/display-name&gt;
  • @RahulThachilath 添加后我仍然得到 404。

标签: java hibernate spring-mvc servlets


【解决方案1】:

WEB-INF/jsp 文件夹中有welcome.jsp 文件吗?这可能是个问题,您也可以检查您的项目上下文根是否正确。(右键单击项目名称 -> 属性 -> Web 项目设置 -> 上下文根应该是 ProjectShaun)

【讨论】:

  • 嗨 - 这可能是问题所在,但这听起来像是评论/澄清而不是答案。为了使它成为一个答案,你可以例如解释该文件是什么以及它如何导致此问题等 - 因为这可能会被删除。谢谢!
  • @Rup 我没有足够的评论声誉。更新了我的答案
  • @nileshvirkar 是的,我的welcome.jsp 在WEB-INF/jsp 文件夹中。我检查了我的上下文根目录,它是 projectShaun。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-14
  • 1970-01-01
  • 2020-05-19
  • 1970-01-01
  • 2016-12-22
  • 2015-02-18
相关资源
最近更新 更多