2017-05-26 158 views
3

是的。我知道這是熟悉的問題。我研究了其他解決方案,但他們並沒有幫助我。我想建立的Spring MVC與Spring 4,休眠5,我的SQL和角度JS 1.x的Spring MVC控制器問題:未找到具有URI的HTTP請求的映射

問題:如圖片所示,當我運行應用程序,它要解決的index.jsp文件預計,然後我輸入'http://localhost:8080/TimeLee/user/test'來獲取網頁'adduser.html'。 Boom,它會拋出以下錯誤'在名爲'mvc-dispatcher'的DispatcherServlet中,找不到具有URI的HTTP請求的映射[/TimeLee/WEB-INF/views/adduser.html]。我檢查了控制器映射,一切看起來不錯。

Folder Structure and Error message from log

Browser Output

UserController中:

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"; 
    } 

} 

的web.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> 

彈簧-MVC-config.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> 
+0

只需添加並從Eclipse中清理項目 - > Menubar ---> Projrct --->清理並重啓項目 –

+0

我試過了。它沒有工作。我仍然看到錯誤 – John

+0

您正在處理@ RestController類的視圖,這是錯誤的,請使用「@ Controller」。 '@ RestController'註釋是'@ ResponseBody'的快捷鍵,可以在每個控制器的方法中指定。對於現在,要返回的對象將被轉換爲JSON並直接傳遞到服務器響應中,而不是查看模板查找。 – WildDev

回答

1

我想嘗試添加在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.xmldispatcher-servlet.xml

+0

沒有。它沒有幫助我。 – John

3

spring-mvc-config.xml文件添加<mvc:default-servlet-handler/>。清理項目並重建它。這可能會解決您的問題。

0

首先,我想知道adduser是一個jap或HTML文件嗎?如果是,則使用簡單的回報。

其次是你在你的控制器中創建adduser post方法,對嗎?在控制器中添加adduser獲取方法。

@requestmapping(value="adduser",requestmethod.GET) 
public String adduser(){ 
    Return // return page name 
} 
0

首先,adduser的是japHTML文件?如果HTML使用簡單的回報。

二,您是否在您的控制器中創建adduserPOST方法?在控制器中添加adduserGET方法。

像:

@requestmapping(value="adduser",requestmethod.GET) 
public String adduser(){ 
    Return // return page name 
} 

在這裏,404錯誤只能說明有一個在您webinf文件夾或者你把你所有的jap/HTML文件沒有adduser.htmladduser.jsp可用。

相關問題