0

I am new in the Spring Boot Application development.I am doing simple spring boot application using spring boot version 1.4.3 Release version and Spring 4.3.5.RELEASE version. I am successfully in the configuration of Mysql database configuration. Also table get created while running Spring Boot main class.

Below is Spring Boot project directory structure :

SpringBootApp[boot]
src/main/java
  -config
  -controller
  -dto
  -entity
  -repository
  -service
src/main/resources
  -application.properties
src/test/java
src/main/webapp
               /recources/css
               /resources/images
               /resources/js
               /WEB-INF/view/jsp/main.jsp

I created DBConfig.java file which having DB related functionality like datasource creation,jpa transaction management.

Below is WebMVC configuration file:

    @EnableWebMvc
    @Configuration
    @Import(DBConfig.class)
    @ComponentScan(basePackages = { "com.tms" })
    public class SpringWebConfig extends WebMvcConfigurerAdapter {
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
        }

        @Bean
        public InternalResourceViewResolver viewResolver() {
            InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
            viewResolver.setViewClass(JstlView.class);
            viewResolver.setPrefix("/WEB-INF/view/");
            viewResolver.setSuffix(".jsp");
            return viewResolver;
        }

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

Below is App Context initializer file :

    public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

        @Override
        protected Class<?>[] getRootConfigClasses() {
            return new Class[] { DBConfig.class };
        }
        @Override
        protected Class<?>[] getServletConfigClasses() {
            return new Class[] { SpringWebConfig.class };
        }
        @Override
        protected String[] getServletMappings() {
            return new String[] { "/" };
        }

    }

I also added below line of code in application.properties file to locate jsp pages and to deactivate thymeleaf template.

spring.mvc.view.prefix: /WEB-INF/jsp/
spring.mvc.view.suffix: .jsp
spring.thymeleaf.check-template-location=false

Below is Spring Controller java file:

@RequestMapping("/")
public class IndexController {

    @RequestMapping(method = RequestMethod.GET)
    public String printWelcome(ModelMap model) {
        model.addAttribute("title", "Hello world!");
        return "main";
    }
}

Below jsp sample page :

/WEB-INF/jsp/main.jsp

    <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
    <html>
    <head>
    <title>Spring Boot</title>
    </head>
    <body>

        <h2>Test Page</h2>
        <table>
            <tr>
                <td>Title</td>
                <td>${title}</td>
            </tr>

        </table>
    </body>
    </html>

Spring Boot Application successfully get started.

Below is STS console output :

2017-01-12 04:22:57.501[0;39m [32m INFO[0;39m [35m4968[0;39m [2m---[0;39m [2m[           main][0;39m [36morg.hibernate.tool.hbm2ddl.SchemaUpdate [0;39m [2m:[0;39m HHH000228: Running hbm2ddl schema update
[2m2017-01-12 04:22:57.677[0;39m [32m INFO[0;39m [35m4968[0;39m [2m---[0;39m [2m[           main][0;39m [36mj.LocalContainerEntityManagerFactoryBean[0;39m [2m:[0;39m Initialized JPA EntityManagerFactory for persistence unit 'jpaUnit'
[2m2017-01-12 04:22:58.392[0;39m [32m INFO[0;39m [35m4968[0;39m [2m---[0;39m [2m[           main][0;39m [36ms.w.s.m.m.a.RequestMappingHandlerMapping[0;39m [2m:[0;39m Mapped "{[/book]}" onto public org.springframework.http.ResponseEntity<java.util.List<com.tms.dto.BookDto>> com.tms.controller.BookController.getAllBooks()
[2m2017-01-12 04:22:58.400[0;39m [32m INFO[0;39m [35m4968[0;39m [2m---[0;39m [2m[           main][0;39m [36ms.w.s.m.m.a.RequestMappingHandlerMapping[0;39m [2m:[0;39m Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
[2m2017-01-12 04:22:58.401[0;39m [32m INFO[0;39m [35m4968[0;39m [2m---[0;39m [2m[           main][0;39m [36ms.w.s.m.m.a.RequestMappingHandlerMapping[0;39m [2m:[0;39m Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
[2m2017-01-12 04:22:58.466[0;39m [32m INFO[0;39m [35m4968[0;39m [2m---[0;39m [2m[           main][0;39m [36mo.s.w.s.handler.SimpleUrlHandlerMapping [0;39m [2m:[0;39m Mapped URL path [/resources/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
[2m2017-01-12 04:22:58.476[0;39m [32m INFO[0;39m [35m4968[0;39m [2m---[0;39m [2m[           main][0;39m [36mo.s.w.s.handler.SimpleUrlHandlerMapping [0;39m [2m:[0;39m Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler]
[2m2017-01-12 04:22:58.596[0;39m [32m INFO[0;39m [35m4968[0;39m [2m---[0;39m [2m[           main][0;39m [36ms.w.s.m.m.a.RequestMappingHandlerAdapter[0;39m [2m:[0;39m Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@33d512c1: startup date [Thu Jan 12 04:22:49 IST 2017]; root of context hierarchy
[2m2017-01-12 04:23:00.111[0;39m [32m INFO[0;39m [35m4968[0;39m [2m---[0;39m [2m[           main][0;39m [36mo.s.j.e.a.AnnotationMBeanExporter       [0;39m [2m:[0;39m Registering beans for JMX exposure on startup
[2m2017-01-12 04:23:00.265[0;39m [32m INFO[0;39m [35m4968[0;39m [2m---[0;39m [2m[           main][0;39m [36ms.b.c.e.t.TomcatEmbeddedServletContainer[0;39m [2m:[0;39m Tomcat started on port(s): 8080 (http)
[2m2017-01-12 04:23:00.276[0;39m [32m INFO[0;39m [35m4968[0;39m [2m---[0;39m [2m[           main][0;39m [36mcom.tms.config.App                      [0;39m [2m:[0;39m Started App in 12.241 seconds (JVM running for 13.221)
[2m2017-01-12 04:23:01.999[0;39m [32m INFO[0;39m [35m4968[0;39m [2m---[0;39m [2m[nio-8080-exec-1][0;39m [36mo.a.c.c.C.[Tomcat].[localhost].[/]      [0;39m [2m:[0;39m Initializing Spring FrameworkServlet 'dispatcherServlet'
[2m2017-01-12 04:23:02.000[0;39m [32m INFO[0;39m [35m4968[0;39m [2m---[0;39m [2m[nio-8080-exec-1][0;39m [36mo.s.web.servlet.DispatcherServlet       [0;39m [2m:[0;39m FrameworkServlet 'dispatcherServlet': initialization started
[2m2017-01-12 04:23:02.040[0;39m [32m INFO[0;39m [35m4968[0;39m [2m---[0;39m [2m[nio-8080-exec-1][0;39m [36mo.s.web.servlet.DispatcherServlet       [0;39m [2m:[0;39m FrameworkServlet 'dispatcherServlet': initialization completed in 40 ms

When i type url http://localhost:8080/ in the browser it will give below message :

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Thu Jan 12 04:24:27 IST 2017
There was an unexpected error (type=Not Found, status=404).

Also i tried http://localhost:8080/SpringBootApp/ url in the browserit will show below message :

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Thu Jan 12 04:33:26 IST 2017
There was an unexpected error (type=Not Found, status=404).
/SpringBootApp/

No any error message printed in the STS console.

I would be thankful if anybody have solution/ to suggest other approach for the same.
M. Deinum
  • 115,695
  • 22
  • 220
  • 224
datta
  • 1
  • 3
  • 1
    Remove `SpringWebConfig`, remove `WebAppInitializer`... You are using Spring Boot that already takes care of all that. You can probably also remove the `DBConfig` by adding the appropriate configuration to `application.properties`... Spring Boot already does all the auto configuration for you, no need to do that again (and influence/fight Spring Boot). – M. Deinum Jan 12 '17 at 06:58
  • Referring the link " http://websystique.com/spring-boot/spring-boot-angularjs-spring-data-jpa-crud-app-example/" finally i achieve. – datta Jan 14 '17 at 15:59

2 Answers2

0

Your views are in /view/jsp/, but your view resolver only says /view/. Also it seems wrong to have the configuration in both application.properties, and code.

And personally I would use Thymeleaf for server side rendering these days.

Klaus Groenbaek
  • 4,820
  • 2
  • 15
  • 30
  • Thanks for your comments i also make the same changes as like project structure /WEB-INF/view/ as well as in the configuration file and .properties file still jsp not get loaded and still the same error message shown in the browser. – datta Jan 13 '17 at 13:05
0

Change

viewResolver.setPrefix("/WEB-INF/view/");

to

viewResolver.setPrefix("/WEB-INF/view/jsp/");

Ali Beadle
  • 4,486
  • 3
  • 30
  • 55
Boldbayar
  • 862
  • 1
  • 9
  • 22
  • I also keep the same changes in the project structure, configuration file and as well as application.properties file for the prefix and suffix for the jsp page but still the same issue appear in the browser. If any additional configuration i miss then tell me. – datta Jan 13 '17 at 13:14
  • Referring the link " http://websystique.com/spring-boot/spring-boot-angularjs-spring-data-jpa-crud-app-example/" finally i achieve. – datta Jan 14 '17 at 16:00