是的。我知道這是熟悉的問題。我研究了其他解決方案,但他們並沒有幫助我。我想建立的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]。我檢查了控制器映射,一切看起來不錯。
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>
只需添加 並從Eclipse中清理項目 - > Menubar ---> Projrct --->清理並重啓項目 –
我試過了。它沒有工作。我仍然看到錯誤 – John
您正在處理@ RestController類的視圖,這是錯誤的,請使用「@ Controller」。 '@ RestController'註釋是'@ ResponseBody'的快捷鍵,可以在每個控制器的方法中指定。對於現在,要返回的對象將被轉換爲JSON並直接傳遞到服務器響應中,而不是查看模板查找。 – WildDev