Spring Java 配置与 Jboss 7

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20666513/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 03:27:29  来源:igfitidea点击:

Spring Java Config vs Jboss 7

javaspringhttp-status-code-404jboss7.xconfig

提问by Thiago Pereira

I`m trying to run a simple application with spring java based configuration on jboss, but no success. This application works fine both on jetty and tomcat. The jboss log looks good, since it shows me some successful mappings etc, but I got 404 trying to access the url.

我正在尝试在 jboss 上使用基于 spring java 的配置运行一个简单的应用程序,但没有成功。此应用程序在 jetty 和 tomcat 上都可以正常工作。jboss 日志看起来不错,因为它向我展示了一些成功的映射等,但是我在尝试访问 url 时遇到了 404。

Here are my code:

这是我的代码:

Initializer

初始化程序

@Order(1)
public class Initializer extends AbstractAnnotationConfigDispatcherServletInitializer  {

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

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

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

    @Override
    protected void customizeRegistration(ServletRegistration.Dynamic registration) {
        registration.setInitParameter("dispatchOptionsRequest", "true");
    }
}

RootConfig

根配置

@Configuration
@ComponentScan(value = "com.test.config", excludeFilters = @Filter(type = FilterType.ASSIGNABLE_TYPE, value = RootConfig.class))
public class RootConfig {

}

WebAppConfig

网络应用程序配置

@Configuration
@ComponentScan("com.test")
@EnableWebMvc
@EnableSpringDataWebSupport
public class WebAppConfig extends WebMvcConfigurerAdapter {

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

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

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

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }
}

And now the jboss log...

现在 jboss 日志...

17:08:53,645 INFO  [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/test]] (MSC service thread 1-8) Spring WebApplicationInitializers detected on classpath: [br.com.cleartech.config.Initializer@2f7e4dd2]
17:08:53,767 INFO  [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/test]] (MSC service thread 1-8) Initializing Spring root WebApplicationContext
17:08:53,768 INFO  [org.springframework.web.context.ContextLoader] (MSC service thread 1-8) Root WebApplicationContext: initialization started
17:08:53,770 INFO  [org.springframework.web.context.support.AnnotationConfigWebApplicationContext] (MSC service thread 1-8) Refreshing Root WebApplicationContext: startup date [Wed Dec 18 17:08:53 BRST 2013]; root of context hierarchy
17:08:53,843 INFO  [org.springframework.context.annotation.ClassPathBeanDefinitionScanner] (MSC service thread 1-8) JSR-250 'javax.annotation.ManagedBean' found and supported for component scanning
17:08:53,846 INFO  [org.springframework.context.annotation.ClassPathBeanDefinitionScanner] (MSC service thread 1-8) JSR-330 'javax.inject.Named' annotation found and supported for component scanning
17:08:53,847 INFO  [org.springframework.web.context.support.AnnotationConfigWebApplicationContext] (MSC service thread 1-8) Registering annotated classes: [class br.com.cleartech.config.RootConfig]
17:08:53,915 INFO  [org.springframework.context.annotation.ClassPathBeanDefinitionScanner] (MSC service thread 1-8) JSR-250 'javax.annotation.ManagedBean' found and supported for component scanning
17:08:53,916 INFO  [org.springframework.context.annotation.ClassPathBeanDefinitionScanner] (MSC service thread 1-8) JSR-330 'javax.inject.Named' annotation found and supported for component scanning
17:08:53,977 INFO  [org.springframework.context.annotation.ClassPathBeanDefinitionScanner] (MSC service thread 1-8) JSR-250 'javax.annotation.ManagedBean' found and supported for component scanning
17:08:53,978 INFO  [org.springframework.context.annotation.ClassPathBeanDefinitionScanner] (MSC service thread 1-8) JSR-330 'javax.inject.Named' annotation found and supported for component scanning
17:08:54,130 INFO  [org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor] (MSC service thread 1-8) JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
17:08:54,153 INFO  [org.springframework.beans.factory.support.DefaultListableBeanFactory] (MSC service thread 1-8) Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@52477602: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,rootConfig,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor,log4j,webAppConfig,homeController,consoleAppender,fileAppender,registerSpringLogger,org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration,mvcValidator,simpleControllerHandlerAdapter,beanNameHandlerMapping,httpRequestHandlerAdapter,mvcContentNegotiationManager,requestMappingHandlerMapping,resourceHandlerMapping,requestMappingHandlerAdapter,mvcConversionService,viewControllerHandlerMapping,defaultServletHandlerMapping,handlerExceptionResolver,org.springframework.data.web.config.SpringDataWebConfiguration,pageableResolver,sortResolver,setupViewResolver]; root of factory hierarchy
17:08:54,269 INFO  [org.hibernate.validator.util.Version] (MSC service thread 1-8) Hibernate Validator 4.2.0.Final
17:08:54,388 INFO  [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] (MSC service thread 1-8) Mapped "{[/teste],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String br.com.cleartech.controller.HomeController.teste()
17:08:54,404 INFO  [org.springframework.web.servlet.handler.SimpleUrlHandlerMapping] (MSC service thread 1-8) Mapped URL path [/resources/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
17:08:54,631 INFO  [org.springframework.web.servlet.handler.SimpleUrlHandlerMapping] (MSC service thread 1-8) Root mapping to handler of type [class org.springframework.web.servlet.mvc.ParameterizableViewController]
17:08:54,634 INFO  [org.springframework.web.servlet.handler.SimpleUrlHandlerMapping] (MSC service thread 1-8) Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler]
17:08:54,668 INFO  [org.springframework.web.context.ContextLoader] (MSC service thread 1-8) Root WebApplicationContext: initialization completed in 899 ms
17:08:54,675 INFO  [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/test]] (MSC service thread 1-8) Initializing Spring FrameworkServlet 'dispatcher'
17:08:54,676 INFO  [org.springframework.web.servlet.DispatcherServlet] (MSC service thread 1-8) FrameworkServlet 'dispatcher': initialization started
17:08:54,679 INFO  [org.springframework.web.context.support.AnnotationConfigWebApplicationContext] (MSC service thread 1-8) Refreshing WebApplicationContext for namespace 'dispatcher-servlet': startup date [Wed Dec 18 17:08:54 BRST 2013]; parent: Root WebApplicationContext
17:08:54,681 INFO  [org.springframework.context.annotation.ClassPathBeanDefinitionScanner] (MSC service thread 1-8) JSR-250 'javax.annotation.ManagedBean' found and supported for component scanning
17:08:54,682 INFO  [org.springframework.context.annotation.ClassPathBeanDefinitionScanner] (MSC service thread 1-8) JSR-330 'javax.inject.Named' annotation found and supported for component scanning
17:08:54,683 INFO  [org.springframework.web.context.support.AnnotationConfigWebApplicationContext] (MSC service thread 1-8) Registering annotated classes: [class br.com.cleartech.config.WebAppConfig]
17:08:54,685 INFO  [org.springframework.context.annotation.ClassPathBeanDefinitionScanner] (MSC service thread 1-8) JSR-250 'javax.annotation.ManagedBean' found and supported for component scanning
17:08:54,686 INFO  [org.springframework.context.annotation.ClassPathBeanDefinitionScanner] (MSC service thread 1-8) JSR-330 'javax.inject.Named' annotation found and supported for component scanning
17:08:54,698 INFO  [org.springframework.context.annotation.ClassPathBeanDefinitionScanner] (MSC service thread 1-8) JSR-250 'javax.annotation.ManagedBean' found and supported for component scanning
17:08:54,699 INFO  [org.springframework.context.annotation.ClassPathBeanDefinitionScanner] (MSC service thread 1-8) JSR-330 'javax.inject.Named' annotation found and supported for component scanning
17:08:54,759 INFO  [org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor] (MSC service thread 1-8) JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
17:08:54,786 INFO  [org.springframework.beans.factory.support.DefaultListableBeanFactory] (MSC service thread 1-8) Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@12f882f3: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,webAppConfig,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor,log4j,rootConfig,homeController,consoleAppender,fileAppender,registerSpringLogger,org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration,mvcValidator,simpleControllerHandlerAdapter,beanNameHandlerMapping,httpRequestHandlerAdapter,mvcContentNegotiationManager,requestMappingHandlerMapping,resourceHandlerMapping,requestMappingHandlerAdapter,mvcConversionService,viewControllerHandlerMapping,defaultServletHandlerMapping,handlerExceptionResolver,org.springframework.data.web.config.SpringDataWebConfiguration,pageableResolver,sortResolver,setupViewResolver]; parent: org.springframework.beans.factory.support.DefaultListableBeanFactory@52477602
17:08:54,885 INFO  [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] (MSC service thread 1-8) Mapped "{[/teste],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String br.com.cleartech.controller.HomeController.teste()
17:08:54,891 INFO  [org.springframework.web.servlet.handler.SimpleUrlHandlerMapping] (MSC service thread 1-8) Mapped URL path [/resources/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
17:08:54,927 INFO  [org.springframework.web.servlet.handler.SimpleUrlHandlerMapping] (MSC service thread 1-8) Root mapping to handler of type [class org.springframework.web.servlet.mvc.ParameterizableViewController]
17:08:54,931 INFO  [org.springframework.web.servlet.handler.SimpleUrlHandlerMapping] (MSC service thread 1-8) Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler]
17:08:54,962 INFO  [org.springframework.web.servlet.DispatcherServlet] (MSC service thread 1-8) FrameworkServlet 'dispatcher': initialization completed in 285 ms
17:08:54,977 INFO  [org.jboss.web] (MSC service thread 1-8) JBAS018210: Registering web context: /test
17:08:54,985 INFO  [org.jboss.as] (MSC service thread 1-5) JBAS015951: Admin console listening on http://127.0.0.1:9990
17:08:54,986 INFO  [org.jboss.as] (MSC service thread 1-5) JBAS015874: JBoss AS 7.1.1.Final "Brontes" started in 8333ms - Started 377 of 455 services (77 services are passive or on-demand)
17:08:55,175 INFO  [org.jboss.as.server] (DeploymentScanner-threads - 2) JBAS018559: Deployed "test.war"

As you can see, it seems to be ok, since I got some:

如您所见,似乎没问题,因为我得到了一些:

Spring WebApplicationInitializers detected on classpath
Initializing Spring root WebApplicationContext
Root WebApplicationContext: initialization started
Mapped URL path [/resources/**]
Mapped URL path [/**]
Registering web context: /test
JBAS018559: Deployed "test.war"

But when trying to access localhost:8080/test or even a simple rest declared into a controller I got 404.

但是当尝试访问 localhost:8080/test 甚至是在控制器中声明的简单休息时,我得到了 404。

EDITJust to explain that at the time this post was written, I was not using spring boot. Most of the answers refers to it as a solution.

编辑只是为了解释在写这篇文章时,我没有使用 spring boot。大多数答案都将其称为解决方案。

采纳答案by Michael R

I had a similar problem with a Spring MVC project deployed to JBoss 7.1 with no web.xml.

我在部署到 JBoss 7.1 且没有 web.xml 的 Spring MVC 项目中遇到了类似的问题。

According to Spring javadocs for WebApplicationInitializer, older versions of Tomcat (<=7.0.14) could not be mapped to "/" programmatically. Older versions of JBoss AS 7 have this same defect.

根据 WebApplicationInitializer 的 Spring javadocs,旧版本的 Tomcat (<=7.0.14) 无法以编程方式映射到“/”。旧版本的 JBoss AS 7 也有同样的缺陷。

This was the source of my problem. I was registering the servlet via "/", but JBoss EAP 6.4 doesn't support this mapping programmatically. It only works via web.xml. I still wanted to use programmatic config, so I changed the mapping to "/*" instead of "/", and it fixed my issue.

这是我问题的根源。我通过“/”注册 servlet,但 JBoss EAP 6.4 不支持这种以编程方式映射的映射。它只能通过 web.xml 工作。我仍然想使用编程配置,所以我将映射更改为“/*”而不是“/”,它解决了我的问题。

public class WebApplicationInitializerImpl implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext container) throws ServletException {
        WebApplicationContext context = getContext();

        Dynamic registration = container.addServlet("dispatcher", new DispatcherServlet(context));
        registration.setLoadOnStartup(1);
        registration.addMapping("/*");
    }

    private WebApplicationContext getContext() {
        AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
        context.setConfigLocation(AppConfig.class.getName());
        return context;
    }

}

Note: This configuration is incompatible with JSP views. "/*" will supersede the servlet container's JSP Servlet. If you still rely on JSP views, I would recommend using web.xml to configure the DispatcherServlet insteadof doing it programmatically; the web.xml configuration works with "/" correctly.

注意:此配置与 JSP 视图不兼容。“/*”将取代 servlet 容器的 JSP Servlet。如果您仍然依赖 JSP 视图,我建议您使用 web.xml 来配置 DispatcherServlet而不是以编程方式进行;web.xml 配置正确使用“/”。

<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value></param-value>
    </init-param>
</servlet>

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

回答by Thiago Pereira

Well, for someone else who is facing this problem, it just works fine on the new Wildfly. If you don't have any specific container to run your application, you can choose between jboss 7 and wildfly AND wants to run spring java config, try it out on wildfly!

好吧,对于面临这个问题的其他人来说,它在新的 Wildfly 上运行良好。如果你没有任何特定的容器来运行你的应用程序,你可以在 jboss 7 和 wildfly 之间进行选择,并且想要运行 spring java config,在wildfly 上试试吧!

回答by István Pató

We have a spring-boot (1.1.4) project on JBoss EAP 6.2 (my customer's requirement...)

我们在 JBoss EAP 6.2 上有一个 spring-boot (1.1.4) 项目(我客户的要求......)

I found a solution to run it on JBoss EAP 6.2.0 GA and keep capability to run on Apache Tomcat 7 container.

我找到了一个在 JBoss EAP 6.2.0 GA 上运行它的解决方案,并保持在 Apache Tomcat 7 容器上运行的能力。

Initially my project run in embedded mode, so I need to create and change some files to run on containers.

最初我的项目以嵌入式模式运行,所以我需要创建和更改一些文件以在容器上运行。

To run on Tomcat as root application I created context.xml: /src/main/webapp/META-INF/context.xml

要在 Tomcat 上作为根应用程序运行,我创建了 context.xml: /src/main/webapp/META-INF/context.xml

<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" path=""/>

To run on JBOSS EAP 6.2.0 GA as root application I created jboss-web.xml: /src/main/webapp/WEB-INF/jboss-web.xml

为了在 JBOSS EAP 6.2.0 GA 上作为根应用程序运行,我创建了 jboss-web.xml: /src/main/webapp/WEB-INF/jboss-web.xml

<?xml version="1.0" encoding="UTF-8"?>
<jboss-web>
    <context-root>/</context-root>
</jboss-web>

I created a class, because JBoss servlet mapping works as /* but not with /:

我创建了一个类,因为 JBoss servlet 映射作为 /* 而不是与 / 一起工作

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration.Dynamic;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

/**
 * Working without web.xml with container (not em,bedded mode).
 * JBOSS EAP 6.2 specific: you need to map dispatcherServlet to /* .
 */
public class ContainerWebXml extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(TomcatStart.class);
    }

    /**
     * JBOSS EAP 6.2 mapping.
     *
     * @param container
     * @throws ServletException
     */
    @Override
    public void onStartup(ServletContext container) throws ServletException {
        WebApplicationContext context = getContext();

        Dynamic registration = container.addServlet("dispatcher", new DispatcherServlet(context));
        registration.setLoadOnStartup(1);
        registration.addMapping("/*"); // required JBOSS EAP 6.2.0 GA
        super.onStartup(container);
    }

    private WebApplicationContext getContext() {
        AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
        context.setConfigLocation(TomcatStart.class.getName());
        return context;
    }

}

Do not forget call super.onStartup(container);

不要忘记调用 super.onStartup(container);

Changes in pom.xml:

pom.xml 中的变化

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>

If you use spring.profile, than you need to set as env. variable.

如果使用 spring.profile,则需要设置为 env。多变的。

I run JBoss EAP 6.2.0 GA as standalone mode:

以独立模式运行JBoss EAP 6.2.0 GA:

export JAVA_OPTS="-Dspring.profiles.active=local"
.../jboss-eap-6.2/bin/standalone.sh

If you run on Tomcat, then do not forget to set -Dspring.profiles.active=local

如果你在 Tomcat 上运行,那么不要忘记设置 -Dspring.profiles.active=local

As I see the server.port setting will be ignored when you run on container.

正如我所见,当您在容器上运行时, server.port 设置将被忽略。

回答by Loedolff

As per the answers provided by Michael R and István Pató, the servlet mapping in JBoss must be "/*", not "/". However, the other solutions cause @Component annotated objects to be instantiated twice. The following solves the double initialization by first calling super.onStartupand then adding another mapping for the dispatcher servlet:

根据 Michael R 和 István Pató 提供的答案,JBoss 中的 servlet 映射必须是“/*”,而不是“/”。但是,其他解决方案会导致 @Component 注释对象被实例化两次。下面通过首先调用super.onStartup然后为调度程序servlet添加另一个映射来解决双重初始化:

public class WebApplicationInitializerImpl implements WebApplicationInitializer {
    @Override
    public void onStartup(ServletContext container) throws ServletException {
        super.onStartup(container);

        Dynamic registration = (Dynamic) container.getServletRegistration(EmbeddedWebApplicationContext.DISPATCHER_SERVLET_NAME);
        registration.setLoadOnStartup(1);
        registration.addMapping("/*");
    }
}

回答by Ignacio Rubio

I was using @SpringBootApplication

我正在使用@SpringBootApplication

As I read in this thread I needed to:

当我在此线程中阅读时,我需要:

Change the mapping of the DispatcherServlet to "/*" instead of "/" (by adding a @Bean of type ServletRegistrationBean with a servlet named "dispatcherServlet")

将 DispatcherServlet 的映射更改为“/*”而不是“/”(通过添加类型为 ServletRegistrationBean 的 @Bean 和名为“dispatcherServlet”的 servlet)

In this url I found the code solution: Add Servlet Mapping to dispatch servlet

在这个 url 我找到了代码解决方案:Add Servlet Mapping to dispatch servlet

@SpringBootApplication
public class Application extends SpringBootServletInitializer {

    @Bean
    public DispatcherServlet dispatcherServlet() {
        return new DispatcherServlet();
    }

    /**
     * Register dispatcherServlet programmatically 
     * 
     * @return ServletRegistrationBean
     */
    @Bean
    public ServletRegistrationBean dispatcherServletRegistration() {
        ServletRegistrationBean registration = new ServletRegistrationBean(
                dispatcherServlet(), "/*");
        registration
                .setName(DispatcherServletAutoConfiguration.DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME);
        return registration;
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

回答by Nikunj Sharma

I am using JBoss EAP 6.4. I was going through the thread.

我正在使用 JBoss EAP 6.4。我正在通过线程。

I would like to add that after changing mapping for dispatchServlet from "/" to "/* ". The JSP in your project might not process correctly. I suspect that since "/* " have more precedence over "/*.jsp ", therefore, JSPServlet might not be getting request to process the JSP and JSP will not be processed correctly. In my case, source of JSP is coming on browser as text.

在将 dispatchServlet 的映射从“/”更改为“/*”之后,我想补充一点。您项目中的 JSP 可能无法正确处理。我怀疑由于“/*”比“/*.jsp”有更高的优先级,因此,JSPServlet 可能不会收到处理 JSP 的请求,并且不会正确处理 JSP。就我而言,JSP 的源代码以文本形式出现在浏览器中。

I have resolved this issue by defining JSP as servlet in web.xml as mentioned below. After that things worked fine for me :)

我通过在 web.xml 中将 JSP 定义为 servlet 解决了这个问题,如下所述。在那之后,事情对我来说很好:)

<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<servlet>
    <servlet-name>LoginServlet</servlet-name>
    <jsp-file>/login.jsp</jsp-file>
</servlet>

<servlet>
    <servlet-name>IndexServlet</servlet-name>
    <jsp-file>/index.jsp</jsp-file>
</servlet>
<!--mapping -->
<servlet-mapping>
    <servlet-name>LoginServlet</servlet-name>
    <url-pattern>/login.jsp</url-pattern>
</servlet-mapping>

<servlet-mapping>
    <servlet-name>IndexServlet</servlet-name>
    <url-pattern>/index.jsp</url-pattern>
</servlet-mapping>

I spent lot of time on this, might help someone :))

我花了很多时间在这上面,可能会帮助某人:))

回答by jeremy simon

I'm using Spring Boot 1.3.1 and JBoss EAP 6.4. And I found with that in your project you can add to src/main/resources/application.properties this line:

我使用的是 Spring Boot 1.3.1 和 JBoss EAP 6.4。我发现在你的项目中你可以添加到 src/main/resources/application.properties 这一行:

server.servlet-path=/*

Also, if you're launching this out of Eclipse, be sure to clean your project... I blew a lot of time because of that alone.

此外,如果您要从 Eclipse 中启动它,请务必清理您的项目……仅此一项,我就浪费了很多时间。

回答by Jan de Graaf

I had similar problems with JBoss 6.4.0. and Spring Boot 1.3 in combination with Tiles 3. After installing the Jboss patch jboss-eap-6.4.6-patch.zip the problems were solved.

我在 JBoss 6.4.0 上遇到了类似的问题。和 Spring Boot 1.3 结合 Tiles 3。安装 Jboss 补丁 jboss-eap-6.4.6-patch.zip 后,问题解决。

After patching I did not need to use the setting server.servlet-path=/*

修补后我不需要使用设置 server.servlet-path=/*