小编jkr*_*z55的帖子

Thymeleaf:错误解决模板

我正在尝试使用Thymeleaf的布局/模板,但我得到以下异常.

异常处理模板"user/index":解析模板"/layouts/default.html"时出错,模板可能不存在或任何已配置的模板解析器可能无法访问

这是我的ThymeleafConfig.java

@Configuration
public class ThymeleafConfig {

    @Bean
    public ServletContextTemplateResolver templateResolver() {
        ServletContextTemplateResolver resolver = new ServletContextTemplateResolver();
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".html");
        resolver.setTemplateMode("HTML5");
        resolver.setOrder(1);
        return resolver;
    }

    @Bean
    public SpringTemplateEngine templateEngine() {
        SpringTemplateEngine engine = new SpringTemplateEngine();
        engine.setTemplateResolver(templateResolver());
        engine.addDialect(new LayoutDialect());
        engine.addDialect(new SpringSecurityDialect());
        engine.addDialect(new SpringStandardDialect());
        return engine;
    }

    @Bean
    public ThymeleafViewResolver thymeleafViewResolver() {
        ThymeleafViewResolver resolver = new ThymeleafViewResolver();
        resolver.setTemplateEngine(templateEngine());
        return resolver;
    }
}
Run Code Online (Sandbox Code Playgroud)

我有以下文件夹结构

webapp/
..WEB-INF/
....views/
......layouts/
........default.html
......user
........index.html
Run Code Online (Sandbox Code Playgroud)

这是我的default.html,这是我的主要布局.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
<head>
    <meta charset="UTF-8" />
    <meta …
Run Code Online (Sandbox Code Playgroud)

java thymeleaf

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

CQRS 和命令 Java + Spring 类似于 MediatR

在处理 .NET 项目时,我遇到了一个库 MediatR,它使 CQRS 和命令易于实现。我真的很喜欢使用命令和命令处理程序,因为我从事过太多项目,这些项目具有巨大的过程式服务类,这些服务类向许多依赖项注入方式,使单元测试变得痛苦。我正在为 Spring + Java 寻找类似于 MediatR 的东西。本质上,我想将单个依赖项注入控制器类,并让它将命令委托给适当的命令处理程序。我在下面提供了一些 MediatR 外观的片段。我更喜欢中介者的方式,因为将 CommandHandlers 注入控制器类可能会导致与注入大量依赖项的类相同的问题。

我遇到过这个库,但它似乎更像是一个已经准备好生产的副项目。https://github.com/sleroy/spring-cqrs-arch。我知道 Axon 框架,但此时我不打算进行全面的事件溯源。是否有任何图书馆已经为此提供了可能我还没有偶然发现的图书馆?我想我可以只使用 Guava EventBus。

下面是 MediatR 用法的 C# 示例。

控制器

namespace DAB.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class PersonController : ControllerBase
    {
        private readonly IMediator mediator;

        public PersonController(IMediator mediator)
        {
            this.mediator = mediator;
        }

        // GET api/values
        [HttpPut("{id}/changename")]
        public async Task<ActionResult> ChangeName([FromBody] ChangeNameCommand command)
        {
            await this.mediator.Send(command);
            return Ok();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

命令

public class ChangeNameCommand: IRequest<bool>
{
    public string FirstName { …
Run Code Online (Sandbox Code Playgroud)

java spring cqrs

6
推荐指数
2
解决办法
2912
查看次数

Spring JPA Config IllegalArgumentException:没有找到名称的持久性单元

我有一个奇怪的问题,我无法解决这个问题.我用过JPA/Hibernate是Spring之前没有persistence.xml文件,Spring处理了一切.我正在开发一个新项目,这次我决定全部使用Java Config.我的PersistenceConfig.java存在一些问题,它一直说它无法找到持久性单元.如果我注释掉该行,则设置PersistenceUnitName,然后它会抛出IllegalStateException:没有从{classpath*:META-INF/persistence.xml}解析的持久性单元.

我不明白为什么我在使用Java Config时尝试使用persistence.xml而不是在使用XML时.有解决方案吗

这是我的PersistenceConfig.java

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup;
import org.springframework.orm.jpa.JpaDialect;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaDialect;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import java.util.Properties;

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = {"com.testapp.ots.repository"})
public class PersistenceConfig {

    @Autowired
    Environment environment;

    @Bean(name = "datasource")
    public DataSource dataSource() {
        JndiDataSourceLookup dsLookup = new JndiDataSourceLookup();
        dsLookup.setResourceRef(true);
        return dsLookup.getDataSource("jdbc/postgres");
    }

    @Bean(name = "entityManagerFactory")
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
        LocalContainerEntityManagerFactoryBean entityManagerFactory = new …
Run Code Online (Sandbox Code Playgroud)

java spring hibernate jpa spring-java-config

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

react-bootstrap 添加要选择的选项 - ReactJS

我正在使用 react-bootstrap 和 ReactJS。我期待选择将选项作为参数,但没有看到填充选项的方法。这是我迄今为止的代码。我如何传入数据

render() {

    return (
        <div>
            <Button bsStyle="primary" onClick={this.open}>Add Parameter</Button>
            <Modal show={this.state.showModal} onHide={this.close}>
                <Modal.Header>
                    <Modal.Title>Add / Edit Parameter</Modal.Title>
                </Modal.Header>
                <Modal.Body>
                    <form>
                        <FormGroup controlId="parameterType">
                            <ControlLabel>Type</ControlLabel>
                            <FormControl componentClass="select" placeholder="Type">
                                <option value="select">select</option>
                                <option value="other">...</option>
                            </FormControl>
                        </FormGroup>
                    </form>
                </Modal.Body>
                <Modal.Footer>
                    <Button bsStyle="primary" onClick={this.close}>Save Changes</Button>
                </Modal.Footer>
            </Modal>
        </div>
    )
}
Run Code Online (Sandbox Code Playgroud)

javascript reactjs react-bootstrap

2
推荐指数
1
解决办法
6615
查看次数

Kotlin supply与执行器异步

我想创建一个具有在Kotlin中特定执行程序上运行的返回值的CompletableFuture。

以下代码可以正常工作。

return CompletableFuture.supplyAsync {
        val commandHandler = registry.get<TCommand, TResponse>(command::class.java)
        commandHandler.handle(command)
 }
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试通过执行程序时,它将无法编译。

return CompletableFuture.supplyAsync({
        val commandHandler = registry.get<TCommand, TResponse>(command::class.java)
        commandHandler.handle(command)
}, exec)
Run Code Online (Sandbox Code Playgroud)

我试图变得更聪明,并编写了Java版本,并把Intellij隐瞒了给Kotlin,但是那个也有同样的错误。我在这里做错了什么?

在此处输入图片说明

编辑:

我可以通过执行以下操作使其工作,但这似乎不必要。有人可以解释为什么这样做有效,而其他方法却无效。还有其他编写此代码的方法吗?

return CompletableFuture.supplyAsync(Supplier {
    commandHandler.handle(command)
}, exec) 
Run Code Online (Sandbox Code Playgroud)

kotlin completable-future

2
推荐指数
1
解决办法
487
查看次数

带有非流应用程序/ json的Spring WebFlux Flux行为

我正在评估使用Spring Webflux,但我们必须支持期望使用application / json而不是application / stream + json的客户端。我不清楚在需要application / json的客户端中,Spring WebFlux如何处理序列化Flux。

如果将Flux序列化为application / json而不是application / stream + json,这是否是阻塞操作?

下面,我整理了一个示例控制器来演示我所看到的。当流是无限的并且产生application / json时,什么都不会返回到浏览器。这似乎很合理,因为它可能正在等待流终止。当流是无限的并产生application / stream + json时,我会按预期在浏览器中连续看到JSON对象。当Flux是有限的(例如100个元素)并且类型为application / json时,它将一次按预期呈现。问题是,它是否必须等待Flux终止才能进行序列化,是否会导致阻塞操作。返回正常的application / json时,使用Flux对性能和可伸缩性有何影响?

@RestController
public class ReactiveController {

    /* Note: In the browser this sits forever and never renders */
    @GetMapping(path = "/nonStreaming", produces = MediaType.APPLICATION_JSON_VALUE)
    public Flux<Person> getPeopleNonStreaming() {
        return Flux.interval(Duration.ofMillis(100))
                .map(tick -> new Person("Dude", "Dude", tick));
    }

    /* Note: This renders in the browser in chunks forever */
    @GetMapping(path = "/streaming", …
Run Code Online (Sandbox Code Playgroud)

spring reactor spring-webflux

2
推荐指数
1
解决办法
750
查看次数