ApplicationContext.xml
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd" >
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="com.mysql.jdbc.Driver">
</property>
<property name="url" value="jdbc:mysql://localhost:3306/db_database07"></property>
<property name="username" value="root"></property>
<property name="password" value="111"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/lwy/dto/Userlogin.hbm.xml</value>
<value>com/lwy/dto/Specialty.hbm.xml</value>
<value>com/lwy/dto/Stuuser.hbm.xml</value>
<value>com/lwy/dto/Course.hbm.xml</value>
<value>com/lwy/dto/Stucourse.hbm.xml</value></list>
</property></bean>
<!-- 定义事务管理器 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean> <!--定义注释驱动-->
<tx:annotation-driven transaction-manager="transactionManager" /> <!-- -->
<bean id="userLoginDao" class="com.lwy.dao.UserLoginDao">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="specialtyDao" class="com.lwy.dao.SpecialtyDao">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="stuUserDao" class="com.lwy.dao.StuUserDao">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="courseDao" class="com.lwy.dao.CourseDao">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean name="/userLogin" class="com.lwy.action.UserLoginAction">
<property name="userLoginDao" ref="userLoginDao" />
<property name="stuUserDao" ref="stuUserDao" />
<property name="specialtyDao" ref="specialtyDao" />
</bean>
<bean name="/reg" class="com.lwy.action.RegAction">
<property name="userLoginDao" ref="userLoginDao"></property>
<property name="specialtyDao" ref="specialtyDao"></property>
</bean>
<bean name="/stuUser" class="com.lwy.action.StuUserAction">
<property name="stuUserDao" ref="stuUserDao" />
<property name="courseDao" ref="courseDao" />
<property name="specialtyDao" ref="specialtyDao" />
</bean>
<bean name="/specialty" class="com.lwy.action.SpecialtyAction">
<property name="specialtyDao" ref="specialtyDao"></property>
</bean>
<bean name="/course" class="com.lwy.action.CourseAction">
<property name="courseDao" ref="courseDao"></property>
<property name="specialtyDao" ref="specialtyDao"></property>
</bean>
<bean name="/statInfo" class="com.lwy.action.StatInfoAction">
<property name="courseDao" ref="courseDao"></property>
<property name="specialtyDao" ref="specialtyDao"></property>
</bean>
<bean name="/updatePwd" class="com.lwy.action.UpdatePwdAction">
<property name="userLoginDao" ref="userLoginDao"></property>
</bean>
</beans>
UserLoginAction:
public class UserLoginAction extends Action {
private ISpecialtyDao specialtyDao;
private IStuUserDao stuUserDao;
private IUserLoginDao userLoginDao;
/*
* Generated Methods
*/
public void setSpecialtyDao(ISpecialtyDao specialtyDao) {
this.specialtyDao = specialtyDao;
} public void setStuUserDao(IStuUserDao stuUserDao) {
this.stuUserDao = stuUserDao;
} public void setUserLoginDao(IUserLoginDao userLoginDao) {
this.userLoginDao = userLoginDao;
}
/**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
DynaActionForm userLoginForm = (DynaActionForm) form;// TODO Auto-generated method stub
ApplicationContext ac = new ClassPathXmlApplicationContext("../applicationContext.xml");
IUserLoginDao dao = (IUserLoginDao)ac.getBean("userLoginDao");
System.out.println(" dao --> "+ dao);
Userlogin userLogin=userLoginDao.findbyNameAndPwd(userLoginForm.getString("loginName"), userLoginForm.getString("pwd"));
if(userLogin==null){
request.setAttribute("error", "用户登录失败,用户名或密码不正确");
return mapping.findForward("index");
}
else{
request.getSession().setAttribute("loginName", userLogin.getLoginName());
request.getSession().setAttribute("id",userLogin.getId());
if(userLogin.getType().equals("1")){
System.out.println("管理员登录");
return mapping.findForward("manager");
}
else{
Stuuser stuUser=stuUserDao.findById(userLogin.getId());
if(stuUser==null){
System.out.println("未填写基本信息");
List<Specialty> list=specialtyDao.findStuByAll();
request.setAttribute("list", list);
return mapping.findForward("addStuInfo");
}
else{
Specialty specialty=specialtyDao.findById(stuUser.getSpecialtyId());
request.setAttribute("specialty", specialty);
request.setAttribute("stuUser", stuUser);
System.out.println("填写过基本信息");
return mapping.findForward("welcome");
}
} }
}
}
经过测试,在UserLoginAction中调用UserLoginDao时,UserLoginDao=null。通过ApplicationContext中获取bean,ApplicationContext ac = new ClassPathXmlApplicationContext("../applicationContext.xml");
IUserLoginDao dao = (IUserLoginDao)ac.getBean("userLoginDao");
这样可以获取dao,应该是注入问题,但是本人是在看不出哪里有问题,纠结好久了,求大神帮忙
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd" >
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="com.mysql.jdbc.Driver">
</property>
<property name="url" value="jdbc:mysql://localhost:3306/db_database07"></property>
<property name="username" value="root"></property>
<property name="password" value="111"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/lwy/dto/Userlogin.hbm.xml</value>
<value>com/lwy/dto/Specialty.hbm.xml</value>
<value>com/lwy/dto/Stuuser.hbm.xml</value>
<value>com/lwy/dto/Course.hbm.xml</value>
<value>com/lwy/dto/Stucourse.hbm.xml</value></list>
</property></bean>
<!-- 定义事务管理器 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean> <!--定义注释驱动-->
<tx:annotation-driven transaction-manager="transactionManager" /> <!-- -->
<bean id="userLoginDao" class="com.lwy.dao.UserLoginDao">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="specialtyDao" class="com.lwy.dao.SpecialtyDao">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="stuUserDao" class="com.lwy.dao.StuUserDao">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="courseDao" class="com.lwy.dao.CourseDao">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean name="/userLogin" class="com.lwy.action.UserLoginAction">
<property name="userLoginDao" ref="userLoginDao" />
<property name="stuUserDao" ref="stuUserDao" />
<property name="specialtyDao" ref="specialtyDao" />
</bean>
<bean name="/reg" class="com.lwy.action.RegAction">
<property name="userLoginDao" ref="userLoginDao"></property>
<property name="specialtyDao" ref="specialtyDao"></property>
</bean>
<bean name="/stuUser" class="com.lwy.action.StuUserAction">
<property name="stuUserDao" ref="stuUserDao" />
<property name="courseDao" ref="courseDao" />
<property name="specialtyDao" ref="specialtyDao" />
</bean>
<bean name="/specialty" class="com.lwy.action.SpecialtyAction">
<property name="specialtyDao" ref="specialtyDao"></property>
</bean>
<bean name="/course" class="com.lwy.action.CourseAction">
<property name="courseDao" ref="courseDao"></property>
<property name="specialtyDao" ref="specialtyDao"></property>
</bean>
<bean name="/statInfo" class="com.lwy.action.StatInfoAction">
<property name="courseDao" ref="courseDao"></property>
<property name="specialtyDao" ref="specialtyDao"></property>
</bean>
<bean name="/updatePwd" class="com.lwy.action.UpdatePwdAction">
<property name="userLoginDao" ref="userLoginDao"></property>
</bean>
</beans>
UserLoginAction:
public class UserLoginAction extends Action {
private ISpecialtyDao specialtyDao;
private IStuUserDao stuUserDao;
private IUserLoginDao userLoginDao;
/*
* Generated Methods
*/
public void setSpecialtyDao(ISpecialtyDao specialtyDao) {
this.specialtyDao = specialtyDao;
} public void setStuUserDao(IStuUserDao stuUserDao) {
this.stuUserDao = stuUserDao;
} public void setUserLoginDao(IUserLoginDao userLoginDao) {
this.userLoginDao = userLoginDao;
}
/**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
DynaActionForm userLoginForm = (DynaActionForm) form;// TODO Auto-generated method stub
ApplicationContext ac = new ClassPathXmlApplicationContext("../applicationContext.xml");
IUserLoginDao dao = (IUserLoginDao)ac.getBean("userLoginDao");
System.out.println(" dao --> "+ dao);
Userlogin userLogin=userLoginDao.findbyNameAndPwd(userLoginForm.getString("loginName"), userLoginForm.getString("pwd"));
if(userLogin==null){
request.setAttribute("error", "用户登录失败,用户名或密码不正确");
return mapping.findForward("index");
}
else{
request.getSession().setAttribute("loginName", userLogin.getLoginName());
request.getSession().setAttribute("id",userLogin.getId());
if(userLogin.getType().equals("1")){
System.out.println("管理员登录");
return mapping.findForward("manager");
}
else{
Stuuser stuUser=stuUserDao.findById(userLogin.getId());
if(stuUser==null){
System.out.println("未填写基本信息");
List<Specialty> list=specialtyDao.findStuByAll();
request.setAttribute("list", list);
return mapping.findForward("addStuInfo");
}
else{
Specialty specialty=specialtyDao.findById(stuUser.getSpecialtyId());
request.setAttribute("specialty", specialty);
request.setAttribute("stuUser", stuUser);
System.out.println("填写过基本信息");
return mapping.findForward("welcome");
}
} }
}
}
经过测试,在UserLoginAction中调用UserLoginDao时,UserLoginDao=null。通过ApplicationContext中获取bean,ApplicationContext ac = new ClassPathXmlApplicationContext("../applicationContext.xml");
IUserLoginDao dao = (IUserLoginDao)ac.getBean("userLoginDao");
这样可以获取dao,应该是注入问题,但是本人是在看不出哪里有问题,纠结好久了,求大神帮忙
解决方案 »
- JAVA 关于TCP的疑问
- servlet跳转问题
- 请问如何通过java,将已有的word文件转成pdf文档。。。
- 急需!!Crystal Reports水晶报表中的一个jar包!!!
- strust如何实现:插入一条纪录(id字段用的是序列),紧接着将id字段的值返回!
- JSP变量写入数据库时是乱码!帮个忙啊~
- 请教:关于JSP页面显示的问题!谢谢!
- 信息发送至邮箱问题
- 在线高分求助:在JSP中用JAVA类实现关联的动态下拉列表(数据存在数组中)
- 在weblogic下应将bean放在什么目录下,为什么总是有错呢?
- web需求文档
- 各位,我在做支付 虽然将金额放到hidden用post提交但用户可以通过按F12改金额提交 应该怎么办?
private IStuUserDao stuUserDao;
private ISpecialtyDao specialtyDao; public void setSpecialtyDao(ISpecialtyDao specialtyDao) {
this.specialtyDao = specialtyDao;
} public void setStuUserDao(IStuUserDao stuUserDao) {
this.stuUserDao = stuUserDao;
} public void setUserLoginDao(IUserLoginDao userLoginDao) {
this.userLoginDao = userLoginDao;
}
set方法已经写了
ApplicationContext ac = new ClassPathXmlApplicationContext("../applicationContext.xml");
IUserLoginDao dao = (IUserLoginDao)ac.getBean("userLoginDao");
System.out.println(" dao --> "+ dao);
这后面打印dao里有返回值的一个方法,看有值不。
这有问题吧,userLoginDao 应该是dao吧,你写错了吧
System.out.println(" dao --> "+ dao);
Userlogin userLogin=userLoginDao.findbyNameAndPwd(userLoginForm.getString("loginName"), userLoginForm.getString("pwd"));
这里,你上面得到是变量名称是dao,为什么你下面调用的时候使用userLoginDao这个变量,你说
ApplicationContext ac = new ClassPathXmlApplicationContext("../applicationContext.xml");
IUserLoginDao dao = (IUserLoginDao)ac.getBean("userLoginDao");
这样是可以获取dao的,那你觉得是哪里出了问题?我觉得,你这个userLoginDao.findbyNameAndPwd调用,来的莫名其妙,不知道是否是你有别的意思、
<bean id="userLoginDao" class="com.lwy.dao.UserLoginDao">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
应改为:
<bean id="userLoginDao" class="com.lwy.dao.IUserLoginDao">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
IUserLoginDao dao = (IUserLoginDao)ac.getBean("userLoginDao");
这个只是用来验证ApplicationContext.xml中的<bean id=userLoginDao>是否正确,这段代码本身是不存在的。
而这段代码Userlogin userLogin=userLoginDao.findbyNameAndPwd(userLoginForm.getString("loginName"), userLoginForm.getString("pwd"));才是真正通过注入来获取dao的
<bean id="userLoginDao" class="com.lwy.dao.UserLoginDao">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
应改为:
<bean id="userLoginDao" class="com.lwy.dao.IUserLoginDao">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
就是这里出错了啊,你没改,试试啊
<bean id="userLoginDao" class="com.lwy.dao.UserLoginDao">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
这是biz
<bean id="IDao" class="com.lwy.dao.IUserLoginDao">
<property name="userLoginDao" ref="userLoginDao" />
</bean>
然后你在配你的Action啊
你说的我也试了,直接叉叉No setter found for property 'userLoginDao' in class 'com.lwy.dao.IUserLoginDao'
@Repository("utilsDao")
public class UtilsDaoImpl extends HibernateDaoSupport implements IUtilsDao { @Resource(name="sessionFactory")
public void setSuperSessionFactory(SessionFactory sessionFactory) {
super.setSessionFactory(sessionFactory);
}
这要看看你的struts-config.xml
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd"><struts-config>
<data-sources />
<form-beans>
<form-bean name="userLoginForm" type="org.apache.struts.action.DynaActionForm">
<form-property name="id" type="java.lang.Integer" />
<form-property name="mail" type="java.lang.String" />
<form-property name="pwd" type="java.lang.String" />
<form-property name="type" type="java.lang.String" />
<form-property name="pwd1" type="java.lang.String" />
<form-property name="loginName" type="java.lang.String" />
</form-bean>
<form-bean name="StuUserForm" type="org.apache.struts.action.DynaActionForm">
<form-property name="id" type="java.lang.Integer" />
<form-property name="stuNo" type="java.lang.String" />
<form-property name="tel" type="java.lang.String" />
<form-property name="homeAddr" type="java.lang.String" />
<form-property name="birthday" type="java.lang.String" />
<form-property name="specialtyId" type="java.lang.Integer" />
<form-property name="addr" type="java.lang.String" />
<form-property name="stuName" type="java.lang.String" />
<form-property name="stuSex" type="java.lang.String" />
</form-bean>
<form-bean name="specialtyForm" type="org.apache.struts.action.DynaActionForm">
<form-property name="id" type="java.lang.Integer" />
<form-property name="name" type="java.lang.String" />
<form-property name="isFinish" type="java.lang.Boolean" />
<form-property name="enterYear" type="java.lang.String" />
<form-property name="langthYear" type="java.lang.String" />
</form-bean>
<form-bean name="courseForm" type="org.apache.struts.action.DynaActionForm">
<form-property name="id" type="java.lang.Integer" />
<form-property name="name" type="java.lang.String" />
<form-property name="isFinish" type="java.lang.Boolean" />
<form-property name="courseInfo" type="java.lang.String" />
<form-property name="specialtyId" type="java.lang.Integer" />
<form-property name="teacherInfo" type="java.lang.String" />
<form-property name="addr" type="java.lang.String" />
<form-property name="credit" type="java.lang.Short" />
<form-property name="teacherName" type="java.lang.String" />
<form-property name="schooltime" type="java.lang.String" />
</form-bean>
</form-beans>
<global-exceptions />
<global-forwards />
<action-mappings>
<action
attribute="userLoginForm"
name="userLoginForm"
path="/reg"
scope="request"
type="org.springframework.web.struts.DelegatingActionProxy">
<set-property property="cancellable" value="true" />
<forward name="addStuInfo" path="/stu/addStuInfo.jsp" />
<forward name="reg" path="/reg.jsp" />
</action>
<action
attribute="StuUserForm"
name="StuUserForm"
parameter="method"
path="/stuUser"
scope="request"
type="org.springframework.web.struts.DelegatingActionProxy">
<set-property property="cancellable" value="true" />
<forward name="course" path="/stu/courseInfo.jsp" />
<forward name="selected" path="/stu/selected.jsp" />
<forward name="select" path="/stu/select.jsp" />
<forward name="welcome" path="/stu/welcome.jsp" />
<forward name="exit" path="index.jsp" />
</action>
<action
attribute="userLoginForm"
name="userLoginForm"
path="/userLogin"
scope="request"
type="com.lwy.action.UserLoginAction">
<set-property property="cancellable" value="true" />
<forward name="manager " path="/manager/manager.jsp" />
<forward name="index" path="/index.jsp" />
<forward name="addStuInfo" path="/stu/addStuInfo.jsp" />
<forward name="welcome" path="/stu/welcome" />
</action>
<action
attribute="specialtyForm"
name="specialtyForm"
path="/specialty"
scope="request"
type="com.lwy.action.SpecialtyAction">
<set-property property="cancellable" value="true" />
<forward name="showSpecialty" path="/manager/showSpecialty.jsp" />
</action>
<action
attribute="courseForm"
name="courseForm"
path="/course"
scope="request"
type="com.lwy.action.CourseAction">
<set-property property="cancellable" value="true" />
<forward name="addCourse " path="/manager/addCourse.jsp" />
<forward name="courseInfo" path="/manager/courseInfo.jsp" />
<forward name="shouCourse" path="/manager/showCourse.jsp" />
</action>
<action
attribute="courseForm"
name="courseForm"
path="/statInfo"
scope="request"
type="com.lwy.action.StatInfoAction">
<set-property property="cancellable" value="true" />
<forward name="showStat" path="/manager/showStat.jsp" />
<forward name="stuList" path="/manager/stuList.jsp" />
</action>
<action
attribute="userLoginForm"
name="userLoginForm"
path="/updatePwd"
scope="request"
type="com.lwy.action.UpdatePwdAction">
<set-property property="cancellable" value="true" />
<forward name="updatePwd" path="/updatePwd.jsp" />
<forward name="stuUpdatePwd" path="/stu/stuUpdatePwd.jsp" />
</action> </action-mappings>
<message-resources parameter="ApplicationResources" />
<!-- 加载Spring插件 -->
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml" />
</plug-in>
</struts-config>
现在的情况是,别的action,如RegAction可以调用UserLoginDao,但是UserLoginAction调用UserLoginDao时UserLoginDao=null;