日期:2014-05-17  浏览次数:20653 次

spring怎么使用IOC的注入到Struts2的Action里?谢谢!
请看我的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注入,但我不知道怎么写才能运行正常,现在总是报错,请大家帮帮忙,谢谢!

------解决方案--------------------
用注解么?
在action类上加@Controller

userService类上加 @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";
    }