Thymeleaf在模板中看不到页面HTML。

huangapple go评论80阅读模式
英文:

Thymeleaf doesn't see page html in templates

问题

我是Spring Boot应用程序开发的新手。我使用Eclipse IDE,Spring Boot版本为3.1.1,Maven项目。

问题是我的.html页面位于模板中(src/main/resources/templates),需要通过它们的链接显示(例如localhost:8080/page),为它们创建了控制器。我还尝试在错误情况下显示页面(/error),该页面位于相同的文件夹中 - 并且它显示出来了。

此外,我将提供主要方法、控制器的代码以及HTML页面的一小部分代码(它们相当大...)

.java文件中的void main方法:

@SpringBootApplication
public class ChatSbAppApplication {

    public static void main(String[] args) {
        SpringApplication.run(ChatSbAppApplication.class, args);
    }

}

错误控制器(正常工作):

@Controller
public class ErrorController implements org.springframework.boot.web.servlet.error.ErrorController {

    @RequestMapping("/error")
    public String handleError() {
        return "error";
    }

    @Override
    public String getErrorPath() {
        return "/error";
    }
}

标准控制器:

@Controller
public class UserAccountController {

    private final UserAccountService userAccountService;

    @Autowired
    public UserAccountController(UserAccountService userAccountService) {
        this.userAccountService = userAccountService;
    }
    
    ///注册
    
    @GetMapping("/register")
    public String showRegistrationForm() {
        return "register";
    }

    @PostMapping("/register")
    public String registerUser(@RequestBody UserAccount userAccount) {
        userAccountService.saveUserAccount(userAccount);
        return "redirect:/login";
    }
    
    ///授权
    
    @GetMapping("/login")
    public String loginUser() {
        return "login";
    }

    @PostMapping("/login")
    public String processLoginForm() {
        return "redirect:/chats";
    }
}

application.properties:

spring.datasource.url=jdbc:postgresql://localhost:5432/ChatDB
spring.datasource.username=postgres
spring.datasource.password=1234
spring.datasource.driver-class-name=org.postgresql.Driver
server.error.whitelabel.enabled = false
spring.jpa.open-in-view=false

我还尝试更改配置,但也不起作用。

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Bean
    public ViewResolver viewResolver() {
        ThymeleafViewResolver resolver = new ThymeleafViewResolver();
        resolver.setTemplateEngine(templateEngine());
        resolver.setCharacterEncoding("UTF-8");
        return resolver;
    }

    @Bean
    public TemplateEngine templateEngine() {
        SpringTemplateEngine engine = new SpringTemplateEngine();
        engine.setEnableSpringELCompiler(true);
        engine.setTemplateResolver(templateResolver());
        return engine;
    }

    @Bean
    public ITemplateResolver templateResolver() {
        SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
        resolver.setPrefix("classpath:/templates/");
        resolver.setSuffix(".html");
        resolver.setTemplateMode(TemplateMode.HTML);
        resolver.setCharacterEncoding("UTF-8");
        return resolver;
    }
    
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LoggingInterceptor());
    }
}

HTML示例如下:error.html可以工作,但相同的login.html无法看到。另外,Thymeleaf可以很好地看到/static中的.css.js...

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
  <title>Error Page</title>
  <link rel="stylesheet" type="text/css" th:href="@{index.css}">
</head>
<body>
  <div class="navbar">
    <div class="navbar-logo">
        <button onclick="changeColor()" class="color-button">
        </button>
    </div>
    <div style="display:flex; margin-right: 40px;">
      <button class="auth-button" onclick="window.location.href='login.html'">Login</button>
    </div>
  </div>
  <script src="index.js"></script>
</body>
</html>

我也可以显示日志,但其中没有什么令人惊讶的地方 - 没有错误或警告,也没有提到创建的映射(我看到,我的观点应该有一条消息,类似“映射已创建在路径[x.html]”之类的消息,但我没有看到这样的消息)。pom.xml中也包含了Thymeleaf依赖项。

英文:

I am new to Spring boot application development. I use eclipse IDE, spring boot v.3.1.1, maven project.

The problem is that my .html pages are in templates (src/main/resources/templates), which need to be displayed by their link (like a localhost:8080/page), controllers were created for them. I also tried to display the page in case of an error (/error), which is located in the same folder - and it is displayed.

In addition, I will give the code of the main method, controllers, and a small code of the html pages (they are quite large ...)

.java file with void main:

@SpringBootApplication
public class ChatSbAppApplication {

	public static void main(String[] args) {
		SpringApplication.run(ChatSbAppApplication.class, args);
	}

}

Error controller (works fine):

@Controller
public class ErrorController implements org.springframework.boot.web.servlet.error.ErrorController {

    @RequestMapping(&quot;/error&quot;)
    public String handleError() {
        return &quot;error&quot;;
    }

    @Override
    public String getErrorPath() {
        return &quot;/error&quot;;
    }
}

Standard controllers:

@Controller
public class UserAccountController {

    private final UserAccountService userAccountService;

    @Autowired
    public UserAccountController(UserAccountService userAccountService) {
        this.userAccountService = userAccountService;
    }
    
    ///registration
    
    @GetMapping(&quot;/register&quot;)
    public String showRegistrationForm() {
        return &quot;register&quot;;
    }

    @PostMapping(&quot;/register&quot;)
    public String registerUser(@RequestBody UserAccount userAccount) {
        userAccountService.saveUserAccount(userAccount);
        return &quot;redirect:/login&quot;;
    }
    
    ///authorisation
    
    @GetMapping(&quot;/login&quot;)
    public String loginUser() {
        return &quot;login&quot;;
    }

    @PostMapping(&quot;/login&quot;)
    public String processLoginForm() {
        return &quot;redirect:/chats&quot;;
    }
}

application.properties:

spring.datasource.url=jdbc:postgresql://localhost:5432/ChatDB
spring.datasource.username=postgres
spring.datasource.password=1234
spring.datasource.driver-class-name=org.postgresql.Driver
server.error.whitelabel.enabled = false
spring.jpa.open-in-view=false

I also tried changing the config, but it didn't work either.

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Bean
    public ViewResolver viewResolver() {
        ThymeleafViewResolver resolver = new ThymeleafViewResolver();
        resolver.setTemplateEngine(templateEngine());
        resolver.setCharacterEncoding(&quot;UTF-8&quot;);
        return resolver;
    }

    @Bean
    public TemplateEngine templateEngine() {
        SpringTemplateEngine engine = new SpringTemplateEngine();
        engine.setEnableSpringELCompiler(true);
        engine.setTemplateResolver(templateResolver());
        return engine;
    }

    @Bean
    public ITemplateResolver templateResolver() {
        SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
        resolver.setPrefix(&quot;classpath:/templates/&quot;);
        resolver.setSuffix(&quot;.html&quot;);
        resolver.setTemplateMode(TemplateMode.HTML);
        resolver.setCharacterEncoding(&quot;UTF-8&quot;);
        return resolver;
    }
    
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LoggingInterceptor());
    }
}

HTML example below: error.html works, but same login.html does not see. Also thymeleaf saw fine .css/.js in /static...

&lt;!DOCTYPE html&gt;
&lt;html xmlns:th=&quot;http://www.thymeleaf.org&quot;&gt;
&lt;head&gt;
  &lt;title&gt;Error Page&lt;/title&gt;
  &lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; th:href=&quot;@{index.css}&quot;&gt;
&lt;/head&gt;
&lt;body&gt;
  &lt;div class=&quot;navbar&quot;&gt;
    &lt;div class=&quot;navbar-logo&quot;&gt;
        &lt;button onclick=&quot;changeColor()&quot; class=&quot;color-button&quot;&gt;
        &lt;/button&gt;
    &lt;/div&gt;
    &lt;div style=&quot;display:flex; margin-right: 40px;&quot;&gt;
      &lt;button class=&quot;auth-button&quot; onclick=&quot;window.location.href=&#39;login.html&#39;&quot;&gt;Login&lt;/button&gt;
    &lt;/div&gt;
  &lt;/div&gt;
  &lt;script src=&quot;index.js&quot;&gt;&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;

I can also show the logs, but there is nothing surprising in them - not 1 error or warning, but not 1 mention of the created mapping (I read, in my opinion there should be a message, like "mapping was created along the path [x.html]" or something like that, I didn't have such a message). The thymeleaf dependency is also in pom.xml Thymeleaf在模板中看不到页面HTML。

As I described above, I tried to change the thymeleaf config, but it didn't work. Also I read some other people's problems and tried to "Project->Update Maven Project" (Alt+F5) but it didn't help too

答案1

得分: 0

使用 window.location 打开链接需要设置完整路径。

尝试替换:

<button class="auth-button" onclick="window.location.href='login.html'">Login</button>

为:

<button class="auth-button" onclick="window.location.href='http://localhost:8080/login'">Login</button>

还请注意,不应包含 .html 扩展名,因为控制器在解析模板时会处理此部分。

英文:

Opening link using window.location will require you to set complete path.

Try to replace

&lt;button class=&quot;auth-button&quot; onclick=&quot;window.location.href=&#39;login.html&#39;&quot;&gt;Login&lt;/button&gt;

With

&lt;button class=&quot;auth-button&quot; onclick=&quot;window.location.href=&#39;http://localhost:8080/login&#39;&quot;&gt;Login&lt;/button&gt;

Also note you should not include .html extension since controller takes care of this when parsing templates

huangapple
  • 本文由 发表于 2023年7月7日 02:05:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76631478.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定