Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在名为';提供';_Java_Spring_Maven - Fatal编程技术网

Java 在名为';提供';

Java 在名为';提供';,java,spring,maven,Java,Spring,Maven,我正在学习spring,并坚持这个错误。我清理了项目,运行了maven build,它运行build success并刷新了目标,但我仍然没有运气。代码如下: 错误 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:sc

我正在学习spring,并坚持这个错误。我清理了项目,运行了maven build,它运行build success并刷新了目标,但我仍然没有运气。代码如下:

错误

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>

春晚计划
index.html
index.htm
index.jsp
default.html
default.htm
default.jsp
提供
提供
提供
org.springframework.web.servlet.DispatcherServlet
1.
提供
/
数据库
数据库连接
jdbc/Spring_教程
javax.sql.DataSource
容器
org.springframework.web.context.ContextLoaderListener
上下文配置位置
类路径:com/learnspring/config/daoContext.xml
类路径:com/learnspring/config/offersService.xml

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

}
package com.learnspring.controller;
导入java.util.List;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.stereotype.Controller;
导入org.springframework.ui.Model;
导入org.springframework.web.bind.annotation.RequestMapping;
导入com.learnspring.datasource.DAO.Offer;
导入com.learnspring.service.OffersService;
@控制器
公共类报价控制器{
私人服务;
//@自动连线
公共无效setOffersService(OffersService OffersService){
this.offersService=offersService;
}
@请求映射(“/”)
公共字符串getHome(模型){
List offers=offersService.getCurrent();
model.addAttribute(“报价”,报价);
返回“家”;
}
}
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();
    }

}
package com.learnspring.service;
导入java.util.List;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.stereotype.Service;
导入com.learnspring.datasource.DAO.Offer;
导入com.learnspring.datasource.DAO.OfferDAO;
@服务(“要约人服务”)
公共类报价服务{
私人奥弗道奥弗道;
@自动连线
公共无效设置为OfferDAO(OfferDAO OfferDAO){
this.offerDAO=offerDAO;
}
公共列表getCurrent(){
return offerDAO.getOffers();
}
}

看起来您正试图通过以下操作在web.xml上注册控制器:

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

提供
/
在我看来,你不需要那样做。您只需注册RequestDispatcher就可以了,正如Patrick LC所提到的,您可以

<context:component-scan base-package="com.learnspring.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>

调度员
org.springframework.web.servlet.DispatcherServlet
2.
调度员
*.spr
然后在控制器上执行以下操作:

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

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

    model.addAttribute("offers", offers);
    return "home";
}
@RequestMapping(“/orders/home.spr”)
公共字符串getHome(模型){
List offers=offersService.getCurrent();
model.addAttribute(“报价”,报价);
返回“家”;
}

哦……您可能想试试spring.io,它的使用更简单。

您的上下文路径是什么?您是否尝试过不在根(/)上映射?您的控制器没有被扫描,您需要在您的上下文配置中包含。您忘记解释为什么据称需要第二个
DispatcherServlet
,以及“*.spr”映射的含义。“OP正在努力学习,所以建议剧烈的改变而不告诉他们为什么会有帮助是没有多大帮助的。”kryger同意。见修改后的答案。
@RequestMapping("/orders/home.spr")
public String getHome(Model model) {

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

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