1
votes

I'm looking for how to run the jsp using spring boot. I tried several times but still not found.

I then made this configuration:

-My dependencies of pom.xml:

<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-web</artifactId>
        </dependency>

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

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

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

        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-server</artifactId>
            <version>1.8</version>
        </dependency>
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-servlet</artifactId>
            <version>1.17.1</version>
        </dependency>
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-json</artifactId>
            <version>1.8</version>
        </dependency>
        <dependency>
            <groupId>com.sun.jersey.contribs</groupId>
            <artifactId>jersey-multipart</artifactId>
            <version>1.8</version>
        </dependency>

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

    <dependency>
    <groupId>javax.ws.rs</groupId>
    <artifactId>javax.ws.rs-api</artifactId>
    <version>2.0</version>
  </dependency>

   <!-- https://mvnrepository.com/artifact/org.apache.pdfbox/pdfbox -->
     <dependency>
          <groupId>org.apache.pdfbox</groupId>
          <artifactId>pdfbox</artifactId>
          <version>2.0.1</version>
      </dependency>

   <!-- https://mvnrepository.com/artifact/com.itextpdf/itextpdf -->
       <dependency>
           <groupId>com.itextpdf</groupId>
           <artifactId>itextpdf</artifactId>
           <version>5.0.6</version>
        </dependency>

      <!-- https://mvnrepository.com/artifact/org.glassfish.jersey.core/jersey-server -->
       <dependency>
         <groupId>org.glassfish.jersey.core</groupId>
         <artifactId>jersey-server</artifactId>
         <version>2.17</version>
       </dependency>

        <!--the SL4J dependencies.  -->
        <dependency>
              <groupId>org.slf4j</groupId>
              <artifactId>slf4j-api</artifactId>
              <version>1.7.19</version>
        </dependency>
        <dependency>
              <groupId>org.apache.logging.log4j</groupId>
              <artifactId>log4j-slf4j-impl</artifactId>
              <version>2.5</version>
        </dependency>

        <!-- the Log4J 2 dependencies. -->   
        <dependency>
             <groupId>org.apache.logging.log4j</groupId>
             <artifactId>log4j-api</artifactId>
             <version>2.5</version>
        </dependency>
        <dependency>
             <groupId>org.apache.logging.log4j</groupId>
             <artifactId>log4j-core</artifactId>
             <version>2.5</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-jcl</artifactId>
            <version>2.5</version>
       </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web-services</artifactId>
        </dependency>

        <!-- Spring security -->
          <dependency>
              <groupId>org.springframework.security</groupId>
               <artifactId>spring-security-web</artifactId>
               <version>4.0.3.RELEASE</version>
          </dependency>

          <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
            <version>4.0.3.RELEASE</version>
         </dependency>

           <!-- Spring Security JSP Taglib -->
         <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-taglibs</artifactId>
            <version>4.1.4.RELEASE</version>
         </dependency>

        <!-- To compile JSP -->
        <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
        <scope>provided</scope>
    </dependency>
    <!-- jstl for jsp page -->
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>${jstl.version}</version>
        </dependency>

    </dependencies>

-class MvcConfig:

 @Configuration
    @EnableWebMvc
    @ComponentScan
    public class MvcConfig  extends WebMvcConfigurerAdapter{
        @Override
        public void configureViewResolvers(ViewResolverRegistry registry) {
            InternalResourceViewResolver resolver = new InternalResourceViewResolver();
            resolver.setPrefix("/WEB-INF/Views/");
            resolver.setSuffix(".jsp");
            resolver.setViewClass(JstlView.class);
            registry.viewResolver(resolver);
        }
     @Override
        public void addViewControllers(ViewControllerRegistry registry) {
          registry.addViewController("/login").setViewName("login");
    }

-class WebSecurityConfig :

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled=true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter{
protected void configure(HttpSecurity http) throws Exception {
     http.authorizeRequests()
         .antMatchers("/Users/**").access("hasRole('ADMIN')")
          .anyRequest().authenticated()
          .and()
            .formLogin().loginPage("/login").permitAll()
            .defaultSuccessUrl("/Acceuil")
          .and()
            .logout().logoutSuccessUrl("/login?logout") 
           .and()
           .exceptionHandling().accessDeniedPage("/403")
          .and()
            .csrf();
}
}

-class AppsApplication :

@SpringBootApplication
@ComponentScan
@EnableAutoConfiguration
public class AppsApplication extends SpringBootServletInitializer{

protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
            return application.sources(AuditConfigurationApplication.class);
        }

    public static void main(String[] args) {
        SpringApplication.run(AuditConfigurationApplication.class, args);



    }
}

-application.properties:

spring.mvc.view.prefix= /WEB-INF/Views/
spring.mvc.view.suffix= .jsp

When I access a url: "http://localhost:8099/App/login"

I have this message:

o.s.web.servlet.PageNotFound: No mapping found for HTTP request with URI [/App/WEB-INF/jsp/login.jsp] in DispatcherServlet with name 'dispatcherServlet'

The jsp page can be found in: src / main / webapp / WEB-INF /jsp/login.jsp

Edit1:

dispatcher.xml :

<beans xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="
   http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:component-scan base-package="com.SSC"/>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

web.xml :

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         id="WebApp_ID" version="3.1">
     <display-name>JSP</display-name>
    <welcome-file-list>
        <welcome-file>login.html</welcome-file>
        <welcome-file>login.htm</welcome-file>
        <welcome-file>login.jsp</welcome-file>
    </welcome-file-list>
    <!-- Spring Boot Servlets -->
    <servlet>
        <servlet-name>SpringBootWebXmlServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/dispatcher.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>SpringBootWebXmlServlet</servlet-name>
        <url-pattern>/login</url-pattern>
    </servlet-mapping>
</web-app>

url of jsp pages: src / main / webapp / WEB-INF /jsp/login.jsp

url of files xml: src / main / webapp / WEB-INF /web.xml , dispatcher.xml

I have this problem :

Circular view path [login]: would dispatch back to the current handler URL [/App/login] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)

Thanks for helping me to run the login.jsp page in the server.

1
baeldung.com/spring-dispatcherservlet - here's an article at Baeldung that goes into details about setting up a JSPAdam Gerard
Thanks for the link @Adam Gerard, I applied the "3.3. The ViewResolver Interface" part of link baeldung. I have this message: o.s.web.servlet.PageNotFound: No mapping found for HTTP request with URI [/App/WEB-INF/jsp/login.jsp] in DispatcherServlet with name 'dispatcherServlet' The jsp page can be found in: src / main / webapp / WEB-INF / jsp / login.jspMichael1
Have you read the JSP limitations section of the Boot doc? docs.spring.io/spring-boot/docs/current/reference/htmlsingle/…JB Nizet
It is necessary to define a controller that returns the names of the jsp pages?Michael1
Hey @Michel - I think that the next problem may have to do with the actual dispatcherServlet mappings - at least, I've had that problem arise when I haven't configured the dispatcherServlet correctly: baeldung.com/register-servletAdam Gerard

1 Answers

0
votes

I don't see your full pom.xml, but what worked for me (according to Spring JSP Limitations mentioned in comments) was to change packaging from JAR to WAR.

<packaging>war</packaging>