gpt4 book ai didi

java - 在 DispatcherServlet 中未找到名称为 'offers' 的 URI [/offers/] 的 HTTP 请求的映射

转载 作者:太空宇宙 更新时间:2023-11-04 13:56:19 25 4
gpt4 key购买 nike

我正在学习 spring 并陷入了这个错误。我已经清理了项目,运行了 Maven build,它运行了构建成功并刷新了目标,但我仍然没有运气。这是代码:

错误

Apr 21, 2015 8:49:30 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:Spring_Project' did not find a matching property.
Apr 21, 2015 8:49:30 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server version: Apache Tomcat/8.0.15
Apr 21, 2015 8:49:30 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server built: Nov 2 2014 19:25:20 UTC
Apr 21, 2015 8:49:30 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server number: 8.0.15.0
Apr 21, 2015 8:49:30 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Name: Mac OS X
Apr 21, 2015 8:49:30 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Version: 10.10.3
Apr 21, 2015 8:49:30 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Architecture: x86_64
Apr 21, 2015 8:49:30 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JAVA_HOME: /Library/Java/JavaVirtualMachines/jdk1.8.0_31.jdk/Contents/Home/jre
Apr 21, 2015 8:49:30 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Version: 1.8.0_31-b13
Apr 21, 2015 8:49:30 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Vendor: Oracle Corporation
Apr 21, 2015 8:49:30 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_BASE: /Users/DrewJocham/Documents/practice/.metadata/.plugins/org.eclipse.wst.server.core/tmp0
Apr 21, 2015 8:49:30 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_HOME: /Library/apache-tomcat-8.0.15
Apr 21, 2015 8:49:30 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.base=/Users/DrewJocham/Documents/practice/.metadata/.plugins/org.eclipse.wst.server.core/tmp0
Apr 21, 2015 8:49:30 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.home=/Library/apache-tomcat-8.0.15
Apr 21, 2015 8:49:30 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dwtp.deploy=/Users/DrewJocham/Documents/practice/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps
Apr 21, 2015 8:49:30 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Djava.endorsed.dirs=/Library/apache-tomcat-8.0.15/endorsed
Apr 21, 2015 8:49:30 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dfile.encoding=UTF-8
Apr 21, 2015 8:49:30 PM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: /Users/DrewJocham/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.
Apr 21, 2015 8:49:30 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-nio-8080"]
Apr 21, 2015 8:49:30 PM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFO: Using a shared selector for servlet write/read
Apr 21, 2015 8:49:30 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-nio-8009"]
Apr 21, 2015 8:49:30 PM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFO: Using a shared selector for servlet write/read
Apr 21, 2015 8:49:30 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 1204 ms
Apr 21, 2015 8:49:30 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Apr 21, 2015 8:49:30 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/8.0.15
Apr 21, 2015 8:49:32 PM org.apache.catalina.core.ApplicationContext log
INFO: No Spring WebApplicationInitializer types detected on classpath
Apr 21, 2015 8:49:32 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring root WebApplicationContext
Apr 21, 2015 8:49:32 PM org.springframework.web.context.ContextLoader initWebApplicationContext
INFO: Root WebApplicationContext: initialization started
Apr 21, 2015 8:49:32 PM org.springframework.web.context.support.XmlWebApplicationContext prepareRefresh
INFO: Refreshing Root WebApplicationContext: startup date [Tue Apr 21 20:49:32 CEST 2015]; root of context hierarchy
Apr 21, 2015 8:49:32 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [com/learnspring/config/daoContext.xml]
Apr 21, 2015 8:49:33 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [com/learnspring/config/offersService.xml]
Apr 21, 2015 8:49:33 PM org.springframework.web.context.ContextLoader initWebApplicationContext
INFO: Root WebApplicationContext: initialization completed in 861 ms
Apr 21, 2015 8:49:33 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring FrameworkServlet 'offers'
Apr 21, 2015 8:49:33 PM org.springframework.web.servlet.DispatcherServlet initServletBean
INFO: FrameworkServlet 'offers': initialization started
Apr 21, 2015 8:49:33 PM org.springframework.web.context.support.XmlWebApplicationContext prepareRefresh
INFO: Refreshing WebApplicationContext for namespace 'offers-servlet': startup date [Tue Apr 21 20:49:33 CEST 2015]; parent: Root WebApplicationContext
Apr 21, 2015 8:49:33 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from ServletContext resource [/WEB-INF/offers-servlet.xml]
Apr 21, 2015 8:49:34 PM org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter initControllerAdviceCache
INFO: Looking for @ControllerAdvice: WebApplicationContext for namespace 'offers-servlet': startup date [Tue Apr 21 20:49:33 CEST 2015]; parent: Root WebApplicationContext
Apr 21, 2015 8:49:34 PM org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter initControllerAdviceCache
INFO: Looking for @ControllerAdvice: WebApplicationContext for namespace 'offers-servlet': startup date [Tue Apr 21 20:49:33 CEST 2015]; parent: Root WebApplicationContext
Apr 21, 2015 8:49:34 PM org.springframework.web.servlet.DispatcherServlet initServletBean
INFO: FrameworkServlet 'offers': initialization completed in 1209 ms
Apr 21, 2015 8:49:34 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-nio-8080"]
Apr 21, 2015 8:49:34 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-nio-8009"]
Apr 21, 2015 8:49:34 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 4187 ms
Apr 21, 2015 8:49:35 PM org.springframework.web.servlet.PageNotFound noHandlerFound
WARNING: No mapping found for HTTP request with URI [/offers/] in DispatcherServlet with name 'offers'

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>Spring_Project</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>

<servlet>
<description>offers</description>
<display-name>offers</display-name>
<servlet-name>offers</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>offers</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

<description>Database</description>
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/Spring_Tutorial</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:com/learnspring/config/daoContext.xml
classpath:com/learnspring/config/offersService.xml
</param-value>
</context-param>

daoContext.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:jee="http://www.springframework.org/schema/jee"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.1.xsd
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-4.1.xsd">



<context:annotation-config></context:annotation-config>
<context:component-scan
base-package="com.learnspring.datasource.DAO">
</context:component-scan>
<jee:jndi-lookup jndi-name="jdbc/Spring_Tutorial" id="dataSource"
expected-type="javax.sql.DataSource">
</jee:jndi-lookup>
</beans>

offersService.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:context="http://www.springframework.org/schema/context"
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-4.1.xsd">


<context:annotation-config></context:annotation-config>
<context:component-scan base-package="com.learnspring.service">
</context:component-scan>
</beans>

OffersController.java

package com.learnspring.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.learnspring.datasource.DAO.Offer;
import com.learnspring.service.OffersService;

@Controller
public class OffersController {

private OffersService offersService;

//@Autowired
public void setOffersService(OffersService offersService) {
this.offersService = offersService;
}

@RequestMapping("/")
public String getHome(Model model) {

List<Offer> offers = offersService.getCurrent();

model.addAttribute("offers", offers);
return "home";
}

}

OffersService.java

package com.learnspring.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.learnspring.datasource.DAO.Offer;
import com.learnspring.datasource.DAO.OfferDAO;

@Service("offersService")
public class OffersService {

private OfferDAO offerDAO;

@Autowired
public void setOfferDAO(OfferDAO offerDAO) {
this.offerDAO = offerDAO;
}

public List<Offer> getCurrent() {

return offerDAO.getOffers();
}

}

最佳答案

看起来您正在尝试通过执行以下操作在 web.xml 上注册 Controller :

<servlet-mapping>
<servlet-name>offers</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

我认为你不需要这样做。您只需注册 RequestDispatcher 即可,然后正如 Patrick LC 提到的那样,

<context:component-scan base-package="com.learnspring.controller">

请扫描您的代码以供 Controller 注册。然后,只需将请求映射设置为您想要使用的任何内容即可。下面我使用 home.spr,其中 spr 代表 spring,但这只是我使用的约定;重要的是,无论您选择什么,都需要在 web.xml 上声明,如下所示:

<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.spr</url-pattern>
</servlet-mapping>

然后在你的 Controller 上执行:

@RequestMapping("/orders/home.spr")
public String getHome(Model model) {

List<Offer> offers = offersService.getCurrent();

model.addAttribute("offers", offers);
return "home";
}

哦...您可能想尝试一下 spring.io,它使用起来更简单一些。

关于java - 在 DispatcherServlet 中未找到名称为 'offers' 的 URI [/offers/] 的 HTTP 请求的映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29780900/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com