小编dtr*_*unk的帖子

划分BigDecimals会导致ArithmeticException

可能重复:
在BigDecimal.divide期间抛出ArithmeticException

这导致ArithmeticException:http://ideone.com/RXeZw

提供比例和舍入模式将给我错误的结果.此示例应输出50.03%.如何正确地圆?

为便于参考,这是代码:

BigDecimal priceDiff = BigDecimal.ONE
     .subtract(new BigDecimal(9.99)
     .divide(new BigDecimal(19.99)))
     .multiply(new BigDecimal(100));
System.out.println(priceDiff.toPlainString());
Run Code Online (Sandbox Code Playgroud)

java math

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

在反应流上递归加载和映射

假设我们有一个商店、一个目录和类别。每个商店都可以有目录,每个类别可以有子类别。

这些文件来自 MongoDB:

@Data
@Document
@NoArgsConstructor
@AllArgsConstructor
public class Catalog {
    @Id
    private String id;
    private Set<String> stores;
    @Transient
    private List<Category> tree;
}

@Data
@Document
@NoArgsConstructor
@AllArgsConstructor
public class Category {
    @Id
    private String id;
    private List<CatalogMember> catalogs;
    private List<CategoryMember> children;
    @Transient
    private List<Category> subCategories;
}

@Data
@NoArgsConstructor
@AllArgsConstructor
public class CatalogMember {
    private String id;
    private Long sequence;
}

@Data
@NoArgsConstructor
@AllArgsConstructor
public class CategoryMember {
    private String id;
    private Long sequence;
}
Run Code Online (Sandbox Code Playgroud)

现在我有其他 API 模型可以在前端进一步使用:

@Getter
@Builder
public …
Run Code Online (Sandbox Code Playgroud)

java project-reactor spring-webflux

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

在Maven Antrun插件中设置目录

我已经将jquery-ui下载到我的webapp,它有一个build.xml用于压缩和缩小的.现在我想build.xml从我的内部运行pom.xml:

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <executions>
      <execution>
        <id>compile</id>
        <phase>compile</phase>
        <configuration>
          <target>
            <ant antfile="src/main/webapp/resources/js/jquery-ui/build/build.xml" />
          </target>
        </configuration>
        <goals>
          <goal>run</goal>
        </goals>
      </execution>
    </executions>
  </plugin>
Run Code Online (Sandbox Code Playgroud)

但现在s buildfile is wrong. It creates a dist folder on the wrong place. Here's the来自jquery-ui的thi build.xml` 的行为:https://github.com/jquery/jquery-ui/blob/master/build/build.xml

它在pom.xml所在的同一个地方(我运行的同一个地方mvn clean package)创建了dist文件夹,它应该创建它src/main/webapp/resources/js/jquery-ui/build

是否可以build.xml从指定目录(工作目录)运行?无法访问Ant任务的文档:http://ant.apache.org/manual/CoreTasks/ant.html

编辑:pom.xml

<?xml version="1.0" encoding="utf-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
                             http://maven.apache.org/xsd/maven-4.0.0.xsd">

  <modelVersion>4.0.0</modelVersion>

  <groupId>com.example.myproject</groupId>
  <artifactId>myproject</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>myproject</name>

  <dependencies>
    <!-- ... --> …
Run Code Online (Sandbox Code Playgroud)

maven maven-antrun-plugin

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

根据现有的Hibernate Validator创建自定义的Hibernate Validator

我想@LengthIfNotBlank(min=,max=)基于Hibernate @Length约束创建一个自定义约束:

@Target( { METHOD, FIELD, ANNOTATION_TYPE })
@Retention(RUNTIME)
@Constraint(validatedBy = LengthIfNotBlankValidator.class)
@Documented
public @interface LengthIfNotBlank {
    String message() default "{com.mycompany.LengthIfNotBlank}";
    Class<? extends Payload>[] payload() default {};
    int max() default Integer.MAX_VALUE;
    Class<?>[] groups() default {};
    int min() default 0;
}

public class LengthIfNotBlankValidator extends LengthValidator {
    @Override
    public boolean isValid(String value, ConstraintValidatorContext constraintValidatorContext) {
        return StringUtils.isBlank(value) || super.isValid(value, constraintValidatorContext);
    }
}
Run Code Online (Sandbox Code Playgroud)

例外:

[java] javax.validation.ValidationException: Unable to initialize com.example.constraints.LengthIfNotBlankValidator
[java] Caused by: java.lang.ClassCastException: $Proxy32 cannot be cast to …
Run Code Online (Sandbox Code Playgroud)

java hibernate hibernate-validator

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

找不到公共登录类

真奇怪.我刚刚在我的网络应用程序中更改了一些东西而无法恢复这些东西,因为我不记得我改变了什么.

我在尝试运行我的webapp时收到此异常:

Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:270)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4779)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5273)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1566)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1556)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:662)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1701)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1546)
    ... 12 more
Run Code Online (Sandbox Code Playgroud)

这是我的pom.xml文件:

<?xml version="1.0" encoding="UTF-8" ?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>example</artifactId>
    <name>example</name>
    <packaging>war</packaging>
    <version>1.0.0-BUILD-SNAPSHOT</version>

    <properties>
        <java.version>1.6</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <spring.version>3.1.1.RELEASE</spring.version>
        <slf4j.version>1.6.4</slf4j.version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib</artifactId>
            <version>2.2.2</version>
        </dependency>

        <!-- Spring …
Run Code Online (Sandbox Code Playgroud)

java maven

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

来自远程标签的Git分支

我已经创建了一个从另一个远程存储库镜像的新的本地git存储库:

git init
git remote add original {url}
git pull original master
git remote add origin {url}
git push -u origin master
Run Code Online (Sandbox Code Playgroud)

这将创建一个originals master分支的镜像.现在我想创建一个标签的新分支original.

命令应该如何?我试过git checkout -b newbranch original/tagname但是我得到了:

fatal: Cannot update paths and switch to branch 'newbranch' at the same time.
Did you intend to checkout 'original/tagname' which can not be resolved as commit?
Run Code Online (Sandbox Code Playgroud)

git git-tag git-branch

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

使用Java ArrayList评估包含字符串的对象包含()

我想对Objects进行更深入的String检查,以便能够执行以下操作:

List<MyObj> myList = new ArrayList<MyObj>() {{
    add(new MyObj("hello"));
    add(new MyObj("world"));
}};

System.out.println(myList.contains("hello")); // true
System.out.println(myList.contains("foo")); // false
System.out.println(myList.contains("world")); // true
Run Code Online (Sandbox Code Playgroud)

但是使用以下完整代码在每个上面都会出错

/* Name of the class has to be "Main" only if the class is public. */
class Ideone {
    public static class MyObj {
        private String str;
        private int hashCode;

        public MyObj(String str) {
            this.str = str;
        }

        public @Override boolean equals(Object obj) {
            System.out.println("MyObj.equals(Object)");
            if (obj == this) {
                return true;
            }

            if (obj instanceof String) { …
Run Code Online (Sandbox Code Playgroud)

java arraylist

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

使用自定义验证程序在地图上进行JSR-303验证

我正在使用Spring和Thymeleaf填写表格:

<form method="post" th:action="@{/postForm}" th:object="${myForm}"><!--/* model.addAttribute("myForm", new MyForm()) */-->
    <input type="text" th:each="id : ${idList}" th:field="*{map['__${id}__']}" /><!--/* results in map['P12345'] */-->
</form>
Run Code Online (Sandbox Code Playgroud)

MyForm看起来像这样:

public class MyForm {
    @Quantity
    private Map<String, String> map = new HashMap<String, String>();

    public Map<String, String> getMap() {
        return map;
    }

    public void setMap(Map<String, String> map) {
        this.map = map;
    }
}
Run Code Online (Sandbox Code Playgroud)

如您所见,我做了一个自定义注释@Quantity,该注释应该检查输入值是否可以解析为BigDecimal

@Target({METHOD, FIELD, ANNOTATION_TYPE})
@Retention(RUNTIME)
@Constraint(validatedBy = QuantityValidator.class)
@Documented
public @interface Quantity {
    String message() default "{com.example.form.validation.constraints.Quantity}";
    Class<? extends Payload>[] payload() default …
Run Code Online (Sandbox Code Playgroud)

java spring bean-validation thymeleaf

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

如何设置Controller范围的全局变量

目前我@ModelAttribute用来设置全局变量(例如页脚的推文)@Controller:

public @Controller class MainController {
    public @ModelAttribute void global(ModelMap map) {
        map.addAttribute("tweets", /*...*/null);
    }
}
Run Code Online (Sandbox Code Playgroud)

但是在创建另一个控制器以保持干净和分离时,它们在逻辑上已经消失了:

public @Controller class GalleryController {
    // ...
}
Run Code Online (Sandbox Code Playgroud)

设置全局变量控制器范围的最佳做法是什么?

java spring-mvc

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

Thymeleaf if + 每个订单

我正在做以下事情:

<p th:if="${foo != null}" th:each="row : ${foo.values().iterator().next().rowKeySet()}">
Run Code Online (Sandbox Code Playgroud)

foo是 的一个实例java.util.Map

Thymeleaf throws an `TemplateProcessingException: 
Exception evaluating SpringEL expression: "foo.values().iterator().next().value.rowKeySet()"` with root cause `SpelEvaluationException: EL1011E:(pos 22): 
Method call: Attempted to call method values() on null context object`.
Run Code Online (Sandbox Code Playgroud)

为什么 Thymeleafth:each在结果为th:ifis时进行处理false以及如何解决?

这是我目前肮脏的解决方法:

<p th:if="${foo != null}" th:each="row : ${foo != null ? foo.values().iterator().next().rowKeySet() : null}">
Run Code Online (Sandbox Code Playgroud)

java spring thymeleaf

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

Thymeleaf/Spring:如何检查消息属性是否存在

是否可以检查消息属性是否存在?

示例:我想在仅包含消息属性的循环中删除可折叠的 div-Element。

<div th:each="payment : ${paymentList}">
    <input type="radio" data-toggle="radio-collapse" data-target="#collapse1" [...] /> [...]
    <div id="collapse1" th:utext="#{|payment.${payment.id}.additionalInfo|}" [...]>
        Hello, world!
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)

如果没有支付的附加信息,则不需要该元素。比方说我们的ID DIRECT_DEBITPAYPALSAFERPAY和下列消息属性:

payment.DIRECT_DEBIT=Direct debit
payment.DIRECT_DEBIT.additionalInfo=Direct debit info text...
payment.PAYPAL=PayPal
payment.PAYPAL.additionalInfo=PayPal info text...
payment.SAFERPAY=Saferpay
Run Code Online (Sandbox Code Playgroud)

正如您所看到的, 没有 addInfo 消息属性SAFERPAY

spring thymeleaf

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

在特定时间跨度内每 5 分钟运行一次 cron

我想在这些时间跨度之间每 5 分钟运行一次 cron 作业:

  • 周二:晚上 8 点至晚上 10 点
  • 周三:晚上 8 点至晚上 10 点
  • 周五:晚上 8.30 至晚上 10.30
  • 周六:下午 3.30 - 下午 5.30 和下午 6.30 - 晚上 8.30
  • 周日:下午 3.30 至晚上 7.30

我想我需要不止一行才能实现它。

我当前的 crontab 看起来像这样:

COMMAND=curl -u user:password http://localhost/webapp/cache/build/openligadb >> /var/log/cron.log
# Tuesday + Wednesday 8pm - 10pm
# m     h           dom mon dow command
*/5     20-22       *   *   2,3 $COMMAND
# Friday 8.30pm - 10.30pm
# m     h           dom mon dow command
*/5+30  20-22       *   *   5 …
Run Code Online (Sandbox Code Playgroud)

cron

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