Spring Boot - 找不到JSP视图

时间:2017-05-05 18:43:25

标签: java maven jsp spring-mvc spring-boot

我正在寻找如何使用spring boot运行jsp。我试了好几次但仍未找到。

然后我做了这个配置:

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

当我访问网址时:“http://localhost:8099/App/login

我有这样的信息:

  

o.s.web.servlet.PageNotFound:找不到HTTP请求的映射   带有名称的DispatcherServlet中的URI [/App/WEB-INF/jsp/login.jsp]   '的DispatcherServlet'

可以在以下位置找到jsp页面: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>

jsp页面的网址: src / main / webapp / WEB-INF /jsp/login.jsp

文件的URL xml: src / main / webapp / WEB-INF /web.xml,dispatcher.xml

我有这个问题:

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

感谢您帮助我在服务器中运行login.jsp页面。

1 个答案:

答案 0 :(得分:0)

我没有看到你的完整 pom.xml ,但是对我来说有用(根据评论中提到的Spring JSP Limitations)是将包装从JAR更改为WAR。

<packaging>war</packaging>