微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

关于使用jsp

如何解决关于使用jsp

我是Java和Spring Boot的新手。现在,我正在使用Spring Boot,安全性和MVC基于视频进行项目。我正在使用MysqL DB。通过跟踪该视频,我已经成功地使用邮递员完成了该任务。我想使用jsp文件执行该项目。但是我不知道该怎么做。我遇到了500个错误(异常处理模板“索引”:错误解析模板[index],模板可能不存在,或者任何配置的模板解析器都无法访问该模板)。有人可以给我任何解决方案!

@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>

解决方法

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

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

如果我正确记得spring-mvc的工作方式,则在@Controller中使用返回String的方法时,spring会查找一个视图,并将返回的String作为其ID。如果找到,它将转发到该视图。

但是,

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。