SSH配置没报错,但运行程序时为什么spring和hibernate不起作用
小弟刚接触SSH,不太会,求大神指导。applicationContext.xml
struts.xml
我感觉没有错啊,可是为什么spring就是没有作用?纠结好几天了,那位大侠帮帮忙哦,感激不尽!! --------------------编程问答-------------------- web.xml 呢 --------------------编程问答--------------------
对呀 --------------------编程问答--------------------
这是web.xml,哪里有问题啊?求指教 --------------------编程问答--------------------
这是web.xml,哪里出错了?? --------------------编程问答-------------------- 应该把spring的配置文件配置到web.xml文件中来 --------------------编程问答-------------------- 比如:
<!-- spring的应用上下文 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/classes/applicationContext.xml,
/WEB-INF/classes/applicationContext-action.xml,
/WEB-INF/classes/applicationContext-service.xml,
/WEB-INF/classes/applicationContext-dao.xml
</param-value>
</context-param> --------------------编程问答--------------------
我配置了啊,开头不就是么 --------------------编程问答--------------------
会不会是我的路径出错了?我的路径是这样的
--------------------编程问答-------------------- 你要配置在tomcat下的路径 --------------------编程问答-------------------- 不好意思 刚刚看漏了 --------------------编程问答-------------------- 没起作用 具体是什么情况 --------------------编程问答--------------------
就是运行程序时不起作用,我觉得应该是没有进去spring的配置,hibernate也是一样的,jsp显示这样的错误
--------------------编程问答--------------------
呵呵,还是谢谢了,纠结好几天了都。。 --------------------编程问答-------------------- 很明显空指针异常嘛,都已经进userAction的login方法了。是不是userService的bean没配置.userAction类21行是什么 --------------------编程问答-------------------- 启动时候 如果配置log的话,会把spring加载的文件log打印出来 看看你的启动log --------------------编程问答-------------------- <bean id="userAction" class="com.bysj.useraction">
</bean>
有这个配置吗 --------------------编程问答-------------------- 没有userAction这个bean,看你的applicationContext不是基于annotation的配置,也没有手动定义这个bean。当然找不到 --------------------编程问答--------------------
有的,我另外写的一个users.xml,在applicationContext.xml里面调用的,在applicationcontext.xml最后不是有个<import resource="application-user.xml">吗,就在那个里面
applicationContext.xml
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.0.xsd ">
<!-- 用户管理 -->
<bean id="IUsersDao" class="com.bysj.dao.impl.IUsersDao" scope="prototype">
<property name="sessionFactory" ref="sessionFactory" />
<property name="jdbcTemplate" ref="jdbcTemplate" />
</bean>
<bean id="IUsersService" class="com.bysj.service.impl.IUsersService" scope="prototype">
<property name="IUsersDao" ref="IUsersDao" />
</bean>
<bean id="UsersAction" class="com.bysj.action.UsersAction" scope="prototype">
<property name="IUsersService" ref="IUsersService" />
</bean>
</beans>
是这个吧 --------------------编程问答--------------------
是java代码
package com.bysj.action;
import java.util.Map;
import org.apache.struts2.interceptor.SessionAware;
import com.bysj.model.Users;
import com.bysj.service.UsersService;
import com.bysj.service.impl.IUsersService;
import com.opensymphony.xwork2.ActionSupport;
public class UsersAction extends ActionSupport implements SessionAware
{
private IUsersService UsersService;
private Users users;
public String Login()
{
//UsersService= new IUsersService();
boolean b=UsersService.Login(users);//这是第21行
if(b)
return SUCCESS;
return ERROR;
}
public void setSession(Map<String, Object> arg0)
{
}
public Users getUsers()
{
return users;
}
public void setUsers(Users users)
{
this.users = users;
}
public UsersService getUsersService()
{
return UsersService;
}
public void setUsersService(IUsersService usersService)
{
UsersService = usersService;
}
}
--------------------编程问答--------------------
有的,我另外写的一个users.xml,在applicationContext.xml里面调用的,在applicationcontext.xml最后不是有个<import resource="application-user.xml">吗,就在那个里面
application-user.xml
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.0.xsd ">
<!-- 用户管理 -->
<bean id="IUsersDao" class="com.bysj.dao.impl.IUsersDao" scope="prototype">
<property name="sessionFactory" ref="sessionFactory" />
<property name="jdbcTemplate" ref="jdbcTemplate" />
</bean>
<bean id="IUsersService" class="com.bysj.service.impl.IUsersService" scope="prototype">
<property name="IUsersDao" ref="IUsersDao" />
</bean>
<bean id="UsersAction" class="com.bysj.action.UsersAction" scope="prototype">
<property name="IUsersService" ref="IUsersService" />
</bean>
</beans>
是这个吧 --------------------编程问答-------------------- 你的UserAction里的Login方法报错了,把报错的方法或者整个类贴出来看看 --------------------编程问答-------------------- <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/classes/applicationContext.xml,
/WEB-INF/classes/applicationContext-action.xml,
/WEB-INF/classes/applicationContext-service.xml,
/WEB-INF/classes/applicationContext-dao.xml
</param-value>
</context-param>
路径不对 看你项目路径 少了个config --------------------编程问答--------------------
有的,在WEB-INF/config/applicationContext.xml里面 --------------------编程问答--------------------
package com.bysj.action;
import java.util.Map;
import org.apache.struts2.interceptor.SessionAware;
import com.bysj.model.Users;
import com.bysj.service.UsersService;
import com.bysj.service.impl.IUsersService;
import com.opensymphony.xwork2.ActionSupport;
public class UsersAction extends ActionSupport implements SessionAware
{
private IUsersService UsersService;
private Users users;
public String Login()
{
//UsersService= new IUsersService();
boolean b=UsersService.Login(users);
if(b)
return SUCCESS;
return ERROR;
}
public void setSession(Map<String, Object> arg0)
{
}
public Users getUsers()
{
return users;
}
public void setUsers(Users users)
{
this.users = users;
}
public UsersService getUsersService()
{
return UsersService;
}
public void setUsersService(IUsersService usersService)
{
UsersService = usersService;
}
}
就是这个,不会有错吧 --------------------编程问答-------------------- private IUsersService UsersService;
和
<bean id="UsersAction" class="com.bysj.action.UsersAction" scope="prototype">
<property name="IUsersService" ref="IUsersService" />
</bean>
比较,楼主太粗心了
明显注入的属性不对嘛。name应该是UsersService,一般不要大写,不规范,多难看 --------------------编程问答-------------------- 对了嘛,你配置离注入的是属性 IUsersService,而你类里的属性名称是UsersService,你在Login方法里调用它,肯定报空指针啦 --------------------编程问答--------------------
我是说你的配置文件的目录里少了config
/WEB-INF/classes/applicationContext.xml,这个里面少了config --------------------编程问答--------------------
不是这个问题吧,我改过了,还是出现同样的问题
--------------------编程问答--------------------
这个不是我的配置,我的配置是这样的
WEB-INF/config/applicationcontext.xml --------------------编程问答--------------------
我现在改了,是这样的
我的action里的UsersAction代码是这样的:
package com.bysj.action;
import java.util.Map;
import org.apache.struts2.interceptor.SessionAware;
import com.bysj.model.Users;
import com.bysj.service.UsersService;
import com.bysj.service.impl.IUsersService;
import com.opensymphony.xwork2.ActionSupport;
public class UsersAction extends ActionSupport implements SessionAware
{
private IUsersService UsersService;
private Users users;
public String Login()
{
//UsersService= new IUsersService();
boolean b=UsersService.Login(users);
if(b)
return SUCCESS;
return ERROR;
}
public void setSession(Map<String, Object> arg0)
{
}
public Users getUsers()
{
return users;
}
public void setUsers(Users users)
{
this.users = users;
}
public UsersService getUsersService()
{
return UsersService;
}
public void setUsersService(IUsersService usersService)
{
UsersService = usersService;
}
}
是这样的吗?可是还是同样的问题啊 --------------------编程问答--------------------