gpt4 book ai didi

spring-mvc - Spring MVC 和 Maven 集成

转载 作者:行者123 更新时间:2023-12-03 07:07:37 28 4
gpt4 key购买 nike

我正在尝试构建 Spring MVC 项目,但在尝试解决以下错误时遇到了一些麻烦。

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

我通过终端输入了以下命令:

mvn clean package
java -jar target/sprint2-0.1.0.jar

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.teamvirus.src</groupId>
<artifactId>sprint2</artifactId>
<version>0.1.0</version>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>0.5.0.M6</version>
</parent>

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

<!--><dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring3</artifactId>
</dependency>-->

</dependencies>

<properties>
<start-class>com.teamvirus.src.Application</start-class>
</properties>

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

<repositories>
<repository>
<id>spring-milestone</id>
<url>http://repo.spring.io/libs-milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>

<pluginRepositories>
<pluginRepository>
<id>spring-milestone</id>
<url>http://repo.spring.io/libs-milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>

</project>

LoginController.java

package com.teamvirus.src;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class LoginController {
@RequestMapping("/login")
public String login() {
return "login";
}

@RequestMapping("/authenticate")
public String authenticate(
@RequestParam(value = "username", required = true) String username,
@RequestParam(value = "password", required = true) String password) {
if ((username.equals("admin") && password.equals("admin")))
return "redirect:dashboard?username=" + username;
else if ((username.equals("student1") && password.equals("student1"))) {
return "redirect:dashboard?username=" + username;
}
return "wrongpassword";
}
}

感谢所提供的任何帮助。

编辑:

应用程序.java

package com.teamvirus.src;

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.context.annotation.ComponentScan;

@ComponentScan
@EnableAutoConfiguration
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

最佳答案

您完全依赖 Spring Boot 来配置所有内容,尽管这对于您当前的 View 来说应该不是问题。

默认情况下,Spring boot 会注册一个 InternalResourceViewResolver,但没有任何前缀/后缀。 (参见source)。假设您的 View 位于 /WEB-INF/views 中,并且它们是 jsp 文件,请执行以下操作

  1. src\main\resources中添加application.properties文件
  2. 添加属性spring.view.prefix,其值为/WEB-INF/views/
  3. 添加值为“.jsp”的属性 spring.view.suffix

重新打包并启动您的应用程序。

如果您没有此附加配置,/login 将返回到 /login,这将返回到 /login,这将返回到 /login。返回到 /login 这将...好吧,我想你明白了。 (login 是您从 @Controller 引用的 View 的名称)。

关于spring-mvc - Spring MVC 和 Maven 集成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20467103/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com