【问题标题】:No mapping found for HTTP request with URI [/hello] in DispatcherServlet with name 'mvc-dispatcher' [duplicate]在名称为“mvc-dispatcher”的 DispatcherServlet 中找不到具有 URI [/hello] 的 HTTP 请求的映射 [重复]
【发布时间】:2016-01-02 07:12:47
【问题描述】:

项目结构 enter image description here

web.xml

<web-app version="2.5"
         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_2_5.xsd">

    <display-name>Spring MVC Application</display-name>

    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:applicationContext.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

mvc-dispatcher-servlet.xml

<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:aop="http://www.springframework.org/schema/aop" 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.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <mvc:annotation-driven/>
    <mvc:default-servlet-handler/>

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

</beans>

applicationContext.xml

<?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:tx="http://www.springframework.org/schema/tx"
        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-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <context:annotation-config />
    <context:component-scan base-package="sse.jason"/>

    <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" >
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/library?useUnicode=true&amp;characterEncoding=UTF-8"/>
        <property name="username" value="root"/>
        <property name="password" value="root"/>
        <property name="initialSize" value="1"/>
        <property name="minIdle" value="1"/>
        <property name="maxIdle" value="30"/>
        <property name="maxActive" value="60"/>
        <property name="testOnBorrow" value="true" />
        <property name="testOnReturn" value="true" />
        <property name="testWhileIdle" value="true" />
    </bean>

    <bean id="sessionFactory"
          class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan">
            <list>
                <value>sse.jason.model</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <value>
                hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect
                hibernate.hbm2ddl.auto=update
                hibernate.show_sql=false
                hibernate.format_sql=false
            </value>
        </property>
    </bean>
    <bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>
    <tx:annotation-driven transaction-manager="txManager"/>
</beans>

HelloContoller.java

@Controller
public class HelloController {
    @RequestMapping("/")
    public String printWelcome(ModelMap model) {
       model.addAttribute("message", "Hello world!");
       return "hello";
    }

}

LoginController.java

@Controller
public class LoginController {

    @Autowired
    @Qualifier("iUserService")
    private UserService iUserService;

    @RequestMapping("/log")
    public String loginPage() {
        return "login";
    }

    @RequestMapping("/loginCheck")
    public ModelAndView login(HttpServletRequest request,LoginCommand command) {
        User user = iUserService.login(command.getUsername(),command.getPassword());
        if (user==null) {
            return new ModelAndView("error");
        } else {
            user.setLastIp(request.getRemoteAddr());
            user.setLasVisit(new Date());
            request.getSession().setAttribute("user",user);
            return new ModelAndView("index");
        }
        //return new ModelAndView("index");
    }

}

编译结果

Jan 02, 2016 12:15:38 org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping registerHandler
Info: Root mapping to handler 'helloController'
Jan 02, 2016 12:15:38  org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping registerHandler
Info: Mapped URL path [/log] onto handler 'loginController'
Jan 02, 2016 12:15:38 org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping registerHandler
Info: Mapped URL path [/log.*] onto handler 'loginController'
Jan 02, 2016 12:15:38 org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping registerHandler
Info: Mapped URL path [/log/] onto handler 'loginController'
Jan 02, 2016 12:15:38 org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping registerHandler
Info: Mapped URL path [/loginCheck] onto handler 'loginController'
Jan 02, 2016 12:15:38 org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping registerHandler
Info: Mapped URL path [/loginCheck.*] onto handler 'loginController'
Jan 02, 2016 12:15:38 org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping registerHandler
Info: Mapped URL path [/loginCheck/] onto handler 'loginController'

错误

localhost:8080  404 error

warnings: No mapping found for HTTP request with URI [/hello] in DispatcherServlet with name 'mvc-dispatcher'
Jan 02, 2016 12:15:40  org.springframework.web.servlet.PageNotFound noHandlerFound

localhost:8080/log  no response

有人可以帮我吗?非常感谢。

【问题讨论】:

  • 你在哪里注册了/hello端点的控制器?
  • 你测试的 URL 是什么,你在哪里指定 /hello 映射在控制器中?
  • 问题是什么?

标签: java spring spring-mvc servlets web


【解决方案1】:

您已将 hello.jsp 页面映射到 HelloController 中的 /,如果您希望在 /hello 上提供服务,请将其修改为以下代码

@RequestMapping("/hello")//instead of @RequestMapping("/")
public String printWelcome(ModelMap model) {
   model.addAttribute("message", "Hello world!");
   return "hello";
}

【讨论】:

    猜你喜欢
    • 2014-06-20
    • 1970-01-01
    • 2015-08-04
    • 2018-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-30
    相关资源
    最近更新 更多