我正在尝试使用注释配置Spring Boot。 我有课
@EnableWebMvc
@Configuration
@ComponentScan({
...
})
@EnableTransactionManagement
@EnableAutoConfiguration
@Import({ SecurityConfig.class })
public class AppConfig extends SpringBootServletInitializer {...}
包含此View解析器,它可以正常工作。
@Bean
public InternalResourceViewResolver internalViewResolver() {
InternalResourceViewResolver viewResolver
= new InternalResourceViewResolver();
viewResolver.setViewClass(JstlView.class);
viewResolver.setPrefix("/WEB-INF/pages/");
viewResolver.setSuffix(".jsp");
viewResolver.setOrder(1);
return viewResolver;
}
但是在收到JSP文件应用程序的名称后引发此错误: 在DispatcherServlet中找不到带有URI [/WEB-INF/pages/MainPage.jsp]的HTTP请求的映射,其名称为' dispatcherServlet'。
我找到了XML配置的解决方案:
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
但我使用的是注释配置,所以这种灵魂不适合我。
我试图解决这个问题,扩展了AbstractAnnotationConfigDispatcherServletInitializer
public class SpringMvcInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[] { AppConfig.class };
}
@Override
protected Class<?>[] getServletConfigClasses() {
return null;
}
@Override
// I thought this method will be equivalent to XML config solution described above
protected String[] getServletMappings() {
return new String[] { "/" };
}
}
但此后没有任何改变。顺便说一下,我查看了一些使用AbstractAnnotationConfigDispatcherServletInitializer的示例,但我仍然不了解应用程序在没有注释时如何使用此类,并且没有创建此类的实例。它刚刚宣布,这就是全部。也许我需要创建这个类的实例并将其附加到任何地方?
无论如何,我在日志中看到这一行:映射servlet:&#39; dispatcherServlet&#39;到[/] 所以看起来我有正确的servlet配置。
我尝试了this solution,但它没有帮助。我删除了InternalResourceViewResolver并使用以下内容创建了application.properties:
spring.view.prefix: /WEB-INF/jsp/
spring.view.suffix: .jsp
但之后我收到了: javax.servlet.ServletException:无法解析名称为&#39; MainPage&#39;在名为&#39; dispatcherServlet&#39;
的servlet中那么解决这个问题的正确方法是什么?
更新 我试图从头开始创建一个新的简单项目。
的pom.xml :
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>edu.springtest</groupId>
<artifactId>SpringTest</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.0.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
Main.java :
@Controller
@Configuration
@ComponentScan
@EnableAutoConfiguration
@SpringBootApplication
public class Main {
@RequestMapping("/")
String home() {
return "hello";
}
public static void main(String[] args) throws Exception {
SpringApplication.run(Main.class, args);
}
}
application.properties :
spring.view.prefix: /WEB-INF/jsp/
spring.view.suffix: .jsp
项目结构:
我使用命令mvn spring-boot运行项目:运行和接收 edu.test.Main:在输出中以2.365秒(JVM运行为5.476)开始运行。 但是当我打开localhost:8080时,我收到了:
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Sat Dec 20 11:25:29 EET 2014
There was an unexpected error (type=Not Found, status=404).
No message available
但现在我没有收到&#34;没有找到映射......&#34;输出错误。当我打开localhost时:8080根本没有输出任何内容。 那么我做错了什么?
答案 0 :(得分:1)
摆脱自定义视图解析器并设置应用程序属性是正确的开始。 Spring Boot的重点在于它能够很好地为您解决这些问题! :)
您应该有一个带有请求映射的控制器。类似的东西:
@RequestMapping("/")
public String mainPage() {
return "MainPage";
}
...将MainPage.jsp
模板用于/
的任何请求。
但是,值得注意的是,默认情况下,src/main/webapp
的内容不会内置到可执行应用程序jar中。为了解决他的问题,我知道了几个选项。
选项1 - 将所有内容从/src/main/webapp/
移至src/main/resources/static
。我认为这也适用于JSP。这里唯一的麻烦是你可以热替换代码,除非你在IDE中运行应用程序。
选项2 - 替代方案(我倾向于使用)是设置我的Maven构建,以便将src/main/webapp
的内容复制到classpath static
文件夹中建立。
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/classes/static</outputDirectory>
<resources>
<resource>
<directory>src/main/webapp</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
为了进一步阅读(虽然这看起来有点像您已经在做的那样),但是有一个示例项目,显示了使用JSP进行模板化的Spring Boot应用程序:
答案 1 :(得分:0)
我遇到同样的问题,但我还没有解决,但可以为您的调查提供更多信息
通过在tomcat-embed-core.jar中的CachedResource源代码中进行跟踪,我发现如果从开发环境运行应用程序,例如Eclipse,资源路径将指向项目目录,如下所示:
"C:/MyProject/MyApplication/src/main/webapp/WEB-INF/views/jsp/index.jsp"
其中必须包含此资源,但是当我使用命令&#34; java -jar ...&#34;从服务器运行它时,资源路径将设置为临时文件夹:
"/tmp/tomcat-docbase.3976292633605332325.8080/WEB-INF/views/jsp/index.jsp"
但/tmp/tomcat-docbase.3976292633605332325.8080为空。但是我不知道如何设置资源路径,但上面两个都显示InternalResourceViewResolver只处理来自物理目录的资源,而不是来自类路径。设置ResourceHandler并不重要。
我有这个
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
registry.addResourceHandler("/WEB-INF/**").addResourceLocations("classpath:/WEB-INF/");
}
并且将所有资源打包到包中,静态资源是find,但是jsp资源一直在说404。
答案 2 :(得分:0)
对我而言,解决方案是在Intellij IDEA中。
在我的tomcat服务器配置中,在Deployment选项卡下,我错过了预期的Application上下文条目。例如。 / myroot
希望能帮助别人!
答案 3 :(得分:-1)
将Bean更改为:
@Bean
public ViewResolver getInternalResourceViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/pages/");
resolver.setSuffix(".jsp");
return resolver;
}
愿这会帮到你