【发布时间】:2023-09-06 10:16:01
【问题描述】:
我正在尝试使用 Spring Boot 构建一个基本的 MVC 应用程序,Hibernate 作为 ORM,MySql 作为数据库。我面临的问题是 jsp 视图 没有得到解决。
当我尝试使用带有以下 URL 的 GET 请求获取注册表单时收到 404 错误:
http://localhost:9000/users/register/
这是我在应用程序中的设置。
目录结构:
-src
-main
-java
-com
ApplicationStart.java
-controllers
UserController.java
-repositories
UserRepository.java
-webapp
-WEB-INF
-jsp
register.jsp
-resources
application.properties
用户控制器:
@RestController
public class UserController {
private UserRepository userRepository;
@Autowired
public UserController(UserRepository userRepository)
{
this.userRepository = userRepository;
}
@RequestMapping(value = "/users/register", method = RequestMethod.GET)
public String Register()
{
return "register";
}
}
Application.properties:
server.port: 9000
spring.datasource.url:jdbc:mysql://localhost/Contacts
spring.datasource.driverClassName: com.mysql.jdbc.Driver
spring.datasource.username: 根
spring.datasource.password:
spring.view.prefix: /WEB-INF/jsp/
spring.view.suffix:.jsp
POM.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.1.3.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- HIBERNATE -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
</dependency>
<!-- MYSQL -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
</dependencies>
主类
@ComponentScan
@Configuration
@EnableAutoConfiguration
public class ApplicationStart {
public static void main(String[] args)
{
SpringApplication.run(ApplicationStart.class, args);
}
}
这是我的应用程序的当前设置。非常感谢任何有关如何解决此问题的帮助。
如果需要更多信息,请发表评论。
谢谢-
【问题讨论】:
-
试试
http://localHost:port/YourProjectName/users/register -
确保你的 pom 包装是战争而不是罐子。
标签: java jsp spring-mvc spring-boot