SimpleDateFormat的javadoc声明SimpleDateFormat未同步.
"日期格式不同步.建议为每个线程创建单独的格式实例.如果多个线程同时访问格式,则必须在外部同步."
但是在多线程环境中使用SimpleDateFormat实例的最佳方法是什么.以下是我想到的一些选项,我过去使用过选项1和2,但我很想知道是否有更好的替代方案或哪些选项可以提供最佳性能和并发性.
选项1:在需要时创建本地实例
public String formatDate(Date d) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(d);
}
Run Code Online (Sandbox Code Playgroud)
选项2:将SimpleDateFormat的实例创建为类变量,但同步对其的访问.
private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
public String formatDate(Date d) {
synchronized(sdf) {
return sdf.format(d);
}
}
Run Code Online (Sandbox Code Playgroud)
选项3:创建ThreadLocal以为每个线程存储SimpleDateFormat的不同实例.
private ThreadLocal<SimpleDateFormat> tl = new ThreadLocal<SimpleDateFormat>();
public String formatDate(Date d) {
SimpleDateFormat sdf = tl.get();
if(sdf == null) {
sdf = new SimpleDateFormat("yyyy-MM-hh");
tl.set(sdf);
}
return sdf.format(d);
}
Run Code Online (Sandbox Code Playgroud) 当dom元素插入dom时,是否有事件jquery触发?
例如,假设我通过ajax加载一些内容并将其添加到DOM中,并且在其他一些javascript中我可以使用.live()添加事件,以便当匹配特定选择器的事件添加到DOM时,事件将触发说元素.与$(document).ready()类似,但是在动态添加到DOM的内容上.
我梦想的是:
$('.myClass').live('ready', function() {
alert('.myClass added to dom');
});
Run Code Online (Sandbox Code Playgroud)
如果jquery不支持这样的东西,那么还有另一种方法可以实现这个功能而无需修改实际执行初始dom操作的代码吗?
可能重复:
属性属性的Html验证错误
根据facebook使用他们的喜欢按钮和开放图形元标记你需要将这样的东西放到你的html页面.
<meta property="og:title" content="The Rock"/>
<meta property="og:type" content="movie"/>
<meta property="og:url" content="http://www.imdb.com/title/tt0117500/"/>
<meta property="og:image" content="http://ia.media-imdb.com/rock.jpg"/>
<meta property="og:site_name" content="IMDb"/>
Run Code Online (Sandbox Code Playgroud)
他们到底在玩什么?这是无效的html,属性'属性'不是,并且从来不是元标记的属性所以为什么地球上有facebook使用这个而不是<meta name ="og:title"content ="The Rock"/>?
我不愿意在我的网站上使用有效的"名称"而不是"财产"来尝试,因为如果我尝试并且它在我的网站上不起作用,那么任何在我测试时点击的人都会失败.那么......有没有人知道我是否使用'名称'而不是'属性'这仍然有用吗?
我目前正在学习Struts 2,目前我正在构建一个简单的应用程序,其中未经验证的用户被重定向到登录表单.
我有一个登录表单和操作功能,它获取用户凭据,验证它们并在会话中存储User对象但是我现在试图阻止在登录之前访问页面并且我试图用拦截器执行此操作.
我的问题是我编写了一个拦截器,用于检查User对象是否已保存在会话中,但如果没有,我想重定向到登录页面,并且无法绕过struts并使用HttpServletResponse.sendRedirect方法
组态:
<package name="mypackage" extends="struts-default" namespace="/admin">
<interceptors>
<interceptor name="login" class="my.LoginInterceptor" />
</interceptors>
<default-interceptor-ref name="login"/>
<action name="login" class="my.LoginAction">
<result name="input">/admin/login.jsp</result>
<result name="success" type="redirect">/admin</result>
</action>
<action name="private" class="my.PrivateAction">
<result>/admin/private.jsp</result>
</action>
</package>
Run Code Online (Sandbox Code Playgroud)
拦截器代码:
@Override
public String intercept(ActionInvocation inv) throws Exception {
Map<String, Object> session = inv.getInvocationContext().getSession();
Object user = session.get("user");
if(user == null) {
// redirect to the 'login' action here
}
else {
return inv.invoke();
}
}
Run Code Online (Sandbox Code Playgroud) 我想使用Hibernate将数据库表中的字段增加1.
我可以通过简单地加载一个对象,增加值和更新但对象如果另一个线程来临时,在负载和更新则该值将损坏之间更新的领域做到这一点.
所以我直接在数据库上调用了更新:
Query updateHits = createQuery(
"UPDATE Job SET hitCount = hitCount + 1 WHERE id = :jobId" );
updateHits.setInteger( "jobId", job.getId() );
updateHits.executeUpdate();
Run Code Online (Sandbox Code Playgroud)
但是我的理解是,这绕过了数据库中的乐观锁定,我目前遇到了SQL服务器死锁的问题,我相信这是罪魁祸首.
有没有办法在不调用更新的情况下增加字段并保持数据完整性?
所以我试图使用Hibernate Tools来反向工程我的数据库,我只是开始使用Freemarker模板来弱化它生成的代码.问题是我想更改它生成的DAO类的名称.默认情况下,DAO类的形式PersonHome中名为然而,名称更改为的PersonDAO我修改了DAO/daohome.ftl.
虽然这确实将生成的类名更改为PersonDAO,但java文件仍称为PersonHome.java.
有没有我可以更改生成的文件名以匹配源代码的地方?
java hibernate freemarker reverse-engineering hibernate-tools
我想在freemarker模板中使用一些自定义标签,这很容易,因为我可以在我的web.xml文件中包含JspSupportServlet并在模板中包含以下行.
<#assign my=JspTaglibs["/WEB-INF/mytaglib.tld"] />
Run Code Online (Sandbox Code Playgroud)
但是,如果.tld捆绑在META-INF目录中的JAR文件中,我该怎么做呢?我试过这两个都没有运气.
<#assign my=JspTaglibs["/META-INF/mytaglib.tld"] />
<#assign my=JspTaglibs["/mynamespace"] />
Run Code Online (Sandbox Code Playgroud) 我想将程序集ID用作组件xml文件中的属性,而不是在多个程序集中复制一部分。查看maven-assembly-plugin文档,我看不到任何对程序集ID的引用。有什么想法吗?
这是我想添加到组件xml文件中的示例。
<files>
<file>
<source>src/main/config/${assembly.id}.properties</source>
<destName>/conf/config.properties</destName>
</file>
</files>
Run Code Online (Sandbox Code Playgroud) 我正在尝试在服务器上设置一些.NET托管服务器,以吸引某人,但是我对.NET不太熟悉,并且当该网站上载时,正在发生的错误不会在其当前托管服务器上发生。该站点已编译,因此其大部分只是DLL文件和aspx文件,错误消息如下。有什么想法需要做什么才能使它起作用?
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: BC30456: 'InitializeCulture' is not a member of 'ASP.index_aspx'.
Source Error:
Line 1: <%@ page language="VB" masterpagefile="~/mst_mathsBuddy.master" autoeventwireup="false" inherits="index, App_Web_nl--pcdh" %>
Line 2:
Line 3: <%@ Register Src="Controls/Competition.ascx" TagName="Competition" TagPrefix="uc3" %>
Run Code Online (Sandbox Code Playgroud)
编辑:
该应用程序在IIS6和ASP.NET 2.0.5727版本上运行,该版本与currnet托管相同。
我正在构建一个使用磁贴的Struts2 Web应用程序但是我发现了一个非常令人沮丧的问题,即如果apache.org关闭(这似乎经常发生)Web应用程序无法启动.这是因为在其标准设置中,StrutsTilesListener尝试加载tiles defenitions文件,该文件包含一个带有public-id的DOCTYPE,该公共id指向位于tiles.apache.org上的DTD.
当应用程序启动时,使用Apache Xerces通过Apache Commons Digester加载定义文件,该文件尝试从tiles.apache.org加载DTD但是如果apache.org关闭则会失败并且随之而来的是整个Web应用程序无法启动.
我可以通过下载文件并将其置于本地并在struts定义文件中指定新的本地位置来绕过远程位置的下载,但是这个解决方案不是很便携,因为本地保存DTD的位置可能会有所不同开发人员机器和不同的一旦上传到实时环境,所以我将不得不继续编辑位置,以便运行webapp运行的机器,这简直是烦人的.
项目中没有其他xml文件存在此问题,包括struts.xml文件,该文件在apache.org上也有DTD位置,所以很明显存在一个设置问题,其中Tiles严格要求DTD但其他组件不是.这有什么解决方案吗?我已经没有耐心了,我不能把这个webapp知道,如果我重新启动apache.org,那么webapp就不会再回来了.
Struts tile defenition文件
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE tiles-definitions PUBLIC
"-//Apache Software Foundation//DTD Tiles Configuration 2.1//EN"
"http://tiles.apache.org/dtds/tiles-config_2_1.dtd">
<tiles-definitions>
<definition name="master" template="/tiles/templates/master.jsp">
</definition>
<definition name="public" extends="master">
<put-attribute name="header" value="/tiles/templates/public/header.jsp" />
<put-attribute name="footer" value="/tiles/templates/public/footer.jsp" />
<put-attribute name="templateMeta" value="/tiles/templates/public/meta.jsp" />
</definition>
</tiles-definitions>
Run Code Online (Sandbox Code Playgroud)
当apache.org关闭时,堆栈跟踪
SEVERE: Exception sending context initialized event to listener instance of class org.apache.struts2.tiles.StrutsTilesListener
java.lang.IllegalStateException: Unable to instantiate container.
at org.apache.tiles.web.startup.TilesListener.contextInitialized(TilesListener.java:60)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3972)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4467)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:785)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443) …Run Code Online (Sandbox Code Playgroud) 在应用程序重新部署时在struts2 webapp中使用Hibernate,在重新部署后尝试访问页面时会出现此错误.
java.lang.IllegalStateException: Timer already cancelled.
Run Code Online (Sandbox Code Playgroud)
在重新部署tomcat之后,以下输出指示问题的原因是当webapp的第一个实例关闭时,Hibernate没有正常关闭.
Oct 15, 2010 8:58:34 PM org.apache.catalina.loader.WebappClassLoader clearReferencesStopTimerThread
SEVERE: A web application appears to have started a TimerThread named [Timer-0] via the java.util.Timer API but has failed to stop it. To prevent a memory leak, the timer (and hence the associated thread) has been forcibly cancelled.
Run Code Online (Sandbox Code Playgroud)
我已经在contextDestroyed方法中添加了一个ContextListener,但是这没有任何效果.
@Override
public void contextDestroyed(ServletContextEvent arg0) {
HibernateUtil.getSessionFactory().close();
}
Run Code Online (Sandbox Code Playgroud)
我还能做些什么来防止这个错误吗?
我想测试浏览器是否支持 transitionend 事件,我可以通过以下方式轻松实现:
var isSupported = 'ontransitionend' in window;
Run Code Online (Sandbox Code Playgroud)
但是,这在 Firefox 中不起作用,并且是一个众所周知的问题。我发现以下博客文章在这里描述了一个解决方案:Detecting event support without browser sniffing但是我也无法得到这个解决方案来成功测试 transitionend 。
问题是,虽然这适用于像“onclick”这样的大多数事件,但这似乎不适用于 transitionend 事件,我找不到有效的解决方案。我创建了一个小提琴来展示这一点,它首先测试 onclick 事件以演示该技术的工作原理,然后对 transitionend 使用相同的技术。
尽管浏览器支持可用,但 Onclick 返回 true 但 ontransitionend 返回 false。
我试图在更大的文档中映射以下XML结构,显然这不是XML的最大用途,但这是我必须使用的.
为清晰起见简化示例:
<details>
<pictures>
<picture1>
http://domain.com/path/picture1.jpg
</picture1>
<picture2>
http://domain.com/path/picture2.jpg
</picture2>
<picture3>
http://domain.com/path/picture3.jpg
</picture3>
<picture4>
http://domain.com/path/picture4.jpg
</picture4>
<picture5>
http://domain.com/path/picture5.jpg
</picture5>
<picture6>
http://domain.com/path/picture6.jpg
</picture6>
<picture7>
http://domain.com/path/picture7.jpg
</picture7>
</pictures>
</details>
Run Code Online (Sandbox Code Playgroud)
该文档有一个DTD,声明最多可以有30个不同的图片元素,编号为1-30 <picutre[n]/>
我想做的是,而不是为这些元素创建30个不同的类,分别称为Picture1,Picture2,Picture3 ......等等.我只想使用一个Picture类并将其用于所有30个唯一元素名称.
以下是我到目前为止所尝试的内容.
@XmlRootElement
public class Details {
...
@XmlElementWrapper
@XmlElementRefs({
@XmlElementRef( name="picture1", type=Picture.class ),
@XmlElementRef( name="picture2", type=Picture.class ),
@XmlElementRef( name="picture3", type=Picture.class ),
@XmlElementRef( name="picture4", type=Picture.class ),
@XmlElementRef( name="picture5", type=Picture.class ),
@XmlElementRef( name="picture6", type=Picture.class ),
@XmlElementRef( name="picture7", type=Picture.class ),
@XmlElementRef( name="picture8", type=Picture.class ),
@XmlElementRef( name="picture9", type=Picture.class ),
@XmlElementRef( name="picture10", type=Picture.class ), …Run Code Online (Sandbox Code Playgroud) java ×7
hibernate ×3
freemarker ×2
struts2 ×2
asp.net ×1
concurrency ×1
css ×1
dtd ×1
facebook ×1
hql ×1
html ×1
interceptor ×1
javascript ×1
jaxb ×1
jquery ×1
jsp ×1
login ×1
maven ×1
maven-2 ×1
maven-3 ×1
opengraph ×1
sql-server ×1
tomcat ×1
xerces ×1
xhtml ×1