gpt4 book ai didi

java - 错误 404 - 显示 Springmvc 项目 View 时出错

转载 作者:行者123 更新时间:2023-12-02 01:26:29 26 4
gpt4 key购买 nike

总的来说,我是 spring mvc 的初学者,我正在 Netbeans 中使用该框架做一个项目,并且我以编程方式工作,而不是直接使用 XML 配置文件来配置 servlet。在索引中我有一个表单来搜索具有特定id的元素,它调用 Controller ,但不显示 View ,显示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>

Trabajos Controller

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 = 打包所有项目 Controller

项目

image project

我无法将项目的图像本身展示出来,因为我还没有它的范围

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

最佳答案

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

关于java - 错误 404 - 显示 Springmvc 项目 View 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57581522/

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