action中service提供了get set方法 在set方法里面打印service是可以打印出来service和this.service都可以打印出来applicationContext.xml部分配置<!-- 准备hibernateTemplate -->
<bean id="hibernateTemplate"
class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>
<bean id="userDao" class="com.dao.impl.UserDaoImpl">
<property name="hibernateTemplate">
<ref bean="hibernateTemplate"/>
</property>
</bean>
<bean id="userService" class="com.service.impl.UserServiceImpl">
<property name="userDao">
<ref bean="userDao"/>
</property>
</bean>
<bean id="loginregAction" class="com.action.LoginRegAction">
<property name="userService">
<ref bean="userService"/>
</property>
</bean>struts2.xml配置<package name="default" namespace="/" extends="struts-default">
<action name="loginregAction" class="com.action.LoginRegAction">
<!--<result type="chain" name="success">userWeiboList</result>
-->
<result name="success">/home.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
public class LoginRegAction extends ActionSupport {
private UserServiceIf userService;
private String name;
private String password;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public UserServiceIf getUserService() {
return userService;
}
public void setUserService(UserServiceIf userService) {
System.out.println("set userService---"+userService);//可以打印出来(在tomcat启动的时候会打印)
this.userService = userService;
System.out.println("set this.userService---"+this.userService);//可以打印出来在tomcat启动的时候会打印)
}
@Override
public String execute() throws Exception {
try {
System.out.println("userService------->"+userService);//这里一直是null 求解
//userService=new UserServiceImpl();
//System.out.println("new userService ====>"+userService);
User user=userService.login(name, password);
Map session =ActionContext.getContext().getSession();
session.put("user",user);
return SUCCESS;
} catch (Exception e) {
e.printStackTrace();
return ERROR;
}
}
}
我用junit4测试了下service,测试是可以通过
ApplicationContext ac = new FileSystemXmlApplicationContext("/WebRoot/WEB-INF/applicationContext.xml");
private UserServiceIf userService=(UserServiceIf) ac.getBean("userService");
@Test
public void testLogin(){
System.out.println(userService);
User user=userService.login("a", "b");
System.out.println(user.getSex());//可以打印user的sex
}
<bean id="hibernateTemplate"
class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>
<bean id="userDao" class="com.dao.impl.UserDaoImpl">
<property name="hibernateTemplate">
<ref bean="hibernateTemplate"/>
</property>
</bean>
<bean id="userService" class="com.service.impl.UserServiceImpl">
<property name="userDao">
<ref bean="userDao"/>
</property>
</bean>
<bean id="loginregAction" class="com.action.LoginRegAction">
<property name="userService">
<ref bean="userService"/>
</property>
</bean>struts2.xml配置<package name="default" namespace="/" extends="struts-default">
<action name="loginregAction" class="com.action.LoginRegAction">
<!--<result type="chain" name="success">userWeiboList</result>
-->
<result name="success">/home.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
public class LoginRegAction extends ActionSupport {
private UserServiceIf userService;
private String name;
private String password;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public UserServiceIf getUserService() {
return userService;
}
public void setUserService(UserServiceIf userService) {
System.out.println("set userService---"+userService);//可以打印出来(在tomcat启动的时候会打印)
this.userService = userService;
System.out.println("set this.userService---"+this.userService);//可以打印出来在tomcat启动的时候会打印)
}
@Override
public String execute() throws Exception {
try {
System.out.println("userService------->"+userService);//这里一直是null 求解
//userService=new UserServiceImpl();
//System.out.println("new userService ====>"+userService);
User user=userService.login(name, password);
Map session =ActionContext.getContext().getSession();
session.put("user",user);
return SUCCESS;
} catch (Exception e) {
e.printStackTrace();
return ERROR;
}
}
}
我用junit4测试了下service,测试是可以通过
ApplicationContext ac = new FileSystemXmlApplicationContext("/WebRoot/WEB-INF/applicationContext.xml");
private UserServiceIf userService=(UserServiceIf) ac.getBean("userService");
@Test
public void testLogin(){
System.out.println(userService);
User user=userService.login("a", "b");
System.out.println(user.getSex());//可以打印user的sex
}
解决方案 »
- 谁有马士兵 elearning(远程教学项目)
- 为什么我的jsp页面一放入css和js就慢的要命
- HttpServlet.session实例的区别
- 如何把框架应用程序换成基于网页打开的applet应用小程序
- 同一个用户Id只能在线五个用户
- 我在a机器的weblogic上配置了一个ejb,在b机器的weblogic上的servlet中能不能调用a的ejb啊
- JAVA高手看过来!!!
- 关于Package 的错误
- jdbc连接oracle出现的错误和异常,希望大家帮帮忙!
- InputStream read 阻塞
- 数据库有数据resultset.next()却是false,急急啊
- Weblogic求教,大神们快来
<property name="userService">
<ref bean="userService"/>
</property>
</bean>
struts的action要交给spring管理在调用action的时候才会通过spring注入
<package name="default" namespace="/" extends="struts-default">
<action name="loginregAction" class="LoginRegAction">
<!--<result type="chain" name="success">userWeiboList</result>
-->
<result name="success">/home.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
class="com.action.LoginRegAction" 这个地方填入的是被拖管对象
而不是填入类这样 action才被spring管理
<bean id="LoginRegAction" class="com.action.LoginRegAction">
<property name="loginService" ref="LoginService" />
</bean>
看你是新手跟定很着急 好好看教程 跟着做别找个帖子或者 找了例子就上手
class="com.action.LoginRegAction" 这个地方填入的是被拖管对象
而不是填入类这样 action才被spring管理
<bean id="LoginRegAction" class="com.action.LoginRegAction">
<property name="loginService" ref="LoginService" />
</bean>我那样填写整个类名也是可以的,我知道是填写那个对象也就是bean里面那个id