java7找不到uri_关于Java:未找到带有URI的HTTP请求的映射(Spring 4.1注释配置)

在Spring Boot应用中遇到HTTP请求与/WEB-INF/index.jsp映射找不到的问题。问题源于没有正确配置DispatcherServlet和视图解析器。解决方案包括检查JSP位置,确认是否使用了正确的嵌入式容器,以及调整Spring Boot的自动配置和主应用类,确保SpringBootServletInitializer的使用,同时在application.properties中设置视图前缀和后缀。
摘要由CSDN通过智能技术生成

首先,请不要锁定该线程。 我已经阅读了所有问题,但仍然无法解决我的问题。

我正在研究如何在不配置xml文件的情况下在spring上使用注释。

我坚持错误:

No mapping found for HTTP request with URI [/WEB-INF/index.jsp] in

DispatcherServlet with name 'dispatcherServlet'

这是项目结构

b529258652a6103cb9e555dd12d5c968.png

这是我的pom.xml

4.0.0

com.mycompany

spring-sample

1.0

war

spring-sample

${project.build.directory}/endorsed

UTF-8

org.springframework.boot

spring-boot-starter-parent

1.2.0.M2

javax

javaee-web-api

7.0

provided

org.springframework

spring-context

org.springframework.boot

spring-boot-starter-web

spring-milestones

Spring Milestones

http://repo.spring.io/milestone

false

spring-snapshots

http://repo.spring.io/snapshot

spring-milestones

http://repo.spring.io/milestone

org.apache.maven.plugins

maven-compiler-plugin

3.1

1.7

1.7

${endorsed.dir}

org.apache.maven.plugins

maven-war-plugin

2.3

false

org.apache.maven.plugins

maven-dependency-plugin

2.6

validate

copy

${endorsed.dir}

true

javax

javaee-endorsed-api

7.0

jar

org.springframework.boot

spring-boot-maven-plugin

repackage

这是配置类:

@ComponentScan(basePackages ="com.sample.spring")

@EnableAutoConfiguration

@EnableWebMvc

@org.springframework.context.annotation.Configuration

public class MainApp extends WebMvcConfigurerAdapter implements WebApplicationInitializer {

@Override

public void onStartup(ServletContext sc) throws ServletException {

// Create the 'root' Spring application context

AnnotationConfigWebApplicationContext rootContext

= new AnnotationConfigWebApplicationContext();

rootContext.register(MainApp.class);

// Manage the lifecycle of the root application context

sc.addListener(new ContextLoaderListener(rootContext));

ServletRegistration.Dynamic registration = sc.addServlet("dispatcher", new DispatcherServlet());

registration.setLoadOnStartup(1);

registration.addMapping("/");

}

@Bean

public ViewResolver configureViewResolver() {

InternalResourceViewResolver viewResolve = new InternalResourceViewResolver();

viewResolve.setPrefix("/WEB-INF/");

viewResolve.setSuffix(".jsp");

return viewResolve;

}

@Bean

public RequestMappingHandlerMapping requestMappingHandlerMapping() {

return new RequestMappingHandlerMapping();

}

@Bean

public RequestMappingHandlerAdapter requestMappingHandlerAdapter() {

return new RequestMappingHandlerAdapter();

}

public static void main(String[] args) {

SpringApplication.run(MainApp.class, args);

}

}

这是控制器:

@org.springframework.stereotype.Controller

public class Controller {

@RequestMapping(value="/", method = RequestMethod.GET)

public String index(){

return"index";

}

}

index.jsp位于WEB-INF文件夹内,而web.xml文件内没有任何内容。

我认为到目前为止该配置是正确的,因为它可以重定向到/WEB-INF/index.jsp。

但是,我认为我缺少一些配置来使应用程序理解路径。 (/WEB-INF/index.jsp)

由于您未提供文件结构,因此确定WEB-INF位置正确吗?

您正在使用什么嵌入式容器? 它似乎缺少JspServlet。

@The Head Rush:我已经添加了图片,我认为它在正确的位置

@Sotirios Delimanolis:我通过命令" mvn spring-boot:run"使用spring-boot运行它

那会用tomcat还是其他东西? 您的DispatcherServlet不应处理对JSP的请求。

@Sotirios Delimanolis:我不确定弹簧靴使用哪一种,但我认为它不是tomcat。 为什么DispatcherServlet不应该处理对JSP的请求?

Servlet容器具有专用于JSP的(默认)Servlet实现。 此JspServlet执行JSP的编译和执行(调用service)。 Springs DispatcherServlet不知道该怎么做。

鉴于您正在使用Spring Boot,因此您的配置要复杂得多。 引导程序将自动为您配置DispatcherServlet,ViewResolver,RequestMappingHandlerMapping和RequestMappingHandlerAdapter。

查看Spring Boot的JSP示例,以了解所需的配置量。

除了删除几乎所有的手动配置之外,一个主要区别是您的主应用程序类应扩展SpringBootServletInitializer而不是直接实现Spring的WebApplicationInitializer。 这是上述示例中的等效类:

package sample.jsp;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;

import org.springframework.boot.builder.SpringApplicationBuilder;

import org.springframework.boot.context.web.SpringBootServletInitializer;

import org.springframework.context.annotation.ComponentScan;

import org.springframework.context.annotation.Configuration;

@Configuration

@EnableAutoConfiguration

@ComponentScan

public class SampleWebJspApplication extends SpringBootServletInitializer {

@Override

protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {

return application.sources(SampleWebJspApplication.class);

}

public static void main(String[] args) throws Exception {

SpringApplication.run(SampleWebJspApplication.class, args);

}

}

使用java -jar启动war文件时使用main方法,而将war部署到单独的Servlet容器时使用configure方法。

还请注意该示例对application.properties的使用,以配置视图前缀和后缀。

谢谢,对我有很大帮助

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值