SSH框架整合,Spring只使用核心模块IOC
applicationContext.xml配置如下:
<bean id="userServiceImpl" class="com.founder.ssj.service.impl.UserServiceImpl" >
</bean>
<bean id="loginAction" class="com.founder.ssj.action.user.LoginAction" >
<property name="userService">
<ref bean="userServiceImpl"/>
</property>
</bean>
web.xml配置如下:
<!-- Spirng配置文件位置 -->
contextConfigLocation
classpath:applicationContext.xml
<!-- 加载 Spring -->
org.springframework.web.context.ContextLoaderListener
struts2
org.apache.struts2.dispatcher.FilterDispatcher
struts2
/*
struts配置如下:
<package name="main" extends="struts-default" >
<action name="login" class="com.founder.ssj.action.user.LoginAction" >
<result name="success">/index.jsp</result>
<result name="fail">/login.jsp</result>
</action>
</package>
Action 代码:GET SET 都全了
public class LoginAction extends ActionSupport{
//为何没有被注入?
private IUserService userService;
private String username;
private String password;
public LoginAction() {
super();
System.out.println(2);
}
/**
*
*/
private static final long serialVersionUID = 835443313500278156L;
@Override
public String execute(){
String state="fail";
try {
if(userService.login(username, password)){
state="success";
};
} catch (Exception e) {
e.printStackTrace();
}
return state;
}
每当执行到该方法时,userService 对象都是空?
为什么呢?
在struts.xml中加入
<constant name="struts.objectFactory" value="spring" />
然后把 <action name="login" class="com.founder.ssj.action.user.LoginAction" >改成
<action name="login" class="loginAction" >
因为在你的action类里 没有get set