【问题标题】:Error 404 - Error displaying views of a Springmvc project错误 404 - 显示 Spring Mvc 项目的视图时出错
【发布时间】:2019-08-20 21:05:33
【问题描述】:

一般来说,我是 spring mvc 的初学者,我正在 Netbeans 中使用该框架做一个项目,并且我正在以编程方式工作,而不是直接使用 XML 配置文件来配置 servlet。在索引中我有一个表单来搜索具有特定id的元素,它调用了控制器,但是视图没有显示,显示资源不存在的404错误,但是我正在通过get方法进行搜索,如果他执行它。事实是我已经按照我在 spring 文档和互联网上看到的配置,即使那样它也不起作用。除了它们包含的包之外,我在这里留下了我的配置

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>redirect.jsp</welcome-file>
    </welcome-file-list>
</web-app>

AppContext 类

package edu.co.ucatolica.trabajoya.config;

import java.util.Properties;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.hibernate5.HibernateTransactionManager;
import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@PropertySource("classpath:database.properties")
@EnableTransactionManagement
@ComponentScan(basePackages = {
    "edu.co.ucatolica.trabajoya"
})
public class AppContext {

    @Autowired
    private Environment environment;

    @Bean
    public LocalSessionFactoryBean sessionFactory() {
        LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
        sessionFactory.setDataSource(dataSource());
        sessionFactory.setPackagesToScan(new String[] {
            "edu.co.ucatolica.trabajoya.model"
        });
        sessionFactory.setHibernateProperties(hibernateProperties());
        return sessionFactory;
    }

    @Bean
    public DataSource dataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(environment.getRequiredProperty("jdbc.driverClassName"));
        dataSource.setUrl(environment.getRequiredProperty("jdbc.url"));
        dataSource.setUsername(environment.getRequiredProperty("jdbc.user"));
        dataSource.setPassword(environment.getRequiredProperty("jdbc.pass"));
        return dataSource;
    }

    private Properties hibernateProperties() {
        Properties properties = new Properties();
        properties.put("hibernate.dialect", environment.getRequiredProperty("hibernate.dialect"));
        properties.put("hibernate.show_sql", environment.getRequiredProperty("hibernate.show_sql"));
        properties.put("hibernate.format_sql", environment.getRequiredProperty("hibernate.format_sql"));
        properties.put("hibernate.hbm2ddl.auto", environment.getRequiredProperty("hibernate.hbm2ddl.auto"));
        return properties;
    }

    @Bean
    public HibernateTransactionManager getTransactionManager() {
        HibernateTransactionManager transactionManager = new HibernateTransactionManager();
        transactionManager.setSessionFactory(sessionFactory().getObject());
        return transactionManager;
    }
}

AppInitializer 类

package edu.co.ucatolica.trabajoya.config;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

/**
 *
 * @author Carlos
 */
public class AppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class < ? > [] getRootConfigClasses() {
        return new Class[] {
            AppContext.class
        };
        //return null;
    }

    @Override
    protected Class < ? > [] getServletConfigClasses() {
        return new Class[] {
            WebMvcConfig.class
        };
    }

    @Override
    protected String[] getServletMappings() {
        return new String[] {
            "/"
        };
    }
}

WebMvcConf 类

package edu.co.ucatolica.trabajoya.config;

import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.validation.Validator;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;

/**
 * @author Ramesh Fadatare
 */
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = {
    "edu.co.ucatolica.trabajoya.controller"
})
public class WebMvcConfig implements WebMvcConfigurer {

    @Bean
    public InternalResourceViewResolver resolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/jsp/");
        resolver.setSuffix(".jsp");
        resolver.setViewClass(JstlView.class);
        return resolver;
    }

    @Bean
    public MessageSource messageSource() {
        ResourceBundleMessageSource source = new ResourceBundleMessageSource();
        source.setBasename("messages");
        return source;
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry
                .addResourceHandler("/resources/**")
                .addResourceLocations("/resources/");
    }

    @Override
    public Validator getValidator() {
        LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
        validator.setValidationMessageSource(messageSource());
        return validator;
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("index");
    }
}

index.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Welcome to Spring Web MVC project</title>
    </head>
    <form action="search" method="get" modelAttribute="trabajos">
        <form:hidden path="id" />
        ID: <input type="text" name="txtIdTrabajo" id="txtIdTrabajo" />
        <br>
        <br>
        <input type="submit" value="Buscar"/>
    </form>

</html>

trabajos.jsp

<%-- 
    Document   : trabajos
    Created on : 19/08/2019, 10:48:36 PM
    Author     : Carlos
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Hello World!</h1>
    </body>
</html>

特拉巴霍斯控制器

package edu.co.ucatolica.trabajoya.controller;

import edu.co.ucatolica.trabajoya.model.Trabajos;
import edu.co.ucatolica.trabajoya.service.TrabajosService;
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.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 *
 * @author Carlos
 */
@Controller
@RequestMapping("/trabajos")
public class TrabajosController {
    @Autowired
    private TrabajosService trabajosService;

    @GetMapping("/list")
    public String list(Model model){
        List<Trabajos> trab = this.trabajosService.list();
        model.addAttribute("trabajos", trab);
        return "list-trabajos";
    }

    @GetMapping("/search")
    public String search(@RequestParam("txtIdTrabajo")int id, Model model){
        Trabajos t = this.trabajosService.search(id);
        model.addAttribute("trabajo", t);
        return "trabajos";
    }
}

我拥有的包如下:

edu.co.ucatolica.trabajoya.model = 项目实体已保存

edu.co.ucatolica.trabajoya.dao = 项目 DAO 课程

edu.co.ucatolica.trabajoya.services = 保存项目服务类

edu.co.ucatolica.trabajoya.config = 保存所有应用程序设置,这里是项目配置的 3 个主要类

edu.co.ucatolica.trabajoya.controller = 打包所有项目控制器

项目

image project

我不能这样放项目的图像,因为我还没有范围

我想知道我只能看到没有任何问题的视图并解决该错误 404。谢谢

【问题讨论】:

    标签: java hibernate spring-mvc jsp


    【解决方案1】:

    尝试将您的 index.jsp 文件与您的 WEB-INF 文件夹放在同一级别。

    【讨论】:

    • 问题不在于index.jsp,既然显示了,那么问题在于其他视图。即便如此,我还是在各个级别移动了它们,无法访问jsp 文件夹中的索引或其他视图
    猜你喜欢
    • 2016-05-13
    • 1970-01-01
    • 1970-01-01
    • 2019-08-04
    • 1970-01-01
    • 2020-06-19
    • 1970-01-01
    • 2017-06-18
    • 1970-01-01
    相关资源
    最近更新 更多