小编Ala*_*ruz的帖子

Swift - 多个交换机案例

我正在制作一个你必须加入两个物体的棋盘游戏.每个对象都有一个类型,有5种不同的类型.对于合并中的每种不同类型组合,将对游戏产生不同的影响.现在,我正在为每个组合使用switch语句.所以,我的代码看起来像这样.

struct Coin {
    var type = 1
}

// Each coin is of type Coin.   
switch(coin1.type, coin2.type) {
    case (1,1):
        functionNormal()
    case (1,2):
        functionUncommon()
    case (1,3):
        functionRare()
    ...
}
Run Code Online (Sandbox Code Playgroud)

对象的位置不会改变结果.(1,2)合并将具有与(2,1)合并相同的效果.是否有一种不那么冗长的方式来实现这一目标?

switch-statement swift

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

Thymeleaf 使用 th:each 动态创建表单

我想知道如何创建th:object用于th:each. 例如,我有以下代码。

超文本标记语言

<th:block th:each="store: ${stores}">
    <form th:object="${store}" th:action="@{/modify-store}">
        <input th:field="*{idStorePk}"/>
        <input th:field="*{name}"/>
        <input th:field="*{phoneNumber}"/>
        <button type="submit">Modify</button>
    </form>
</th:block>
Run Code Online (Sandbox Code Playgroud)

控制器

@RequestMapping(value = "/stores")
public String getIndex(Model model) {
    model.addAttribute("stores", storeService.getAllStores());
    return "store";
}
Run Code Online (Sandbox Code Playgroud)

因此,我想为每个对象添加一个表单,但似乎不可能,并且出现以下错误。

java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'store' available as request attribute
Run Code Online (Sandbox Code Playgroud)

所以,我决定在我的控制器中添加一个@ModelAttribute,但无法返回实际的商店。

@ModelAttribute("store")
public Store getStore(Store store) {
    return store;
}
Run Code Online (Sandbox Code Playgroud)

通过这种方法,我的所有表单都有空值。我也尝试添加 a @PathVariable,但看不到使用 绑定它th:object。有解决办法吗?

java spring thymeleaf

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

Spring的静态资产缓存

我正在开发一个Web应用程序,并打算利用缓存资源所带来的性能提升,但是它带有一个重要的警告。每当我更新静态文件时,用户都不会立即看到这些更改,因此必须禁用浏览器的缓存才能获取最新版本。为了解决此问题,我决定添加静态资产版本控制。可以与以下代码配合使用。

@Override
public void addResourceHandlers(final ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/**")
            .addResourceLocations("classpath:/static/")
            .setCacheControl(CacheControl.maxAge(365, TimeUnit.DAYS))
            .resourceChain(true)
            .addResolver(new VersionResourceResolver().addContentVersionStrategy("/**"))
            // Costume made transformer to handle JS imports
            .addTransformer(new JsLinkResourceTransformer())
            .addTransformer(new CssLinkResourceTransformer());
}

@Bean
public ResourceUrlEncodingFilter resourceUrlEncodingFilter() {
    return new ResourceUrlEncodingFilter();
}
Run Code Online (Sandbox Code Playgroud)

除了一个简单的细节,一切都按预期进行。JS导入仍在加载无版本文件。因此,类似的内容import * from './myscrypt.js'将无法正常工作。

为了避免这种新的警告,我必须实现自己的资源转换器。该实现完成了工作,现在我的导入将获取正确的版本,例如import * from './myscript-149shdhgshs.js'。然后,我以为一切都已解决,但是又出现了一个新问题。这是场景,这将使其更易于理解。

  1. 我加载的页面包括 script.js
  2. 然后Spring为我提供文件的正确版本 script-v1.js
  3. 之后,script-v1.jsmyscript.js
  4. 浏览器获取正确版本的脚本 myscript-v1.js
  5. 他们两个被本地缓存
  6. 我更新myscript.js制作新版本myscript-v2.js
  7. 我重新加载了页面,但是由于script-v1.js存储在缓存中,所以myscript-v1.js即使有新版本,也要使用旧的import加载它。

我似乎无法使其工作。当然,我可以停止使用js modules,而是立即加载所有脚本,但这不是我想要的解决方案。是否有js module使用Spring 进行版本控制的解决方案?

javascript java spring spring-mvc spring-boot

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

Spring Security Dialect sec:授权未解析

我正在使用 spring boot 2.1.0 和 spring security 构建一个小应用程序。我可以使用表单身份验证登录和退出。但是, sec:authorize 标记不会被解析或评估,呈现的 html 包含这些属性。

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <dependency>
        <groupId>org.thymeleaf.extras</groupId>
        <artifactId>thymeleaf-extras-springsecurity4</artifactId>
        <version>3.0.4.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>nz.net.ultraq.thymeleaf</groupId>
        <artifactId>thymeleaf-layout-dialect</artifactId>
        </dependency>
Run Code Online (Sandbox Code Playgroud)

不知道我错过了什么

security spring spring-security dialect thymeleaf

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