简体   繁体   English

Java 中 Spring Boot Web 应用程序的问题?

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

I am very new to Spring Boot.我对 Spring Boot 很陌生。 I was reading the documentation and trying to play around with stuffs.我正在阅读文档并尝试玩弄东西。 I like the whole idea of not having to do anything with the configuration stuffs like in Spring MVC app but then I was trying to connect my Controller with my View by putting data in a Model object.我喜欢不必对 Spring MVC 应用程序中的配置内容做任何事情的整个想法,但后来我试图通过将数据放入模型对象来将我的控制器与我的视图连接起来。 But for some reason, I am not getting what I am looking for in the View.但出于某种原因,我没有在视图中得到我想要的东西。 Can someone please point my error.有人可以指出我的错误。 This is my controller class-这是我的控制器类-

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";
    }

}

I have used Thymeleaf to render my model object to my view.我使用 Thymeleaf 将我的模型对象渲染到我的视图中。 I added the thymeleaf dependency through Maven and then added the following html file in my templates folder of my Springboot application.我通过 Maven 添加了 thymeleaf 依赖项,然后在我的 Springboot 应用程序的模板文件夹中添加了以下 html 文件。 But I still dont get the message that I am passing from my controller.但是我仍然没有收到我从控制器传递的消息。 Rather it prints out loginview as string rather than looking up for loginview in my template folder and printing out the message HelloWorld.而是将 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>

These is what I see in my logs-这些是我在日志中看到的-

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

项目

Change @RestController to @Controller .@RestController更改为@Controller When a bean is annotated with @RestController , Spring assumes that each method of a controller is annotated with @ResponseBody .当 bean 用@RestController注释时,Spring 假定控制器的每个方法都用@ResponseBody注释。

Would you try to creata a subpackage of com.example.LoginAndRegistration as com.example.LoginAndRegistration.controller and move your loginController in it?你会尝试克雷娅察的子包com.example.LoginAndRegistration作为com.example.LoginAndRegistration.controller和移动你loginController的呢? I had same problem and solved like that我有同样的问题并解决了这样的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM