Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring mvc 控制器返回字符串";索引“;,而不是重定向到index.jsp_Spring Mvc_Spring Boot - Fatal编程技术网

Spring mvc 控制器返回字符串";索引“;,而不是重定向到index.jsp

Spring mvc 控制器返回字符串";索引“;,而不是重定向到index.jsp,spring-mvc,spring-boot,Spring Mvc,Spring Boot,我正在尝试用SpringBoot制作web应用程序,我花了很多时间,看了很多帖子和文章,但仍然无法重定向到我的jsp,我做错了什么?。这就是我所拥有的: 我的项目结构(我无法发布图像): pom.xml: <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId>

我正在尝试用SpringBoot制作web应用程序,我花了很多时间,看了很多帖子和文章,但仍然无法重定向到我的jsp,我做错了什么?。这就是我所拥有的:

我的项目结构(我无法发布图像):

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>
类别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";
}
}
SpringBoot应用程序:

//@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

您需要从控制器方法中删除
@ResponseBody
,以便分派视图(index.jsp),否则响应将作为http正文的一部分返回

@指示方法返回值的ResponseBody注释应 绑定到web响应主体


您需要从控制器方法中删除
@ResponseBody
,以便分派视图(index.jsp),否则响应将作为http正文的一部分返回

@指示方法返回值的ResponseBody注释应 绑定到web响应主体

要解决您的问题:

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
尝试按以下方式更改您的应用程序:

使用
jsp
的模板解析器不需要
WebMvcConfig
类。您可以在
应用程序.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";
    }
}
如果您不将thymeleaf用作模板引擎,则您不需要
资源
文件夹中的
模板
文件夹以及pom中的依赖项。你可以删除

   <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>

org.springframework.boot
弹簧启动装置
要解决您的问题:

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
尝试按以下方式更改您的应用程序:

使用
jsp
的模板解析器不需要
WebMvcConfig
类。您可以在
应用程序.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";
    }
}
如果您不将thymeleaf用作模板引擎,则您不需要
资源
文件夹中的
模板
文件夹以及pom中的依赖项。你可以删除

   <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>

org.springframework.boot
弹簧启动装置

请删除此应用程序。属性:

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";
}
}

请删除此应用程序。属性:

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";
}
}

在我的场景中,我使用SpringMVCAPI部署了一个ngular应用程序

所以其他解决方案对我不起作用,所以我使用了
web.xml
文件中的
servlet映射

下面是Servlet及其在web.xml文件中的映射示例

<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>

在我的场景中,我使用SpringMVCAPI部署了一个ngular应用程序

所以其他解决方案对我不起作用,所以我使用了
web.xml
文件中的
servlet映射

下面是Servlet及其在web.xml文件中的映射示例

<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>

如果您想返回index.html,请从您的
主控制器中删除
@ResponseBody
,我正在尝试重定向到index.jsp。我已删除@ResponseBody,并得到:出现意外错误(type=internalserver error,status=500)。解析模板“index”时出错,如果要返回index.htmli,则模板可能不存在,或者任何配置的模板解析程序都无法从
MainController
中删除
@ResponseBody
。我已删除@ResponseBody,并得到:出现意外错误(type=internalserver error,status=500)。解析模板“索引”时出错,模板可能不存在,或者任何配置的模板解析程序都无法访问该模板。现在我有一个错误:出现了一个意外错误(type=internalserver error,status=500)。解析模板“索引”时出错,模板可能不存在,或者任何配置的模板解析程序都无法访问该模板。现在我有一个错误:出现了一个意外错误(type=internalserver error,status=500)。解析模板“索引”时出错,模板可能不存在,或者任何已配置的模板解析程序都无法访问该模板。我已删除依赖项“spring boot starter thymeleaf”,它可以正常工作!谢谢我已经删除了依赖项“spring boot starter thymeleaf”,这很有效!非常感谢。