gpt4 book ai didi

angularjs - JHipster 中使用 Thymeleaf 和 A​​ngular 的服务器/客户端路由

转载 作者:行者123 更新时间:2023-12-02 23:50:46 24 4
gpt4 key购买 nike

背景:我是一名法国开发人员,正在团队中开发基于 JHipster 的众筹平台。

在JHipster中,访问http://domain.com/unknown时,检索到的页面是服务器端错误页面。

我想知道的是: - thymeleaf 如何知道它必须返回 error.html ? - 参数从哪里来以及它们是怎样的,特别是对于像这样的变量

<span th:text="${error}">

--> 错误从何而来(状态和消息相同)?某处是否有某种服务器端 Controller ?

另一个推论的问题:为什么 jhipster 中有 2 个错误页面,一个客户端和一个服务器?不应该只有一个客户端页面,因为 JHipster 是为 SPA 设计的。为什么服务器不总是发回index.html,让客户端测试是否有错误?

最后一个问题也是必然的:是否有某种方法可以在客户端( Angular )路由(在 app.js 文件中)请求动态服务器端(thymeleaf)模板。这对于开发丰富的 SPA 非常有用。

PS:谢谢朱利安的工作

最佳答案

What I'd like to know : - how thymeleaf knows it has to return error.html ?

两部分。首先,Spring 配置为对所有服务器端 View 使用 Thymeleaf。在 JHipster 中,Application 的注释如下:

@ComponentScan
@EnableAutoConfiguration(exclude = {MetricFilterAutoConfiguration.class, MetricRepositoryAutoConfiguration.class})
public class Application {

@EnableAutoConfiguration 是理解 Thymeleaf 如何设置的关键。它会触发 ThymeleafAutoConfiguration 创建 Thymeleaf View 解析器等。请注意,JHipster 有一个 ThymeleafConfiguration 类,但这仅用于电子邮件,与您的问题无关。

第二部分... Spring Boot 使用“/error”作为默认错误 View 。来自 the docs :

Spring Boot provides an /error mapping by default that handles all errors in a sensible way, and it is registered as a ‘global’ error page in the servlet container. For machine clients it will produce a JSON response with details of the error, the HTTP status and the exception message. For browser clients there is a ‘whitelabel’ error view that renders the same data in HTML format (to customize it just add a View that resolves to ‘error’).

在解析错误 View 时,Spring 将为您正在使用的 View 解析器添加正确的后缀(我描述的第一部分)。例如,.html 代表 Thymleaf、.vm 代表 Velocity、.ftl 代表 Freemarker。

Is there some sort of server-side controller somewhere?

是的。 JHipster 也使用 Spring Boot Actuator。查看this section of the docs 。主要兴趣类别是 ErrorMvcAutoConfiguration 。具体来说这部分:

@Bean
@ConditionalOnMissingBean(value = ErrorAttributes.class, search = SearchStrategy.CURRENT)
public DefaultErrorAttributes errorAttributes() {
return new DefaultErrorAttributes();
}

@Bean
@ConditionalOnMissingBean(value = ErrorController.class, search = SearchStrategy.CURRENT)
public BasicErrorController basicErrorController(ErrorAttributes errorAttributes) {
return new BasicErrorController(errorAttributes);
}

where does error come from (same for status and message)?

请参阅我上面引用的代码,然后查看 DefaultErrorAttributes :

Map<String, Object> errorAttributes = new LinkedHashMap<String, Object>();
errorAttributes.put("timestamp", new Date());
// and so on...

Another question which is a corollary : why is there 2 error pages in jhipster, one client and one server? Shouldn't there be just one client side page as JHipster is intended for SPA. Why isn't server always sending back index.html, letting client test is there is an error or not?

需要有一个服务器端错误处理程序。并非所有内容都会映射到 Angular 路线。例如,www.yoursite.com/doesnt_exist 会导致浏览器向服务器发出另一个请求。

Last question which is a corollary too : is there some way to request dynamic server side (thymeleaf) templates in client side (angular) routing (in app.js file). That would be great for developping enriched SPAs.

是的。您可以在app.js中为templateUrl指定JS函数。

关于angularjs - JHipster 中使用 Thymeleaf 和 A​​ngular 的服务器/客户端路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26817761/

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