Spring与struts2不配合要怎么整 呢??
spring与hibernate 一起做测试的时侯是没问题的但是在整 合struts的时侯报这个
空指针java.lang.NullPointerException kqserviceimp.LoginServiceImp.login(LoginServiceImp.java:28)
action.LoginAction.login(LoginAction.java:45)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:452)
com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:291)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:254)
com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:176)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
我单独测Spring是没这个问题的 struts2-spring-plugin这个插件我也放到了lib目录里
Struts 和spring我都是用的注解配置的
这是Strutsr 配置
import java.util.Map;
import org.apache.struts2.convention.annotation.*;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import kq.service.LoginService;
import kqserviceimp.LoginServiceImp;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
@Component
@Namespace("/")
public class LoginAction extends ActionSupport{
/**
*
*/
private static final long serialVersionUID = 1L;
private String username;
private String password;
private LoginService loginservice=new LoginServiceImp();
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Actions({
@Action(value="/login/log",results={
@Result(name="Success",location="WEB-INF/loginok.jsp"),
@Result(name="fail",location="WEB-INF/loginfail.jsp")
}),
@Action("/test/tx")
})
public String login(){
boolean t=loginservice.login(username, password);
if(t==true){
ActionContext actioncontext=ActionContext.getContext();
Map session=actioncontext.getSession();
session.put("username", username);
return "Success";
}
return "fail";
}
@Actions(
@Action(value="/ax/t",results={
@Result(name="null",location="/loginok.jsp")
}))
public String ok() {
return "null";
}
}
这是spring 的配置
import java.util.Properties;
import javax.annotation.Resource;
import javax.sql.DataSource;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.Property;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean;
@Configuration
public class SpringConfig {
private @Value("#{jdbcProperties.url}") String jdbcurl;
private @Value("#{jdbcProperties.username}") String username;