小编Jam*_*hon的帖子

修复Spring MVC应用程序中的Null EntityManger?

在下面的代码中,我注入了EnitityManager,它总是显示为null ;

public class GenericController extends AbstractController {

    @PersistenceContext(unitName = "GenericPU")
    private EntityManager em;

    protected ModelAndView handleRequestInternal(
            HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        //(em == null) is always trigged
        if (em == null) throw new NullPointerException("em is null");
        Collection<Generic> Generics = em.createNamedQuery("Generic.findAll").getResultList();

        ModelAndView mav = new ModelAndView("Generic");
        mav.addObject(Generics); 
        return mav;
    }
}
Run Code Online (Sandbox Code Playgroud)

这是bean定义,在dispatcher-servlet.xml中定义.

<bean id="Generic" class="com.application.web.GenericController" />
Run Code Online (Sandbox Code Playgroud)

应该在persistence-context.xml文件中定义基于tx:annotation的注入EnitityManager .

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">

    <bean …
Run Code Online (Sandbox Code Playgroud)

java spring jpa spring-mvc entitymanager

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

在Python的NLTK中从自定义文本生成随机句子?

我在Python下使用NLTK遇到了麻烦,特别是.generate()方法.

生成(自我,长度= 100)

打印使用trigram语言模型生成的随机文本.

参数:

   * length (int) - The length of text to generate (default=100)
Run Code Online (Sandbox Code Playgroud)

这是我正在尝试的简化版本.

import nltk

words = 'The quick brown fox jumps over the lazy dog'
tokens = nltk.word_tokenize(words)
text = nltk.Text(tokens)
print text.generate(3)
Run Code Online (Sandbox Code Playgroud)

这将始终生成

Building ngram index...
The quick brown
None
Run Code Online (Sandbox Code Playgroud)

而不是建立一个随机短语.

这是我的输出

print text.generate()

Building ngram index...
The quick brown fox jumps over the lazy dog fox jumps over the lazy
dog dog The quick brown fox jumps over the lazy dog …
Run Code Online (Sandbox Code Playgroud)

python random nltk

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

为什么Java开发人员应该学习其他JVM语言?

我是一名java开发人员,我想知道,学习Scala或Groovy等语言的主要好处是什么?

java jvm jvm-languages

7
推荐指数
2
解决办法
731
查看次数

非主键列的自动递增数字生成

我对主键使用了以下 id 生成策略。

    @Id
        @GeneratedValue(strategy = IDENTITY)
        @Column(name = "id", unique = true, nullable = false, insertable = false, updatable = false)
        private Integer id;
Run Code Online (Sandbox Code Playgroud)

我想对非主键列做同样的事情。[a] 为这样的密钥定义自动生成方案的语义是什么 [b] 是否可以保证生成的数字不会出现数字差距。

hibernate jpa

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

Groovy合并两个列表?

我有两个清单:

listA: 
[[Name: mr good, note: good,rating:9], [Name: mr bad, note: bad, rating:5]] 

listB: 
[[Name: mr good, note: good,score:77], [Name: mr bad, note: bad, score:12]]
Run Code Online (Sandbox Code Playgroud)

我想得到这个

listC:
[[Name: mr good, note: good,, rating:9, score:77], [Name: mr bad, note: bad, rating:5,score:12]] 
Run Code Online (Sandbox Code Playgroud)

我怎么能这样做?

谢谢.

merge groovy list

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

是否有一个库来支持Javascript对象的自动修复?

无论如何,无论是本地还是通过库,在Javascript对象上使用自动生成?

IE,假设foo是一个没有属性的对象,能够做foo.bar.baz = 5而不是需要foo.bar = {}; foo.bar.baz = 5.

javascript object autovivification

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

ExtJS在远程排序存储上设置默认排序?

ExtJS 4.1.3中有没有办法在Grid上设置默认排序,后备存储设置为remoteSort: true

extjs

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

将模拟服务注入到Grails单元测试的域类中?

我正在Grails 2.1.1下编写一些基于Spock规范的单元测试.我无法springSecurityService注入我单位使用的域对象.

这是我到目前为止,

@Mock([SecUser])
@TestFor(FooService)
class FooServiceSpec extends Specification {

    def "test some stuff"() {
        given:
        def mockSecUserService = Mock(SecUserService)
        mockSecUserService.emailValid(_) >> { true }
        mockSecUserService.checkUsername(_) >> { null }
        service.secUserService = mockSecUserService

        def mockSpringSecurityService = Mock(SpringSecurityService)
        mockSpringSecurityService.encodePassword(_) >> { 'tester' }
        // FIXME this needs to be injected somehow

        def params = [
                email: 'unittest@test.com',
                username: 'unittester',
                password: 'tester',
                password2: 'tester'
        ]

        when:
        def result = service.createUser(params)

        then:
        // test results
    }
}
Run Code Online (Sandbox Code Playgroud)

所以会发生的事情是我的测试服务会抛出一个NullPointerException因为没有注入mockSpringSecruityService.我的服务中的createUser调用验证一些参数,然后创建一个SecUser …

testing grails groovy unit-testing spock

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

自动浇铸弹簧豆

有没有办法将Spring bean自动转换为应用程序上下文XML中定义的类?我想避免在两个位置放置关于bean的类型信息....在xml配置文件中以及在代码中作为强制转换.

例如,给定此配置文件

<bean id="bean-name" class="SimpleSpringBean"  scope="prototype">
    <property name="myValue" value="simple value"></property>
</bean>
Run Code Online (Sandbox Code Playgroud)

我可以这样调用ApplicationContext.getBean("bean-name")以避免直接将返回类型转换为SimpleStringBean.我知道我也可以打电话ApplicationContext.getBean("bean-name", SimpleSpringBean.class)来避免演员本身,但我仍然有2个地方的类型信息.

看起来Spring可以获取类info(ApplicationContext.getType)或者从bean本身获取类型,但没有办法在没有程序员干预的情况下自动转换类型.

java spring casting javabeans

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

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