我正在尝试用春季启动制作网络应用程序,我花了很多时间,看了很多帖子和文章,但仍然无法重定向到我的jsp,我做错了什么?我有:
我的项目结构(我无法发布图片):
http://i.imgur.com/8zKwlfO.png
的pom.xml:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.7</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-remote-shell</artifactId>
</dependency>
</dependencies>
application.properties:
spring.mvc.view.prefix: /WEB-INF/jsp/
spring.mvc.view.suffix: .jsp
spring.session.store-type=none
spring.datasource.url:jdbc:mysql://localhost/phonebook?useSSL=false
spring.datasource.username:root
spring.datasource.password:root
spring.datasource.driver:com.mysql.jdbc.Driver
spring.jpa.properties.hibernate.dialect:org.hibernate.dialect.MySQL5Dialect
logging.level.org.hibernate.SQl:debug
class WebMvcConfig:
@Configuration
//@EnableWebMvc
public class WebMvcConfig extends WebMvcConfigurerAdapter {
@Override
public void configureDefaultServletHandling(
DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
@Bean
public InternalResourceViewResolver viewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("WEB-INF/jsp/");
resolver.setSuffix(".jsp");
return resolver;
}
控制器:
@Controller
public class MainController {
@RequestMapping(value = "/index", method = RequestMethod.GET)
@ResponseBody
public String index() {
return "index";
}
}
SpringBootApplication:
//@Configuration
//@ComponentScan
//@EnableAutoConfiguration
@SpringBootApplication
public class PhoneBookApplication extends SpringBootServletInitializer{
public static void main(String[] args) {
SpringApplication.run(PhoneBookApplication.class, args);
}
}
从我的控制器中删除@ResponseBody
后,我遇到了这个例外:
There was an unexpected error (type=Internal Server Error, status=500). Error resolving template "index", template might not exist or might not be accessible by any of the configured Template Resolvers
答案 0 :(得分:0)
您需要从控制器方法中删除@ResponseBody
才能调度视图(index.jsp),否则响应将作为http正文的一部分返回。
@ResponseBody注释指示方法返回值应该 受到网络响应机构的约束。
答案 1 :(得分:0)
解决您的问题:
There was an unexpected error (type=Internal Server Error, status=500). Error resolving template "index", template might not exist or might not be accessible by any of the configured Template Resolvers
尝试更改您的应用程序:
对于WebMvcConfig
使用模板解析器,您不需要jsp
类。您可以在application.properties
文件中正确配置后缀和前缀。所以删除这个类。
@SpringBootApplication
public class PhoneBookApplication extends SpringBootServletInitializer{
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(PhoneBookApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(PhoneBookApplication.class, args);
}
}
控制器:
@Controller
public class MainController {
@RequestMapping(value = "/index", method = RequestMethod.GET)
public String index() {
return "index";
}
}
如果你不使用百日咳作为模板,你不需要template
文件夹中的resource
文件夹和pom中的依赖项。你可以删除
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
答案 2 :(得分:0)
请删除此 application.properties:
spring.mvc.view.prefix: /WEB-INF/jsp/
spring.mvc.view.suffix: .jsp
enter code here
此外,从控制器 @ResponseBody
中删除它@Controller
public class MainController {
@RequestMapping(value = "/index", method = RequestMethod.GET)
public String index() {
return "index";
}
}
答案 3 :(得分:0)
在我的情况下,我正在使用Spring MVC API部署一个 ngular应用程序。
其他解决方案对我不起作用,所以我使用了servlet-mapping
文件中的web.xml
。
这是web.xml文件中Servlet及其Mappling的示例。
<servlet>
<servlet-name>index</servlet-name>
<jsp-file>/index.html</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>index</servlet-name>
<url-pattern>/login/*</url-pattern>
</servlet-mapping>
需要将web.xml文件放置在Web-INF目录中。
这是web.xml
文件的结构
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app>
... Your configurations
</web-app>