簡體   English   中英

Java 中 Spring Boot Web 應用程序的問題?

[英]Problems with Spring Boot web application in Java?

我對 Spring Boot 很陌生。 我正在閱讀文檔並嘗試玩弄東西。 我喜歡不必對 Spring MVC 應用程序中的配置內容做任何事情的整個想法,但后來我試圖通過將數據放入模型對象來將我的控制器與我的視圖連接起來。 但出於某種原因,我沒有在視圖中得到我想要的東西。 有人可以指出我的錯誤。 這是我的控制器類-

package com.example.LoginAndRegistration;

import java.util.Map;

import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class LoginController {

    @RequestMapping("/Login")
    public String welcome(Model model) {
        model.addAttribute("login", "Hello World");
        return "loginview";
    }

}

我使用 Thymeleaf 將我的模型對象渲染到我的視圖中。 我通過 Maven 添加了 thymeleaf 依賴項,然后在我的 Springboot 應用程序的模板文件夾中添加了以下 html 文件。 但是我仍然沒有收到我從控制器傳遞的消息。 而是將 loginview 作為字符串打印出來,而不是在我的模板文件夾中查找 loginview 並打印出消息 HelloWorld。

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="ISO-8859-1"></meta>
<title>Insert title here</title>
</head>
<body>
Message: <span th:text="${login}"></span>
</body>
</html>

這些是我在日志中看到的-

2017-06-18 11:33:30.508  INFO 3112 --- [           main] c.e.L.LoginAndRegistrationApplication    : Starting LoginAndRegistrationApplication on VAIO with PID 3112 (C:\Users\user\javaprojects\LoginAndRegistration\target\classes started by user in C:\Users\user\javaprojects\LoginAndRegistration)
2017-06-18 11:33:30.534  INFO 3112 --- [           main] c.e.L.LoginAndRegistrationApplication    : No active profile set, falling back to default profiles: default
2017-06-18 11:33:30.818  INFO 3112 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@58134517: startup date [Sun Jun 18 11:33:30 EAT 2017]; root of context hierarchy
2017-06-18 11:33:34.615  INFO 3112 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2017-06-18 11:33:34.641  INFO 3112 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2017-06-18 11:33:34.644  INFO 3112 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.15
2017-06-18 11:33:34.983  INFO 3112 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2017-06-18 11:33:34.983  INFO 3112 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 4198 ms
2017-06-18 11:33:35.402  INFO 3112 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: 'dispatcherServlet' to [/]
2017-06-18 11:33:35.410  INFO 3112 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2017-06-18 11:33:35.412  INFO 3112 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2017-06-18 11:33:35.412  INFO 3112 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2017-06-18 11:33:35.413  INFO 3112 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2017-06-18 11:33:36.071  INFO 3112 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@58134517: startup date [Sun Jun 18 11:33:30 EAT 2017]; root of context hierarchy
2017-06-18 11:33:36.215  INFO 3112 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/Login]}" onto public java.lang.String com.example.LoginAndRegistration.LoginController.welcome(org.springframework.ui.Model)
2017-06-18 11:33:36.222  INFO 3112 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2017-06-18 11:33:36.223  INFO 3112 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2017-06-18 11:33:36.290  INFO 3112 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-06-18 11:33:36.290  INFO 3112 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-06-18 11:33:36.370  INFO 3112 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-06-18 11:33:36.761  INFO 3112 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2017-06-18 11:33:36.871  INFO 3112 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2017-06-18 11:33:36.878  INFO 3112 --- [           main] c.e.L.LoginAndRegistrationApplication    : Started LoginAndRegistrationApplication in 7.793 seconds (JVM running for 13.483)
2017-06-18 11:33:54.561  INFO 3112 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring FrameworkServlet 'dispatcherServlet'
2017-06-18 11:33:54.561  INFO 3112 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization started
2017-06-18 11:33:54.754  INFO 3112 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization completed in 193 ms

項目

@RestController更改為@Controller 當 bean 用@RestController注釋時,Spring 假定控制器的每個方法都用@ResponseBody注釋。

你會嘗試克雷婭察的子包com.example.LoginAndRegistration作為com.example.LoginAndRegistration.controller和移動你loginController的呢? 我有同樣的問題並解決了這樣的問題

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM