小编dan*_*nik的帖子

Jquery发布到Servlet

我在客户端有以下代码:

      <script src="http://code.jquery.com/jquery-1.5.js"></script>
   <script>
    $(document).ready(function() {
   $("a").click(function() {
   //var orderId =  $("#orderId").val();
   $.post("test", { orderId : "John"},
   function(data) {
     alert("Data Loaded: " + data);
   });
   });
 });
    </script>
Run Code Online (Sandbox Code Playgroud)

服务器端:

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        PrintWriter writer =  response.getWriter();
        try{
           String orderId = request.getAttribute("orderId").toString();
           writer.write(orderId);
           writer.close();
           }
       catch(Exception ex)
      {
      ex.getStackTrace();
      }
    }
Run Code Online (Sandbox Code Playgroud)

我的

request.getAttribute("orderId")
Run Code Online (Sandbox Code Playgroud)

是null并且我得到null引用exeption.我究竟做错了什么?

java ajax post json servlets

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

EhCache + hibernate

我有以下问题:我有一个查询,它返回35我的结果,我想保持在二级缓存:

public List<Product> getAllProducts() {
        Session session = this.sessionfactory.getCurrentSession();
        String queryString = "from com.ewave.upromotions.objects.Product product where product.active=:active";
        Query query = session.createQuery(queryString); 
        query.setBoolean("active", true);
        query.setCacheable(true);
        query.setCacheRegion("productCache");
        List<Product> products =query.list();
        return products;
    }
Run Code Online (Sandbox Code Playgroud)

我的目标如下:

@Entity
@Table(name="products",schema="test11")
@Cacheable
@Cache( usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Product implements Serializable {

//all setters and getters ommited:

}
Run Code Online (Sandbox Code Playgroud)

我的ehcache.xml文件位于/ src /目录中:

<ehcache>  
    <diskStore path="java.io.tmpdir"/>  
    <defaultCache maxElementsInMemory="10000"  
        eternal="false"  
        timeToIdleSeconds="120"  
        timeToLiveSeconds="120"  
        overflowToDisk="false"/>  
    <cache name="hibernate.test.org.hibernate.cache.UpdateTimestampsCache"   
        maxElementsInMemory="10000"  
        eternal="false"  
        timeToIdleSeconds="120"  
        timeToLiveSeconds="120"  
        overflowToDisk="true"/>  
    <cache name="hibernate.test.org.hibernate.cache.StandardQueryCache"   
        maxElementsInMemory="10000"  
        eternal="false"  
        timeToIdleSeconds="120"  
        timeToLiveSeconds="120"  
        overflowToDisk="true"/>
     <cache name="com.vanilla.objects.Product"
        maxElementsInMemory="300" …
Run Code Online (Sandbox Code Playgroud)

java spring hibernate ehcache second-level-cache

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

乐观锁定和org.hibernate.StaleObjectStateException:

我只是在尝试乐观锁定.

我有以下课程:

@Entity
public class Student {

    private Integer id;
    private String firstName;
    private String lastName;
    private Integer version; 
@Version
    public Integer getVersion() {
        return version;
    }

//all other getters ommited.
}
Run Code Online (Sandbox Code Playgroud)

现在我正在抓住其中一个学生并尝试同时更新其属性.

Thread t1 = new Thread(new MyRunnable(id));
    Thread t2 = new Thread(new MyRunnable(id));
    t1.start();
    t2.start();
Run Code Online (Sandbox Code Playgroud)

和MyRunnable内部:

public class MyRunnable implements Runnable {
    private Integer id;
    @Override
    public void run() {
        Session session = HibernateUtil.getSessionFactory().openSession();       
        session.beginTransaction();
        Student student = (Student) session.load(Student.class, id);
        student.setFirstName("xxxx");
        session.save(student);
        session.getTransaction().commit();
        System.out.println("Done"); 
    }

    public MyRunnable(Integer …
Run Code Online (Sandbox Code Playgroud)

java concurrency hibernate java-ee

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

在Spring MVC中返回@Async方法结果并将其返回给Ajax客户端

我在Controller中有一些执行@Async任务的方法

@Async
public Future<String> getResultFromServer(){
    String result = ......
    return new AsyncResult<String>(result);
} 
Run Code Online (Sandbox Code Playgroud)

方法执行时间最长为1分钟.我需要做的只是将结果返回给客户端,将使用AJAX/JQuery连接.

我不希望客户端每秒都请求我的服务器是否@Async执行方法.我只想保持连接打开,然后将结果"推送"到服务器.

@RequestMapping(value="/async.do", method=RequestMethod.POST)
public void getResult(HttpServletResponse res){
    String result = null;
    PrintWriter out = res.getWriter();
    Future<String> future = getResultFromServer();
    try {
        if (future.isDone())
            result = future.get();
        out.println(result);
        out.close();
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (ExecutionException e) {
        e.printStackTrace();
    }
}
Run Code Online (Sandbox Code Playgroud)

我明白这与Comit模型非常接近,但我对彗星一般不熟悉.

我的问题是如何使用JavaScript/JQuery在客户端保持连接打开?

我的@RequestMapping(value="/async.do", method=RequestMethod.POST)方法会将结果推送给客户吗?

javascript ajax spring spring-mvc java-ee

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

Cassandra CompositeType

我在Hector和Cassandra教程中看到过DynamicCompositeType.

任何人都可以详细说明它们之间的区别

   create column family Composite with comparator ='DynamicCompositeType
       (t=>TimeUUIDType,s=>UTF8Type)'
       and default_validation_class=UTF8Type and key_validation_class=UTF8Type;
Run Code Online (Sandbox Code Playgroud)

create column family Composite
    with comparator = 'CompositeType(TimeUUIDType,UTF8Type)' 
    and key_validation_class = 'UTF8Type' 
    and default_validation_class = 'UTF8Type'
Run Code Online (Sandbox Code Playgroud)

我没有在Cassandra文档中找到它

cassandra nosql hector

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

阿卡的战略模式

这是我之前的问题的延续我如何在Akka接收方法上绕过类型擦除

我有10种类型的事件,它从我需要处理的事件扩展而来.

我想在单独的特征中为每个事件实现业务逻辑,因为混合所有10个事件处理函数将产生数百(如果不是数千)行代码.

我不想为每个事件创建不同的Actor类型.例如:

class Event1Actor extend Actor{
  def receive ={
     case Event1(e) => //event1 Business Logic
   }
}

class Event2Actor extend Actor{
  def receive ={
     case Event2(e) => //event2 Business Logic
   }
}  
Run Code Online (Sandbox Code Playgroud)

和相同的Event3Actor,Event4Actor等....

这样的代码对我来说似乎很难看,因为我需要在每个Actor中实现业务逻辑.

实现10个不同的特征和10个不同的Actor类似乎也是糟糕的设计.

我正在寻找基于设计模式的某种通用解决方案,例如策略模式.

design-patterns scala akka

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

JSF 2.1条件字段验证

我有以下JSF 2.1页面

<h:selectOneRadio value="#{userBean.newUser}">
    <f:selectItem itemValue="0" itemLabel="new User" />
    <f:selectItem itemValue="1" itemLabel="existing User" />
</h:selectOneRadio>
<br />
<h:inputText value="#{userBean.customerId}" id="customerId" />
<h:message for="customerid" />
<br />
<h:inputText value="#{userBean.firstName}" id="firstName" />
<h:message for="fisrtName" />
<br />
<h:inputText value="#{userBean.lastName}" id="lastName" />
<h:message for="lastName" />
<br />
<h:commandButton value="Submit" action="#{userBean.login}" />
Run Code Online (Sandbox Code Playgroud)

这是我的豆子:

public class UserBean {

    private String customerId;
    private String newUser= "0";
    private String firstName;
    private String lastName;

    // Getters and seeters ommited.
}
Run Code Online (Sandbox Code Playgroud)

我需要通过以下方式验证此表单:

如果选择"新用户"单选按钮,则应验证所有表单输入.如果选择"现有用户",我只需要验证客户ID.

我尝试了Hibernate验证,我还通过实现javax.faces.validator.Validator接口尝试了自定义验证器.

我能以某种方式实现这样的功能吗?

java jsf richfaces java-ee jsf-2

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

Java for newbies - DeadLock模仿

我正在尝试编写一个非常简单的程序,它将模仿简单的DeadLock,其中线程A等待由线程B锁定的资源A,线程B等待由线程A锁定的资源B.

这是我的代码:

//it will be my Shared resource
public class Account {
    private float amount;

    public void debit(double amount){
        this.amount-=amount;
    }

    public void credit(double amount){
        this.amount+=amount;
    }

}  
Run Code Online (Sandbox Code Playgroud)

这是我的runnable,它在上面的资源上执行Operation:

public class BankTransaction implements Runnable {
    Account fromAccount,toAccount;
    float ammount;
    public BankTransaction(Account fromAccount, Account toAccount,float ammount){
        this.fromAccount = fromAccount;
        this.toAccount = toAccount;
        this.ammount = ammount;
    }

    private void transferMoney(){
        synchronized(fromAccount){
            synchronized(toAccount){
                fromAccount.debit(ammount);
                toAccount.credit(ammount);
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                e.printStackTrace();
                }
                System.out.println("Current Transaction Completed!!!");
            }
        }
    }

    @Override
    public …
Run Code Online (Sandbox Code Playgroud)

java concurrency multithreading deadlock

5
推荐指数
2
解决办法
376
查看次数

可调用执行期间的异常

我有以下Callable:

public class Worker implements Callable<Boolean>{

   @Override
   public Boolean call(){
      boolean success=true;

      //do Something
     return success;
   }

}
Run Code Online (Sandbox Code Playgroud)

现在我正在执行它:

Worker worker - new Worker();
Future<Boolean> submit = executor.submit(worker);
Run Code Online (Sandbox Code Playgroud)

我正在以一种hashMap的形式存储提交,以便在代码中的某个地方执行某些操作.

如何知道worker.call()函数中是否发生了异常?

请问submit.isCancelled(),如果发生假某种异常的,如果一切工作正常返回true?

java concurrency future java-ee

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

Quartz - 在一周和几天的某一天每两周安排一次工作

我需要创建Job,它将:

  • 2012年12月20日开始
  • endDate = 12/31/2017
  • 将在周日和周一每两周举行一次
  • 下午5点开火.

这个cron表达式有效吗?

Date start = 12/20/2012;
Date endDate = 12/31/2017;
SimpleTrigger trigger = newTrigger()
    .withIdentity("trigger3", "group1")
    .startAt(startDate) 
    .withSchedule(cronSchedule("* * 17 0 0/2 *,SUN,MON").build())
    .endAt(endDate)
    .build;
Run Code Online (Sandbox Code Playgroud)

请指教.

java cron quartz-scheduler java-ee

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