小编Max*_*kov的帖子

Java 8 Time API:如何将格式为"MM.yyyy"的字符串解析为LocalDate

我对Java 8 Time API中的日期解析感到有点气馁.

以前我可以轻松写:

String date = "04.2013";
DateFormat df = new SimpleDateFormat("MM.yyyy");
Date d = df.parse(date);
Run Code Online (Sandbox Code Playgroud)

但是现在如果我使用LocalDate并像这样做:

String date = "04.2013";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM.yyyy");
LocalDate ld = LocalDate.parse(date, formatter);
Run Code Online (Sandbox Code Playgroud)

我收到一个例外:

java.time.format.DateTimeParseException: Text '04' could not be parsed at index 0
java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1948)
java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1850)
java.time.LocalDate.parse(LocalDate.java:400)
java.time.LocalDate.parse(LocalDate.java:385)
com.luxoft.ath.controllers.JsonController.region(JsonController.java:38)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:483)
org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)
javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
Run Code Online (Sandbox Code Playgroud)

如果我将字符串格式更改为"yyyy-MM-dd",即使没有格式化程序,一切也能正常工作:

String date = "2013-04-12";
LocalDate …
Run Code Online (Sandbox Code Playgroud)

java date java-8 java-time

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

Thymeleaf:如何获取URL属性值

我找不到使用Thymeleaf从URL获取属性的任何解决方案.例如,对于URL:

somesite.com/login?error=true
Run Code Online (Sandbox Code Playgroud)

我需要获得"错误"属性值.我也在使用SpringMVC,如果它可能有用.

java spring templates thymeleaf

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

如何在鼠标悬停的JetBrains IDE(IntelliJ IDEA,PyCharm,WebStorm)中禁用自动显示提示

如何在鼠标悬停时禁用IntelliJ IDEA中的自动显示提示?

在此输入图像描述

编辑:

如果使用不正确的参数调用现有方法,则此提示可能非常大.这很不舒服.

PyCharm中有相同的行为:

在此输入图像描述

WebStorm:

在此输入图像描述

intellij-idea pycharm webstorm

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

使用logback记录spring JdbcTemplate的sql参数

我正在使用带有Spring JdbcTemplate的Logback来记录我的SQL查询.我的配置包含下一行:

<logger name="org.springframework.jdbc.core.JdbcTemplate" level="DEBUG" />
Run Code Online (Sandbox Code Playgroud)

但是这仅记录带有通配符的查询而?没有参数列表.

在这里,我发现了一些如何使用log4j实现参数记录的答案.但我不想切换到log4j.

那么如何通过Logback接收JdbcTemplate的参数列表呢?

编辑

实际上,NamedParameterJdbcTemplate如果重要的话,我正在使用它.

spring logback jdbctemplate

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

Thymeleaf的Spring MVC.更新静态数据

我正在使用Spring MVC与Thymeleaf和Tomcat,我希望能够更新静态数据(html页面)而无需重新部署.在我的应用程序中,html是由Spring控制器映射的.即使是JRebel也无济于事.它更新了java类,但对视图没有任何作用.我该怎么做才能解决这个问题?也许对于html我需要一些像Jasper for JSP的监听器机制,或者我应该为Spring控制器禁用一些缓存?

tomcat spring-mvc thymeleaf

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

com.google.gdata.util.ResourceNotFoundException

我有工作代码直到昨天和今天我看到异常,com.google.gdata.util.ResourceNotFoundException 同时从我的gae代码阅读谷歌文档如下.请注意,最后一行是抛出此异常.

DocsService client = new DocsService("sakshum-YourAppName-v1");
            client.setOAuthCredentials(oauthParameters,
                    new OAuthHmacSha1Signer());
            URL feedUrl = new URL(
                    "https://docs.google.com/feeds/default/private/full/");
            DocumentQuery dquery = new DocumentQuery(feedUrl);
            dquery.setTitleQuery(title);
            dquery.setTitleExact(true);
            dquery.setMaxResults(10);
            DocumentListFeed resultFeed = client.getFeed(dquery,
                    DocumentListFeed.class);
            log.info("feed size:" + resultFeed.getEntries().size());
            if (resultFeed.getEntries().size() != 1) {
                log.info("More than one document is found with same title.");
                return null;
            }
            emailBody = new StringBuffer("");
            for (DocumentListEntry entry : resultFeed.getEntries()) {
                String docId = entry.getDocId();
                String docType = entry.getType();
                log.info("Getting doc from url with docId:" + docId
                        + " docType:" …
Run Code Online (Sandbox Code Playgroud)

google-app-engine gdata-api

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

SpringMVC控制器:如果发生表单验证错误,如何保持在页面上

我的SpringMVC控制器中有下一个工作代码:

@RequestMapping(value = "/register", method = RequestMethod.GET)
public void registerForm(Model model) {
    model.addAttribute("registerInfo", new UserRegistrationForm());
}

@RequestMapping(value = "/reg", method = RequestMethod.POST)
public String create(
        @Valid @ModelAttribute("registerInfo") UserRegistrationForm userRegistrationForm,
        BindingResult result) {

    if (result.hasErrors()) {
        return "register";
    }
    userService.addUser(userRegistrationForm);
    return "redirect:/";
}
Run Code Online (Sandbox Code Playgroud)

简而言之,create尝试验证UserRegistrationForm.如果表单有错误,它会将用户留在填充表单字段的同一页面上,其中将显示错误消息.

现在我需要将相同的行为应用到另一个页面,但在这里我有一个问题:

@RequestMapping(value = "/buy/{buyId}", method = RequestMethod.GET)
public String buyGet(HttpServletRequest request, Model model, @PathVariable long buyId) {
    model.addAttribute("buyForm", new BuyForm());
    return "/buy";
}

@RequestMapping(value = "/buy/{buyId}", method = RequestMethod.POST)
public String …
Run Code Online (Sandbox Code Playgroud)

validation spring-mvc

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

Tensorflow 和 Scikitlearn log_loss 函数实现的区别

嗨,我正在尝试进入 tensorflow,但感觉有点傻。TF 中的 log_loss 与 sklearn 的不同吗?

这是我的代码中的一些行,我是如何计算的:

from sklearn.metrics import log_loss

tmp = np.array(y_test)
y_test_t = np.array([tmp, -(tmp-1)]).T[0]

tf_log_loss = tf.losses.log_loss(predictions=tf.nn.softmax(logits), labels=tf_y)

with tf.Session() as sess:

    # training

    a = sess.run(tf.nn.softmax(logits), feed_dict={tf_x: xtest, keep_prob: 1.})
    print("    sk.log_loss: ", log_loss(y_test, a,eps=1e-7 ))
    print("    tf.log_loss: ", sess.run(tf_log_loss, feed_dict={tf_x: xtest, tf_y: y_test_t, keep_prob: 1.}))
Run Code Online (Sandbox Code Playgroud)

我得到的输出

Epoch  7, Loss:     0.4875 Validation Accuracy: 0.818981
    sk.log_loss:  1.76533018874
    tf.log_loss:  0.396557
Epoch  8, Loss:     0.4850 Validation Accuracy: 0.820738
    sk.log_loss:  1.77217639627
    tf.log_loss:  0.393351
Epoch  9, Loss:     0.4835 Validation …
Run Code Online (Sandbox Code Playgroud)

machine-learning loss scikit-learn tensorflow

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

带有SpringMVC的Tomcat中带有静态内容的外部目录

我有一个应用程序,应该在某些目录中保存内容(用户头像),例如C:\ avadir.在我的应用程序中,我正在使用SpringMVC.我需要显示用户定义的头像.为此,我必须配置Tomcat以使用此外部目录.我在我的ROOT.xml中有这样的opthions ,放在%CATALINA_HOME%\ conf\Catalina\localhost中:

<Context path="/ava" docBase="c:/avadir" debug="0" reloadable="true" crossContext="true" />
Run Code Online (Sandbox Code Playgroud)

我的下一个设置servlet-context.xml:

<resources mapping="/ava/**" location="/ava/" />
Run Code Online (Sandbox Code Playgroud)

设置完这个设置后,我仍然无法通过url localhost:8080/ava/file.jpg访问放在C:\ avadir\file.jpg中文件.有没有遗漏的东西?

java tomcat spring-mvc static-content

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