【问题标题】:Spring NullPointerException serviceSpring NullPointerException 服务
【发布时间】:2013-11-25 21:15:17
【问题描述】:

我正在尝试使用 Spring 3 Struts 2 和 Hibernate 设置一个小型 Web 应用程序,但我做错了我无法弄清楚它是什么,当我尝试调用 struts 控制器中的服务层时,我得到一个 NullPointerException .

实体:

@Entity(name="T_Personne")
public class Personne {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long id;

    @Column(nullable=false,length=30)
    private String nom;
}

道:

@Repository(value="personneDao")
public class PersonneDaoImpl implements IPersonneDao{


    @PersistenceContext
    private EntityManager em;

    public void setEntityManager(EntityManager entityManager) {
        this.em = entityManager;
    }


    public void save(Personne per) {

        em.persist(per);
    }
}

服务:

@Service(value="personneService")
@Transactional
public class PersonneServiceImpl implements IPersonneService {

    @Autowired
    private IPersonneDao personneDao;

    public void savePersonne(Personne per) {
        personneDao.save(per);

    }

}

控制器:

public class HelloAction extends ActionSupport{

    @Autowired
    private IPersonneService personneService;


    public String execute() throws Exception{

            Personne p = personneService.findPersonneById(1L);
            System.out.println("Nom : "+ p.getNom() + "Prenom : "+ p.getPrenom());

        return SUCCESS;

    } 
}

Spring XML:

<context:annotation-config />

<context:component-scan base-package="net.cadr" />

<tx:annotation-driven transaction-manager="transactionManager"/>

<bean class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean" id="entityManagerFactory">
        <property name="persistenceUnitName" value="MyPersistenceUnit"/>
    </bean>

<bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>

Struts XML:

<struts>  

    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="true" />

    <package name="personne" extends="struts-default">

        <action name="Hello" class="net.cadr.actions.HelloAction">
            <result name="success">/views/hello.html</result>
        </action>

    </package>

</struts>

问题是实体管理器创建表,struts 控制器也在工作,我可以映射动作并调用它们!!!

我错过了什么?

谢谢

【问题讨论】:

  • 你从哪里得到NPE?
  • 您的 Personne 服务没有 findPersonneById 方法。您确定代码可以编译,还是您没有发布正确的代码?
  • 嗨,我在这一行得到了异常 Personne p = personneService.findPersonneById(1L);谢谢
  • 第18行是哪一个?
  • 嗨,第 18 行是:Personne p = personneService.findPersonneById(1L);

标签: java spring hibernate struts2 jpa-2.0


【解决方案1】:

这是因为你的控制器可能没有spring bean(Spring默认不使用Struts,而是使用自己的框架:Spring-MVC)。

如果您将 Spring 与 Struts 一起使用,那么您应该将此行添加到您的 struts 配置中:

<constant name="struts.objectFactory"
          value="org.apache.struts2.spring.StrutsSpringObjectFactory" />

@见Struts Spring Plugin

【讨论】:

  • 从 2.3.x 开始,只需插入插件 struts.apache.org/release/2.3.x/docs/spring-plugin.html
  • @LukaszLenart:你的意思是不需要它吗?来自参考(黄色框)“但是,如果你想使用 Spring 来注入你的动作,那么这个选项就在那里。”
  • 这意味着,您不必在 Spring 配置中定义每个操作。并且:To enable Spring integration, simply include struts2-spring-plugin-x-x-x.jar in your application. If you are using more than one object factory, (for example, by including both the Spring and Plexus plugins in your application,) you will need to set the struts.objectFactory property in struts.properties or in one of several XML files via Constant
猜你喜欢
  • 2016-05-23
  • 1970-01-01
  • 2022-11-08
  • 2020-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-12
  • 1970-01-01
相关资源
最近更新 更多