当前位置: 首页 > 知识库问答 >
问题:

Spring MVC控制器问题:找不到URI为的HTTP请求的映射

洪宇定
2023-03-14

是的。我知道这是一个熟悉的问题。我已经研究了其他解决方案,但它们没有帮助我。我正在尝试用Spring 4、HiberNate 5、My Sql和Angular JS 1. x构建Spring MVC应用程序

问题:如图所示,当我运行应用程序时,它按照预期index.jsp文件,然后我输入“http://localhost:8080/TimeLee/user/test”以获得网页“adduser.html”。嘣,它会抛出以下错误“在DispatcherServlet中,没有找到与名为“mvc-调度器”的URI[/TimeLee/WEB-INF/view/adduser.html的HTTP请求映射”。我检查了控制器映射,一切看起来都很好。

用户控制器:

package com.timelee.users.controllers;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.timelee.users.model.User;
import com.timelee.users.services.UserService;

@RestController
@RequestMapping("/user")
public class UserController
{
    @Autowired
    private UserService userService;

    @RequestMapping(value="/adduser",method=RequestMethod.POST)
    public void saveUser(@RequestBody User user)
    {
        userService.saveUser(user);
    }


    @RequestMapping(value="/getuser",method=RequestMethod.GET)
    public @ResponseBody User getUser(@RequestParam("userId") String userId)
    {
        return userService.getUser(userId);
    }   

    @RequestMapping(value="/test",method=RequestMethod.GET)
    public  ModelAndView test()
    {
        ModelAndView modelAndView=new ModelAndView("adduser");
        return modelAndView;
    }

    @RequestMapping(value="/test2",method=RequestMethod.GET)
    public  String test2()
    {
        return "adduser";
    }

}

网状物xml

<web-app id="WebApp_ID" version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <display-name>Spring Web 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>/WEB-INF/spring-mvc-config.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>

spring mvc配置。xml

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx 
  http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/views/</value>
        </property>
        <property name="suffix">
            <value>.html</value>
        </property>
    </bean>


    <context:property-placeholder location="classpath:database.properties" />
      <context:component-scan  base-package="com.timelee.*" />


    <tx:annotation-driven transaction-manager="transactionManager"/>


     <!--  <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">  -->

     <bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
       <property name="driverClassName" value="${jdbc.driverClassName}" />
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}"/>

    </bean>



    <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean" >
        <property name="dataSource" ref="dataSource"/>
        <property name="packagesToScan">
            <list>
                <value>com.timelee.timesheet.model</value>
                <value>com.timelee.users.model</value>
                <value>com.timelee.*</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                <prop key="hibernate.show_sql">${hibernate.show_sql:false}</prop>
                <prop key="hibernate.format_sql">${hibernate.format_sql:false}</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
            </props>
        </property>       
    </bean>

    <bean id="transactionManager"  class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <bean id="persistenceExceptionTranslationPostProcessor"
        class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>

</beans>

共有3个答案

郎宏浚
2023-03-14

首先我想知道adduser是jap还是HTML文件?如果是,则使用简单的退货。

其次是在控制器中创建adduserpost方法,对吗?在控制器中添加adduserget方法<喜欢

@requestmapping(value="adduser",requestmethod.GET)
public String adduser(){
     Return // return page name
}
朱兴学
2023-03-14

我想试着添加spring-mvc-config.xml。希望这对你有用。其他东西看起来不错。

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

下一步尝试是将这些代码添加到web的顶部。xml文件。你的情况似乎不一样。

    <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    version="3.1">

 </web-app>

如果它不为你工作,然后按照我的链接,看看区别web.xml以及application-context.xml和dispatcher-servlet.xml

楚翰
2023-03-14
 类似资料: