【问题标题】:There was an unexpected error (404 Not Found) JSP and Spring Boot出现意外错误(404 Not Found)JSP 和 Spring Boot
【发布时间】:2018-12-24 12:29:50
【问题描述】:

当我尝试将一个项目翻译成 Spring Boot 时,我得到一个错误——我无法理解我做错了什么。 添加到property-file中的后缀和前缀,试图改变jsp的位置,debager显示在controller的方法中来了。 我做错了什么?如果有任何帮助,我将不胜感激

错误:

Whitelabel 错误页面 此应用程序没有明确的映射 /error,因此您将其视为后备。

2018 年 7 月 17 日星期二 00:45:23 EEST 出现意外错误(type=Not 找到,状态=404)。 /Authorization.jsp

我目前的项目结构: pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>com.solopov.hillel</groupId>
 <artifactId>uquiz</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <packaging>war</packaging>

 <name>uquiz</name>
 <description>The project allows you to create and conduct surveys to anyone who wants</description>

 <parent>
  <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-parent</artifactId>
  <version>2.0.3.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-security</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-test</artifactId>
   <scope>test</scope>
  </dependency>
  <dependency>
   <groupId>org.springframework.security</groupId>
   <artifactId>spring-security-test</artifactId>
   <scope>test</scope>
  </dependency>
  <dependency>
   <groupId>org.springframework.security</groupId>
   <artifactId>spring-security-taglibs</artifactId>
  </dependency>
  <dependency>
   <groupId>mysql</groupId>
   <artifactId>mysql-connector-java</artifactId>
  </dependency>
  <dependency>
   <groupId>org.apache.tomcat.embed</groupId>
   <artifactId>tomcat-embed-jasper</artifactId>
  </dependency>
  <dependency>
   <groupId>javax.servlet</groupId>
   <artifactId>jstl</artifactId>
  </dependency>
 </dependencies>

 <build>
  <plugins>
   <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
   </plugin>
  </plugins>
 </build>


</project>

Spring Boot application.properties:

logging.level.root = info

spring.mvc.view.preffix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp

spring.datasource.url=jdbc:mysql://localhost:3306/quizzes?autoReconnect=true&useSSL=false
spring.datasource.username=root
spring.datasource.password=password
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=none
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect
spring.jpa.properties.hibernate.current_session_context_class=org.springframework.orm.hibernate5.SpringSessionContext

Sping Security 配置:(以防万一)

package com.solopov.hillel.uquiz.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import javax.sql.DataSource;

@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
    @Autowired
    DataSource dataSource;

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth.jdbcAuthentication().dataSource(dataSource).passwordEncoder(new BCryptPasswordEncoder())
            .usersByUsernameQuery("select login,password,true from user where login=?")
             .authoritiesByUsernameQuery("select login, role from user where login=?");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http    .authorizeRequests()
                .antMatchers("/auth", "/reg","/welcomepage").permitAll()
                .antMatchers("/admin/**").hasAuthority("admin")
                .anyRequest().authenticated().and()
                .formLogin()
                .loginPage("/auth").usernameParameter("login")
                .permitAll()
                .and()
                .logout().logoutSuccessUrl("/auth")
                .and().csrf().disable();
    }
}

控制器方法:

@RequestMapping(method = GET, value = "/auth")
    public String authorization() {
        return "Authorization";
    }

开始上课:

@SpringBootApplication
public class UquizApplication {

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

【问题讨论】:

  • @AmithKumar 不,不是。它看起来像这样。实际上,目录 pats 和其他 jsp 处于同一级别。在该文件夹中是 header.jsp 和 footer.jsp。
  • @AmithKumar 我有 Spring Boot 应用程序:我的配置是 application.properties,我的应用程序从 UquizApplication 类开始,我是这样做的
  • @AmithKumar 网址,我在打什么:/auth

标签: java spring spring-boot


【解决方案1】:

您将请求映射到“/auth”并访问请求“Authorization.jsp”。您的请求应该是 localhost:port/auth

【讨论】:

  • 为什么?我们必须通过 get-request 进入 jsp 页面。如果我们重定向到地址——它如何理解必须访问 Authorization.jsp?
  • 地址,如:localhost:8080/auth——由Spring Boot内置的Apache Tomcat组成
  • 如果你想直接访问 jsp 页面然后使用 curl 就像 ip:port/ : - localhost:9109/tiles/common/body.jsp
  • 我认为位置jsp-页面的结构和前缀有问题。应该在没有“tiles / common”的情况下工作
【解决方案2】:

请更正您的属性文件中的错字: spring.mvc.view.preffixspring.mvc.view.prefix

【讨论】:

  • 谢谢!这就是解决方案!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-05-30
  • 2018-07-19
  • 2017-09-03
  • 1970-01-01
  • 1970-01-01
  • 2015-07-31
  • 2020-10-21
相关资源
最近更新 更多