【问题标题】:About Spring Boot with jsp关于使用 jsp 的 Spring Boot
【发布时间】:2021-01-23 14:07:24
【问题描述】:

我是 Java 和 Spring Boot 的新手。现在我正在使用 Spring boot、security 和 MVC 做一个基于视频的项目。我正在使用 MySql 数据库。我已经按照那个视频成功地完成了使用邮递员的工作。我想用jsp文件做那个项目。但我不知道该怎么做。我收到了一些 500 错误(异常处理模板“索引”:解析模板 [索引] 时出错,模板可能不存在或可能无法被任何配置的模板解析器访问)。谁能给我任何解决方案!

@RestController
@RequestMapping("/api/auth")
@AllArgsConstructor
public class AuthController {

private final AuthService authService;
private final RefreshTokenService refreshTokenService;

@RequestMapping(value="/")
public String index(Model model) throws IOException{
    System.out.println("Spring Reddit Clone");
    String message="Welcome To Spring Reddit Clone!!!!!!";
    System.out.println(message);
    model.addAttribute("welcomeMessage", message);
    return "index";
}
/*
@RequestMapping(value="/",method=RequestMethod.GET)
public String index(ModelMap m){
    System.out.println("Spring Reddit Clone");
    String message="Welcome To Spring Reddit Clone!!!!!!";
    System.out.println(message);
    m.addAttribute("welcomeMessage", message);
    return "index";
}*/

@PostMapping("/signup")
public String signup(@RequestBody RegisterRequest registerRequest) {
      authService.signup(registerRequest);
    return "index.jsp";
}

@PostMapping("/login")
public AuthenticationResponse login(@RequestBody LoginRequest loginRequest){
    return authService.login(loginRequest);
}

@GetMapping("accountVerification/{token}")
public ResponseEntity<String> verifyAccount(@PathVariable String token) {
    authService.verifyAccount(token);
    return new ResponseEntity<>("Account Activated Successfully", OK);
}

@PostMapping("refresh/token")
public AuthenticationResponse refreshToken(@Valid @RequestBody RefreshTokenRequest 
refreshTokenRequest){
    return authService.refreshToken(refreshTokenRequest);
}

@PostMapping("/logout")
public ResponseEntity<String> logout(@Valid @RequestBody RefreshTokenRequest refreshTokenRequest){
    refreshTokenService.deleteRefreshToken(refreshTokenRequest.getRefreshToken());
    return ResponseEntity.status(OK).body("Refresh Token Deleted Successfully!!!!");
}

}

这是我的 jsp...

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<html  xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>SpringRedditClone!!!</title>
</head>
<body>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

${welcomeMessage}
<font face="verdana" size="2">
     <h2>Spring Reddit Clone!!!!!</h2>
  </font>

  <div><a href="login.jsp">Login</a> </div>

  <div><a href="signup.jsp">Signup</a></div>
 </body>
 </html>

但我得到的是“索引”字符串而不是 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.3.3.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<packaging>war</packaging>
<artifactId>reddit_clone</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>reddit_clone</name>
<description>Demo project for Spring Boot</description>

<properties>
    <java.version>11</java.version>
    <org.mapstruct.version>1.3.1.Final</org.mapstruct.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-mail</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>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-test</artifactId>
        <scope>test</scope>
    </dependency>
    <!-- JWT related dependencies-->
    <dependency>
        <groupId>io.jsonwebtoken</groupId>
        <artifactId>jjwt-api</artifactId>
        <version>0.10.5</version>
    </dependency>
    <dependency>
        <groupId>io.jsonwebtoken</groupId>
        <artifactId>jjwt-impl</artifactId>
        <scope>runtime</scope>
        <version>0.10.5</version>
    </dependency>
    <dependency>
        <groupId>io.jsonwebtoken</groupId>
        <artifactId>jjwt-jackson</artifactId>
        <scope>runtime</scope>
        <version>0.10.5</version>
    </dependency>
    <!-- For Displaying time as Relative Time Ago ("Posted 1 Day ago"),
     as this is a Kotlin library, we also need Kotlin runtime libraries-->
    <dependency>
        <groupId>com.github.marlonlom</groupId>
        <artifactId>timeago</artifactId>
        <version>4.0.1</version>
    </dependency>
    <dependency>
        <groupId>org.jetbrains.kotlin</groupId>
        <artifactId>kotlin-stdlib-jdk8</artifactId>
        <version>${kotlin.version}</version>
    </dependency>
    <dependency>
        <groupId>org.jetbrains.kotlin</groupId>
        <artifactId>kotlin-test</artifactId>
        <version>${kotlin.version}</version>
        <scope>test</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/javax.persistence/javax.persistence-api -->
    <dependency>
        <groupId>javax.persistence</groupId>
        <artifactId>javax.persistence-api</artifactId>
        <version>2.2</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/javax.validation/validation-api -->
    <dependency>
        <groupId>javax.validation</groupId>
        <artifactId>validation-api</artifactId>
        <version>2.0.1.Final</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <dependency>
        <groupId>org.mapstruct</groupId>
        <artifactId>mapstruct</artifactId>
        <version>${org.mapstruct.version}</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-jasper -->
    <dependency>
        <groupId>org.apache.tomcat</groupId>
        <artifactId>tomcat-jasper</artifactId>
        <version>9.0.37</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>5.2.3.RELEASE</version>
    </dependency>


</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.5.1</version> <!-- or newer version -->
            <configuration>
                <source>1.8</source> <!-- depending on your project -->
                <target>1.8</target> <!-- depending on your project -->
                <annotationProcessorPaths>
                    <path>
                        <groupId>org.mapstruct</groupId>
                        <artifactId>mapstruct-processor</artifactId>
                        <version>${org.mapstruct.version}</version>
                    </path>
                    <path>
                        <groupId>org.projectlombok</groupId>
                        <artifactId>lombok</artifactId>
                        <version>1.18.8</version>
                    </path>
                </annotationProcessorPaths>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-maven-plugin</artifactId>
            <version>${kotlin.version}</version>
            <executions>
                <execution>
                    <id>compile</id>
                    <phase>process-sources</phase>
                    <goals>
                        <goal>compile</goal>
                    </goals>
                    <configuration>
                        <sourceDirs>
                            <source>src/main/java</source>
                            <source>target/generated-sources/annotations</source>
                        </sourceDirs>
                    </configuration>
                </execution>
            </executions>
        </plugin>

    </plugins>
</build>

【问题讨论】:

    标签: spring spring-boot jsp controller pom.xml


    【解决方案1】:

    您的“索引”方法看起来不错。

    但我认为您的问题来自使用@RestController 而不是@Controller。

    如果我没记错 spring-mvc 是如何工作的,当在 @Controller 中使用返回 String 的方法时,spring 会寻找一个视图,返回的 String 作为它的 ID。如果找到,它将转发到该视图。

    @RestController 和/或 ResponseEntity 但是不转发到视图。 它们返回一些序列化的数据,这些数据通常应该被当前页面操作以进行 DOM 操作。

    【讨论】:

    • 尝试返回 ModelAndView 的实例而不是 String。保留 Controller 而不是 RestController
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-06
    • 1970-01-01
    • 2018-10-30
    • 2017-11-27
    • 2019-04-04
    • 2018-01-13
    • 2014-06-11
    相关资源
    最近更新 更多