1

i am trying to make web app with spring boot, i've spent much time, looked many posts and articles, and still can't make redirect to my jsp, what am i doing wrong?. That's i have:

my project structure (I can't post image):

https://i.sstatic.net/z7CYR.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 :

@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);
}
}

After deleting @ResponseBody from my Controller I got this exception:

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
2
  • delete @ResponseBody from your MainController if you want to return index.html Commented Nov 3, 2016 at 12:59
  • i am trying to redirect to index.jsp. I've deleted @ResponseBody, and got: 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 Commented Nov 3, 2016 at 13:02

4 Answers 4

1

To solve your issue:

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

Try to change your application like this:

You dont need to have a WebMvcConfig class for using template resolver for jsp. You configure the suffix and prefix correctly in your application.properties file. So delete this class.

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

@Controller
public class MainController {

    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index() {
       return "index";
    }
}

You dont need a template folder in your resource folder and the dependency in your pom, if you dont use thymeleaf as template enginge. You can delete

   <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
Sign up to request clarification or add additional context in comments.

1 Comment

I've deleted dependency "spring-boot-starter-thymeleaf", and it's work! Thank you
0

You need to remove the @ResponseBody from the controller method in order to dispatch the view (index.jsp), otherwise the response will be returned as part of the http body.

@ResponseBody Annotation that indicates a method return value should be bound to the web response body.

http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/bind/annotation/ResponseBody.html

1 Comment

i did it. now i have an error: 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

Please remove this application.properties:

spring.mvc.view.prefix: /WEB-INF/jsp/
spring.mvc.view.suffix: .jsp
enter code here

Also, Remove it from Controller @ResponseBody

@Controller
public class MainController {

@RequestMapping(value = "/index", method = RequestMethod.GET)
public String index() {
   return "index";
}
}

Comments

0

In my scenario, i'm deploying a Angular application with Spring MVC API.

So the other solutions were not working for me, So i used servlet-mapping from the web.xml file.

Here is example of the Servlet and its Mappling in the web.xml file.

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

The web.xml file need to be placed in the Web-INF directory.

Here is the structure of the web.xml file

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app>
... Your configurations
</web-app>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.