小编Mah*_*zad的帖子

如何避免Spring MVC测试的"循环视图路径"异常

我的一个控制器中有以下代码:

@Controller
@RequestMapping("/preference")
public class PreferenceController {

    @RequestMapping(method = RequestMethod.GET, produces = "text/html")
    public String preference() {
        return "preference";
    }
}
Run Code Online (Sandbox Code Playgroud)

我只是尝试使用Spring MVC测试来测试它,如下所示:

@ContextConfiguration
@WebAppConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class PreferenceControllerTest {

    @Autowired
    private WebApplicationContext ctx;

    private MockMvc mockMvc;
    @Before
    public void setup() {
        mockMvc = webAppContextSetup(ctx).build();
    }

    @Test
    public void circularViewPathIssue() throws Exception {
        mockMvc.perform(get("/preference"))
               .andDo(print());
    }
}
Run Code Online (Sandbox Code Playgroud)

我收到以下异常:

循环视图路径[preference]:将再次调度回当前处理程序URL [/ preference].检查您的ViewResolver设置!(提示:由于默认的视图名称生成,这可能是未指定视图的结果.)

我发现奇怪的是,当我加载包含模板和视图解析器的"完整"上下文配置时,它工作正常,如下所示:

<bean class="org.thymeleaf.templateresolver.ServletContextTemplateResolver" id="webTemplateResolver">
    <property name="prefix" value="WEB-INF/web-templates/" />
    <property name="suffix" value=".html" …
Run Code Online (Sandbox Code Playgroud)

spring spring-mvc circular-reference thymeleaf spring-mvc-test

96
推荐指数
10
解决办法
16万
查看次数

Spring MVC 3.2 Thymeleaf Ajax Fragments

我正在使用Spring MVC 3.2和Thymeleaf模板引擎构建应用程序.我是Thymeleaf的初学者.

我有一切正常工作,包括Thymeleaf,但我想知道是否有人知道如何对控制器执行简单的Ajax请求的简单明了的结果,并且结果只渲染模板(片段)的一部分.

我的应用程序配置了所有内容(Spring 3.2,spring-security,thymeleaf,...)并按预期工作.现在我想做Ajax请求(使用jQuery非常简单,但我不想使用是因为Thymeleaf在其教程中,第11章:渲染模板片段(链接)提到它可以用片段完成.

目前我在我的控制器中

@RequestMapping("/dimensionMenuList")
public String showDimensionMenuList(Model model) {

    Collection<ArticleDimensionVO> articleDimensions;
    try {
        articleDimensions = articleService.getArticleDimension(ArticleTypeVO.ARTICLE_TYPE);
    } catch (DataAccessException e) {
        // TODO: return ERROR
        throw new RuntimeException();
    }

    model.addAttribute("dimensions", articleDimensions);

    return "/admin/index :: dimensionMenuList";
}
Run Code Online (Sandbox Code Playgroud)

我希望替换<ul></ul>菜单项的视图部分:

<ul th:fragment="dimensionMenuList" class="dropdown-menu">
    <li th:unless="${#lists.isEmpty(dimensions)}" th:each="dimension : ${dimensions}">
        <a href="#" th:text="${dimension.dimension}"></a>
    </li>
</ul>
Run Code Online (Sandbox Code Playgroud)

任何线索都非常感谢.特别是如果我不必包含任何更多的框架.对于Java Web应用程序来说已经太多了.

ajax spring spring-mvc thymeleaf

46
推荐指数
3
解决办法
5万
查看次数

在Thymeleaf格式化日期

我是Java/Spring/Thymeleaf的新手,所以请关注我目前的理解水平.我确实审查了这个类似的问题,但无法解决我的问题.

我想要一个简化的日期而不是长日期格式.

// DateTimeFormat annotation on the method that's calling the DB to get date.
@DateTimeFormat(pattern="dd-MMM-YYYY")
public Date getReleaseDate() {
    return releaseDate;
}
Run Code Online (Sandbox Code Playgroud)

HTML:

<table>
    <tr th:each="sprint : ${sprints}">
        <td th:text="${sprint.name}"></td>
        <td th:text="${sprint.releaseDate}"></td>
    </tr>
</table>
Run Code Online (Sandbox Code Playgroud)

电流输出

sprint1 2016-10-04 14:10:42.183
Run Code Online (Sandbox Code Playgroud)

java datetime-format thymeleaf spring-boot

41
推荐指数
3
解决办法
5万
查看次数

Thymeleaf模板 - 有没有办法装饰模板而不是包含模板片段?

我是第一次与Thymeleaf合作,我需要澄清模板.如果我正确理解文档,我可以在我的页面中包含一个模板 - 或者只是一个片段.例如,我可以这样写:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
    <head th:include="template/layout :: header">
    </head>
    <body>
        Hello world
        <div th:include="template/layout :: footer"></div>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

但我想要的实际上是使用模板的相反方式:不是在页面中包含模板片段,我想我的模板中包含页面,类似于:

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
    ...
</head>
<body>

    <div id="my-template-header">...</div>

    <div id="the-content">
        <!-- include here the content of the current page visited by the user -->
        ???
    </div>

    <div id="my-template-footer">...</div>
</body>
Run Code Online (Sandbox Code Playgroud)

换句话说,有没有办法在Thymeleaf中拥有等效的Sitemesh装饰器标签

谢谢

java spring-mvc thymeleaf

38
推荐指数
2
解决办法
3万
查看次数

Kotlin 协程:通道与流

我最近在学习和阅读很多关于Flow和 Kotlin 协程的内容。但是我仍然对什么时候应该使用Flow以及什么时候应该使用Channel.

一开始看起来更简单。使用热数据流?Channel. 冷的?Flows. 如果您需要从多个地方收听数据流,情况也是如此;如果是这样的话Channel是选择去。还有很多例子和问题。

但是最近FlowChannels引入了大量方法和类,这些方法和类鼓励使用Flow,转换Channels成哪些设施Flows等等。随着每个 Kotlin 版本中出现所有这些新东西,我越来越困惑。所以问题是:

什么时候应该使用Channel,什么时候应该使用Flow

channel stream kotlin kotlin-coroutines

36
推荐指数
3
解决办法
8003
查看次数

Thymeleaf,片段和默认参数

我创建了fragment.html文件.它包含以下片段:

<div th:fragment="my_fragment(content)">
    <p th:text="${content}"></p>
</div>
Run Code Online (Sandbox Code Playgroud)

我将上面的片段放入我的视图文件中:

<div th:replace="fragments :: my_fragment('test')"></div>
Run Code Online (Sandbox Code Playgroud)

现在,我想将两个参数传递给my_fragment,但我必须确保向后兼容性.

我试着按如下方式解决问题:

<div th:fragment="my_fragment(content, defaultParameter='default value')">
    <p th:text="${content}"></p>
</div>
Run Code Online (Sandbox Code Playgroud)

不幸的是,上面的解决方案产生了错误:

org.springframework.web.util.NestedServletException:请求处理失败; 嵌套异常是org.thymeleaf.exceptions.TemplateProcessingException:无法解析片段.签名"my_fragment(content,defaultParameter ='default value')"声明2个参数,但片段选择指定1个参数.片段选择不正确匹配.

任何的想法?

java spring spring-mvc thymeleaf

33
推荐指数
3
解决办法
3万
查看次数

Spring Boot和自定义404错误页面

在我的Spring Boot应用程序中,我正在尝试配置自定义错误页面,例如对于404,我在应用程序配置中添加了以下Bean:

@Bean
public EmbeddedServletContainerCustomizer containerCustomizer() {
    return new EmbeddedServletContainerCustomizer() {
        @Override
        public void customize(ConfigurableEmbeddedServletContainer container) {
            container.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/404.html"));
        }
    };
}
Run Code Online (Sandbox Code Playgroud)

另外,我创建了以下简单的Thymeleaf模板:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
    <head>
        <title>404 Not Found</title>
        <meta charset="utf-8" />
    </head>
    <body>
        <h3>404 Not Found</h3>
        <h1 th:text="${errorCode}">404</h1>
        <p th:utext="${errorMessage}">Error java.lang.NullPointerException</p>
        <a href="/" th:href="@{/}">Back to Home Page</a>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

并将其添加到/resources/templates/文件夹中.就在404错误我只能看到白屏.

我做错了什么以及如何正确设置我的404页面?此外,是否可以使用模板而不仅仅是自定义错误页面的静态页面?

java spring spring-mvc thymeleaf spring-boot

29
推荐指数
6
解决办法
7万
查看次数

Android FAB按钮图标始终为黑色,带有MaterialComponents主题

我正在创建一个Android应用程序,我正在使用AndroidX库和Material设计主题.我的应用主题styles.xml是:

<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>
Run Code Online (Sandbox Code Playgroud)

我从库中有以下FAB按钮:

<com.leinardi.android.speeddial.SpeedDialView
        android:id="@+id/work_log_fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        app:layout_behavior="@string/speeddial_scrolling_view_snackbar_behavior"
        app:sdMainFabClosedSrc="@drawable/ic_add_white_24dp"
        app:sdOverlayLayout="@id/overlay" />
Run Code Online (Sandbox Code Playgroud)

并尝试了默认的FAB:

<com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end|bottom"
        android:src="@drawable/ic_add_white_24dp"
        android:layout_margin="16dp" />
Run Code Online (Sandbox Code Playgroud)

没有图标的颜色(矢量可绘制),FAB内的图标(来自库和默认图标)总是黑色.我已经将问题缩小到材料设计主题,因为使用旧的Theme.AppCompat.Light.DarkActionBar而不是新Theme.MaterialComponents.Light.DarkActionBar的FAB内的图标获得原始矢量drawable的颜色.

有谁知道为什么会发生这种情况以及如何解决这个问题?

android android-theme material-design floating-action-button androidx

29
推荐指数
5
解决办法
4323
查看次数

Thymeleaf不显示Spring表单错误消息

我正在将Spring jsp应用程序迁移到Thymeleaf但是在显示表单错误时遇到问题.

我正在使用SpringTemplateEngine和ThymeleafViewResolver并且模板的渲染工作.表单值也在表单输入字段中填充.

到目前为止唯一不起作用的是显示表单错误消息.

我的控制器看起来像:

@RequestMapping(method = RequestMethod.POST)
String save(@Valid CustomerForm form, BindingResult bindingResult, Model model, RedirectAttributes redirectAttributes) {
    if (bindingResult.hasErrors()) {
        model.addAttribute("form", form)
        return "app/customers/create"
    }
    ....
Run Code Online (Sandbox Code Playgroud)

我打印了bindingResult以验证它是否包含错误:

binding result = org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'customerForm' on field 'name': rejected value []; codes [customerForm.name.NotBlank,name.NotBlank,java.lang.String.NotBlank,NotBlank]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [customerForm.name,name]; arguments []; default message [name]]; default message [may not be empty]
Run Code Online (Sandbox Code Playgroud)

当我尝试使用以下方法显示错误:

<ul>
    <li th:each="e : ${#fields.detailedErrors()}" th:class="${e.global}? globalerr : fielderr">
        <span th:text="${e.global}? '*' : ${e.fieldName}">The field name</span> …
Run Code Online (Sandbox Code Playgroud)

spring thymeleaf

27
推荐指数
2
解决办法
4万
查看次数

在ThymeLeaf中循环遍历数组

我是ThymeLeaf的新手,我想知道是否有办法循环<p>html标签以及遍历该<p>标签内的数组.我希望其中的元素smokeTest最终出现在不同的段落中.

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Getting Started: Serving Web Content</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
<p th:text="${smokeTests[0].name}"/>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

谢谢你的帮助

arrays loops paragraph thymeleaf

27
推荐指数
1
解决办法
4万
查看次数