用SSH加做的一个登录 我就是想问一下比如:<bean name="/login" class="com.yourcompany.struts.action.LoginAction">
<property name="service">
<ref bean="service"/>
</property> <property name="service"> 这个service是哪里的?
<ref bean="service"/> 这个service是哪里的?其实也不要看我的这个代码
也可以说一般 这样的位置是和什么样的类匹配啊??
<property name="service">
<ref bean="service"/>
</property> <property name="service"> 这个service是哪里的?
<ref bean="service"/> 这个service是哪里的?其实也不要看我的这个代码
也可以说一般 这样的位置是和什么样的类匹配啊??
<ref bean="service"/> 这个不太理解
<ref bean="service"/> 这个service是哪里的? 这个是 "/login"的service的属性引用了一个叫service的bean我是这么理解的,刚学,
看楼主的代码就是用到了SSH了是吧!~呵呵!~!
那我就来帮你回答一下这个问题吧!@~!
<bean name="/login" class="com.yourcompany.struts.action.LoginAction">
<property name="service">
<ref bean="service"/>
</property>
</bean> <property name="service">中的service是你在Action中要注入的属性名称,你这样写就是意味着你的LoginAction中
有一个私有的 service成员变量,我不知道你有没有用接口!~但是只要记住,<property name="service">是你的LoginAction中的成员变量,而且有setService()的属性;<ref bean="service"/>这个service就是你自己在spring的配置文件中配置的,就以你上边的代码为例子吧
<bean id="service" class="com.yourcompany.service.Service">
<property name="adminDao">
<ref bean="AdminDAO"/>
</property>
</bean> 这个bean就是在你的业务类里边注入的DAO,也就是说你在你的业务类Service里面注入了数据访问类AdminDAO,
同理,你在Action中也注入了业务类,<ref bean="service"/>这个标签就是取得你在spring中配置的业务类的对象;
说得通俗一点:<property name="service">就是你的Action中的service属性,<ref bean="service"/>就是你的spring配置文件中的业务类的<bean>的id或者是name(id和name是不可共存的,但是殊途同归)不知楼主有没有一点理解了呢,呵呵
<ref bean="service--》是你自己定义的BEAN的ID或者名称"/>
<ref bean="service"/> --这个是就是下面代码中的service
<bean id="service" class="com.yourcompany.service.Service">
<property name="adminDao">
<ref bean="AdminDAO"/>
</property>
<property name="adminDao">
<ref bean="AdminDAO"/>
</property>
</bean>看到了没有以上就是你倒数的第二个bean下面是倒数第一个bean
<bean name="/login" class="com.yourcompany.struts.action.LoginAction">
<property name="service">
<ref bean="service"/>
</property>
</bean> 就是说倒数的第一个bean中的Loginaction要用到上面倒数第二个bean的内容,你再仔细分析下LoginAction与Service中的代码你就会发现问题了.
我明白了
我在LoginAction中定义了一个private Service service
还有依赖注入的 set方法 :
public void setService(Service service) {
this.service = service;
}但是我修改了一下:private Service service1 <bean name="/login" class="com.yourcompany.struts.action.LoginAction">
<property name="service"> //这个service要变成service1吗?
<ref bean="service"/>
</property>
<property name="service">
是哪里的,对应下面的红色字的地方<bean id="service" class="com.yourcompany.service.Service">
<property name="adminDao">
<ref bean="AdminDAO"/>
</property>
</bean>
对应
package com.yourcompany.service;import com.yourcompany.dao.AdminDAO;
import com.yourcompany.vo.Admin;public class Service {
private AdminDAO adminDao; public AdminDAO getAdminDAO() {
return adminDao;
} public void setAdminDAO(AdminDAO adminDao) {
this.adminDao = adminDao;
} public Admin regist(Admin admin) {
adminDAO.save(admin);
return admin;
}
}
<bean name="/login" class="com.yourcompany.struts.action.LoginAction">
<property name="service">
<ref bean="service"/>
</property>
</bean>
对应
package com.yourcompany.struts.action;
public class LoginAction extends Action {
private Service service; public Service getService() {
return service;
} public void setService(Service service) {
this.service = service;
}
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
AdminForm adminForm = (AdminForm) form;
Admin admin=new Admin();
admin.setUsername(userForm.getUsername());
admin.setPassword(userForm.getPassword());
if(service.regist(admin) != null){
return mapping.findForward("success");}
else
{return mapping.findForward("success");}}
}
}