org.springframework.web.servlet.PageNotFound - 没有为带有 URI 的 HTTP 请求找到映射
Posted
技术标签:
【中文标题】org.springframework.web.servlet.PageNotFound - 没有为带有 URI 的 HTTP 请求找到映射【英文标题】:org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI 【发布时间】:2013-05-11 19:15:59 【问题描述】:嗨,我是新的 Spring Mvc。
我无法解决这个问题
Dispatcher servlet (servlet-context.xml)
<?xml version="1.0" encoding="UTF-8"?>
http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the $webappRoot/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<context:component-scan base-package="com.projects.model" />
<beans:bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<beans:property name="driverClassName" value="com.mysql.jdbc.Driver" />
<beans:property name="url" value="jdbc:mysql://localhost:3306/customerdb" />
<beans:property name="username" value="root" />
<beans:property name="password" value="root" />
</beans:bean>
<beans:bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<beans:property name="dataSource" ref="dataSource" />
<beans:property name="packagesToScan" value="com.projects.model" />
<beans:property name="hibernateProperties">
<beans:props>
<beans:prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</beans:prop>
<beans:prop key="hibernate.show_sql">true</beans:prop>
</beans:props>
</beans:property>
</beans:bean>
<beans:bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<beans:property name="sessionFactory" ref="sessionFactory" />
</beans:bean>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<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">
<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
控制器类
package com.myprojects.controller;
import java.util.Locale;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import com.projects.model.CustomerDao;
import com.projects.model.Customer;
@Controller
public class HomeController
private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
/**
* Simply selects the home view to render by returning its name.
*/
private CustomerDao dao;
@RequestMapping(value = "/", method = RequestMethod.GET)
public String customer(Locale locale, Model model)
logger.info("Welcome home! The client locale is .", locale);
return "home";
@RequestMapping(value = "/", method = RequestMethod.POST)
public String addCustomer(@ModelAttribute("customer") Customer customer, BindingResult result)
dao.save(customer);
return "home";
查看页面(home.jsp)
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="false" %>
<html>
<head>
<title>Home</title>
</head>
<body>
<P> The time on the server is $serverTime. </P> --%>
<form action="addCustomer">
Cust_Id :
<input type="text">
<br>
Name :
<input type="text">
<br>
Age :
<input type="text">
<input type="submit" value="Save">
</form>
</body>
</html>
请帮我解决这个问题。
按下保存按钮后,我会调用以下网址
http://localhost:8080/model/customer
它会抛出上述错误。
【问题讨论】:
【参考方案1】:您的两个方法映射到/
。但是你的网址是/customer
。
而且,这没有意义:
<form action="customer" action="addCustomer">
一个表单只能有一个动作。如果要调用方法addCustomer,应该是
<form action="<c:url value='/'/>" method="post">
【讨论】:
【参考方案2】:您也可以使用 spring taglib 来帮助您生成 URL。
例子:
<spring:url value="/model/customer" var="url" />
<a href="$url">Link</a>
【讨论】:
【参考方案3】:您的组件扫描标记扫描“com.projects.model”包但您的控制器在“com.myprojects.controller”包中更改组件扫描 来自
<context:component-scan base-package="com.projects.model" />
到
<context:component-scan base-package="com.myprojects.controller"/>
那就试试吧。
【讨论】:
【参考方案4】:您的组件扫描标签扫描“com.projects.model”不正确。正确的应该是“com.myprojects”。
<context:component-scan base-package="com.myprojects.model" />
因此将您的组件扫描更改为:
<context:component-scan base-package="com.myprojects" />
在组件扫描中,我们放置所有其他文件夹存在的文件夹路径,即控制器、服务、存储库等。
【讨论】:
以上是关于org.springframework.web.servlet.PageNotFound - 没有为带有 URI 的 HTTP 请求找到映射的主要内容,如果未能解决你的问题,请参考以下文章