请看我的struts2的action代码:
public class RegisterAction { private String username;
private String password;
private User user;
private UserService userService; public String execute() {
System.out.println("username: " + username);
return "success";
}
我的这段代码是使用spring3.0和struts2.0做整合的。
username和password是JSP传递过来的数据。
我现在需要让spring帮我把user和userService对象使用IOC注入,但我不知道怎么写才能运行正常,现在总是报错,请大家帮帮忙,谢谢!
public class RegisterAction { private String username;
private String password;
private User user;
private UserService userService; public String execute() {
System.out.println("username: " + username);
return "success";
}
我的这段代码是使用spring3.0和struts2.0做整合的。
username和password是JSP传递过来的数据。
我现在需要让spring帮我把user和userService对象使用IOC注入,但我不知道怎么写才能运行正常,现在总是报错,请大家帮帮忙,谢谢!
在action类上加@ControlleruserService类上加 @Service@Controller
public class RegisterAction { private String username;
private String password;
@Autowired
private User user;
//实体类,一般不用注入,如果要注入,在这个类上加 @Component,然后 像下面一样@Autowired注入
@Resource
private UserService userService; //这个是接口,还要有个实现类,如果你不把这个类定义为接口,
就改用 @Autowired 注入 public String execute() {
System.out.println("username: " + username);
return "success";
}
<property name="userDao" ref="userDaoImpl" />
</bean><bean id="userAction" class="com.pyh.ssh.web.UserAction">
<property name="userService" ref="userServiceImpl" />
</bean>
因此你应该将Action交给Spring管理,要不就是加注释,要不就用配置!看你具体系统采用的是哪种方式……