Bonjour,
Après un long moment d'hésitation entre l'utilisation de EJB3/JSF ou Spring/Struts2 dans un projet JEE, j'ai décidé de tester les deux plateformes.
J'ai commencé par Spring/Struts2 et je me suis retrouvé face au problème d'injection de Spring.
le message d'erreur affiché est :
StandardWrapperValve[default]: PWC1406 : servlet.service() pour le servlet default a émis une exception.
java.lang.NullPointerException
at com.example.action.LoginAction.save(LoginAction.java:47)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
Je suis pas un expert dans le domaine, mais j'ai cru comprendre que Spring n'a pas instancié la classe métier dans la classe action. le processus d'injection ne fonctionne pas, c'est qu'il y a un problème dans le fichier de configuration (injection par constructeur!!!!)
ci dessous le code de mes trois classes, et le fichier de configuration qui génère l'erreur.
LA CLASSE ACTION
package com.example.action;
import com.example.domain.Login;
import com.example.service.LoginServ;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
import java.util.List;
public class LoginAction extends ActionSupport{
private LoginServ loginservice;
private List<Login> logins;
private Login login;
private Integer id;
private String loginid;
private String password;
private String email;
private String address;
private Integer phno;
@Override
public String execute() {
this.logins = loginservice.findAll();
return Action.SUCCESS;
}
public String save() {
login=new Login();
login.setId(this.id);
login.setAddress(this.address);
login.setLoginid(this.loginid);
login.setPassword(this.password);
login.setPhno(this.phno);
login.setEmail(this.email);
System.out.println(login.toString());
loginservice.save(login);
// this.login = new Login();
return execute();
}
public String remove() {
loginservice.remove(id);
return execute();
}
public LoginServ getLoginservice() {
return loginservice;
}
public void setLoginservice(LoginServ loginservice) {
this.loginservice = loginservice;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getLoginid() {
return loginid;
}
public void setLoginid(String loginid) {
this.loginid = loginid;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Integer getPhno() {
return phno;
}
public void setPhno(Integer phno) {
this.phno = phno;
}
public List<Login> getLogins() {
return logins;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public void prepare() throws Exception {
if (id != null)
login = loginservice.find(id);
}
public Login getLogin() {
return login;
}
public void setLogin(Login login) {
this.login = login;
}
}
LA CLASSE METIER
package com.example.service;
import com.example.dao.LoginDao;
import com.example.domain.Login;
import java.util.List;
import org.springframework.transaction.annotation.Transactional;
@Transactional
public class LoginServImpl implements LoginServ{
private LoginDao logindao;
public LoginDao getlogindao() {
return logindao;
}
public void setlogindao(LoginDao logindao) {
this.logindao = logindao;
}
public List<Login> findAll(){
return logindao.findAll();
}
public void save(Login login){
logindao.save(login);
}
public void remove(int id){
logindao.remove(id);
}
public Login find(int id){
return logindao.find(id) ;
}
}
LE FICHIER DE CONFIGURATION APPLICATIONCONTEXTE
Implementation de JPA: TOPLINK
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans default-autowire="autodetect">
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
<!-- ========================= RESOURCE DEFINITIONS ========================= -->
<!-- Configurer that replaces ${...} placeholders with values from a properties file -->
<!-- (in this case, JDBC-related settings for the dataSource definition below) -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location"><value>/resources/jdbc.properties</value></property>
</bean>
<!-- Local DataSource that works in any environment -->
<!-- Note that DriverManagerDataSource does not pool; it is not intended for production -->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.ContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.TopLinkJpaVendorAdapter">
<property name="showSql" value="true"/>
<property name="generateDdl" value="true"/>
<property name="databasePlatform" value="oracle.toplink.essentials.platform.database.HSQLPlatform"/>
</bean>
</property>
<property name="loadTimeWeaver">
<bean class="org.springframework.instrument.classloading.SimpleLoadTimeWeaver"/>
</property>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"><value>${jdbc.driverClassName}</value></property>
<property name="url"><value>${jdbc.url}</value></property>
<property name="username"><value>${jdbc.username}</value></property>
<property name="password"><value>${jdbc.password}</value></property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- ========================= BUSINESS OBJECT DEFINITIONS ========================= -->
<!-- couches applicatives -->
<bean id="logindao" class="com.example.dao.LoginDaoImpl"></bean>
<bean id="loginservice" class="com.example.service.LoginServImpl">
<property name="logindao" ref="logindao" />
</bean>
<!-- Email Service -->
<bean id="mailbean" class="com.example.mail.SendMail">
<property name="strSmtp"><value>127.0.0.1</value></property>
</bean>
<!-- actions -->
<bean id="loginAction" class="com.example.action.LoginAction">
<property name="loginservice" ref="loginservice" />
</bean>
</beans>
J'ai essayé de mixer Stuts2 et JSF mais ça n'a pas marché et il n'y a pas assez de doc sur le net, dommmmmmmmmage
Merci.
Partager