Spring MVC错误404所需的资源不可用

时间:2018-08-02 02:45:05

标签: hibernate spring-mvc spring-data-jpa

我正在用Spring开发一个项目。我有一部分工作了,但是在创建了一些新的控制器,服务和存储库之后,所有部分(旧的和新闻的)都停止工作,并给了我这个错误。这是我拥有的基本配置

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">
    <servlet>

        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/dispatcher-servlet.xml</param-value></init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>

        <url-pattern>/</url-pattern>
    </servlet-mapping>
<!--    <session-config>  -->
<!--    <tracking-mode>COOKIE</tracking-mode>  -->
<!-- </session-config> -->
<!-- needed for ContextLoaderListener -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/root-context.xml</param-value>
    </context-param>

    <!-- Bootstraps the root web application context before servlet initialization -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
</web-app>

根上下文

<?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"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:repository="http://www.springframework.org/schema/data/repository"
    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.3.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.8.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
        http://www.springframework.org/schema/data/repository http://www.springframework.org/schema/data/repository/spring-repository-1.11.xsd">

    <context:component-scan
        base-package="net.thunderfat.app.service" />
    <jpa:repositories
        base-package="net.thunderfat.app.repository" />
    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName"
            value="com.mysql.jdbc.Driver" />
        <property name="url"
            value="jdbc:mysql://localhost:3306/thunderfat?useSSL=false" />
        <property name="username" value="root" />
        <property name="password" value="" />
    </bean>
    <bean id="jpaVendorAdapter"
        class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
        <property name="generateDdl" value="false" />
        <property name="showSql" value="true"></property>
        <property name="databasePlatform"
            value="org.hibernate.dialect.MySQL5Dialect" />
    </bean>
    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="packagesToScan"
            value="net.thunderfat.app.model" />
        <property name="dataSource" ref="dataSource" />
        <property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
    </bean>
    <bean id="transactionManager"
        class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory"
            ref="entityManagerFactory" />
    </bean>
</beans>

dispatcher-servlet

<?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"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.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.3.xsd">

    <context:component-scan
        base-package="net.thunderfat.app.controller" />
    <mvc:annotation-driven />
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/views/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>
    <mvc:resources location="/resource/**"
        mapping="/resource/"></mvc:resources>
</beans>

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>pfg</groupId>
    <artifactId>thunderfat</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.0.7.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
            <version>2.0.8.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.2.12.Final</version>
        </dependency>
<!--        <dependency> -->
<!--            <groupId>org.hibernate</groupId> -->
<!--            <artifactId>hibernate-validator</artifactId> -->
<!--            <version>6.0.11.Final</version> -->
<!--        </dependency> -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.43</version>
        </dependency>


    </dependencies>
</project>

我开发和工作过的一个控制器示例(非工作状态的控制器类似)

package net.thunderfat.app.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.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import net.thunderfat.app.model.Alimento;
import net.thunderfat.app.service.IAlimentoService;

@Controller
@RequestMapping("/alimento")
public class AlimentoController {
    @Autowired
    private IAlimentoService serviceAlimento;

    @GetMapping(value = "/index")
    public String mostrarIndex(Model model) {
        List<Alimento> listaAlimentos = serviceAlimento.ListarAlimentos();
        model.addAttribute("alimentos", listaAlimentos);
        return "alimento/listAlimento";
    }


    @GetMapping(value = "/create")
    public String crear(@ModelAttribute Alimento alimento) {
        return "alimento/formAlimento";
    }

    /**
     * Metodo para guardar el registro de la Noticia
     * 
     * @param noticia
     * @param result
     * @param model
     * @return
     */
    @PostMapping(value = "/save")
    public String guardar(@ModelAttribute Alimento alimento, BindingResult result, Model model,
            RedirectAttributes attributes) {
        // Insertamos la noticia
        serviceAlimento.insertar(alimento);
        attributes.addFlashAttribute("msg", "Los datos del alimento  fueron guardados!");
        return "redirect:/alimento/index";
    }

    /**
     * Metodo para eliminar una noticia
     * 
     * @param idNoticia
     * @param model
     * @param attributes
     * @return
     */
    @GetMapping(value = "/delete/{id}")
    public String eliminar(@PathVariable("id") int id_alimento, RedirectAttributes attributes) {
        serviceAlimento.eliminar(id_alimento);
        attributes.addFlashAttribute("msg", "La noticia fue eliminada!.");
        return "redirect:/alimento/index";
    }

    /**
     * Metodo para mostrar el formulario de Editar
     * 
     * @param idNoticia
     * @param model
     * @return
     */
    @GetMapping(value = "/edit/{id}")
    public String editar(@PathVariable("id") int id_alimento, Model model) {
        Alimento alimento= serviceAlimento.buscarPorId(id_alimento);
        model.addAttribute("alimento", alimento);
        return "alimento/formAlimento";
    }


}

项目结构 https://imgur.com/a/DojwtLI

有什么主意吗?我曾经检查过类似的问题,但没有起作用

提前谢谢,对我的英语感到抱歉

0 个答案:

没有答案