gpt4 book ai didi

java - java配置中的Spring MVC在访问jsp时得到404

转载 作者:行者123 更新时间:2023-12-03 05:43:14 26 4
gpt4 key购买 nike

我在 java config 中创建了一个简单的 spring mvc 演示,但是当我尝试访问 home.jsp 时得到了 404 .
下面是代码:

enter image description here

build.gradle

ext {
// spring
springVersion = '5.0.7.RELEASE'
// servlet+jsp
servletVersion = '3.1.0'
jspApiVersion = '2.3.1'
// test
junitVersion = '4.12'
// log
slf4jVersion = "1.7.25"
logbackVersion = "1.2.3"
logbackExtVersion = "0.1.4"
}

group = 'com.springmvc'
version = '1.0.0'

apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'idea'

sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
maven {
url = 'http://maven.aliyun.com/nexus/content/groups/public/'
}
}

idea {
module {
downloadJavadoc = true
// download source
downloadSources = true
}
}

dependencies {
providedCompile(
"javax.servlet:javax.servlet-api:${servletVersion}",
"javax.servlet.jsp:javax.servlet.jsp-api:${jspApiVersion}"
)
compile(
// spring
"org.springframework:spring-core:${springVersion}",
"org.springframework:spring-webmvc:${springVersion}",
// log
"org.slf4j:slf4j-api:${slf4jVersion}",
"ch.qos.logback:logback-core:${logbackVersion}",
"ch.qos.logback:logback-classic:${logbackVersion}",
"org.logback-extensions:logback-ext-spring:${logbackExtVersion}"
)
testCompile("junit:junit:${junitVersion}")
}

WebAppInitializer.java
package com.springmvc.config;

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

public class WebAppInitializer
extends AbstractAnnotationConfigDispatcherServletInitializer {
/**
* Specify {@code @Configuration} and/or {@code @Component} classes for the
* {@linkplain #createRootApplicationContext() root application context}.
*
* @return the configuration for the root application context, or {@code null}
* if creation and registration of a root context is not desired
*/
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[]{RootConfig.class};
}

/**
* Specify {@code @Configuration} and/or {@code @Component} classes for the
* {@linkplain #createServletApplicationContext() Servlet application context}.
*
* @return the configuration for the Servlet application context, or
* {@code null} if all configuration is specified through root config classes.
*/
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[]{WebConfig.class};
}

/**
* Specify the servlet mapping(s) for the {@code DispatcherServlet} &mdash;
* for example {@code "/"}, {@code "/app"}, etc.
*/
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
}

RootConfig.java
package com.springmvc.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

@Configuration
@ComponentScan(
basePackages = "com.springmvc",
excludeFilters = {
@ComponentScan.Filter(
type = FilterType.ANNOTATION,
value = EnableWebMvc.class),
@ComponentScan.Filter(
type = FilterType.ANNOTATION,
value = Controller.class
)
})
public class RootConfig {
}

WebConfig.java
package com.springmvc.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
@EnableWebMvc
@ComponentScan("com.springmvc.web")
public class WebConfig implements WebMvcConfigurer {

/**
* Configure simple automated controllers pre-configured with the response
* status code and/or a view to render the response body. This is useful in
* cases where there is no need for custom controller logic -- e.g. render a
* home page, perform simple site URL redirects, return a 404 status with
* HTML content, a 204 with no content, and more.
*/
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("home");
}

/**
* Configure view resolvers to translate String-based view names returned from
* controllers into concrete {@link View}
* implementations to perform rendering with.
*
* @since 4.1
*/
@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
registry.jsp("/views/", ".jsp");
}
}

和简单的jsp:
<%@ page contentType="text/html;charset=UTF-8" %>
<html>
<head>
<title>SpringMvcDemo</title>
</head>
<body>
Hello Spring MVC 5!
</body>
</html>

这里有一些日志:
23:16:18.038 [http-nio-7070-exec-2] DEBUG org.springframework.web.servlet.DispatcherServlet - DispatcherServlet with name 'dispatcher' processing GET request for [/]
23:16:18.042 [http-nio-7070-exec-2] DEBUG org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Looking up handler method for path /
23:16:18.042 [http-nio-7070-exec-2] DEBUG org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Did not find handler method for [/]
23:16:18.043 [http-nio-7070-exec-2] DEBUG org.springframework.web.servlet.handler.SimpleUrlHandlerMapping - Mapping [/] to HandlerExecutionChain with handler [org.springframework.web.servlet.mvc.ParameterizableViewController@784f4a1a] and 1 interceptor
23:16:18.044 [http-nio-7070-exec-2] DEBUG org.springframework.web.servlet.DispatcherServlet - Last-Modified value for [/] is: -1
23:16:18.051 [http-nio-7070-exec-2] DEBUG org.springframework.web.servlet.DispatcherServlet - Rendering view [org.springframework.web.servlet.view.InternalResourceView: name 'home'; URL [/views/home.jsp]] in DispatcherServlet with name 'dispatcher'
23:16:18.052 [http-nio-7070-exec-2] DEBUG org.springframework.web.servlet.view.InternalResourceView - Forwarding to resource [/views/home.jsp] in InternalResourceView 'home'
23:16:18.053 [http-nio-7070-exec-2] DEBUG org.springframework.web.servlet.DispatcherServlet - Successfully completed request

我以为一切都完成了,我启动了tomcat,输入:
http://localhost:7070/

但是 然后,我得到了一个 404 :(
enter image description here

(我也试过 http://localhost:7070/SpringMvcDemo ,但没有帮助..)

我不知道为什么..所以请谁能告诉我为什么会发生这种情况?

最佳答案

创建 UrlBasedViewResolver bean 在你的WebConfig类(class)。

@Bean
public UrlBasedViewResolver setupViewResolver() {

UrlBasedViewResolver resolver = new UrlBasedViewResolver();
resolver.setPrefix("/views/");
resolver.setSuffix(".jsp");
resolver.setViewClass(JstlView.class);
return resolver;
}

您可以扩展您的 WebConfig来自 WebMvcConfigurerAdapter . WebMvcConfigurerAdapter 是 WebMvcConfigurer 的实现界面

关于java - java配置中的Spring MVC在访问jsp时得到404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51502861/

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