spring struts2 整合时,采用自动注解,找不到action问题解决办法

在搭传统ssh框架时,碰到一个令人无比头疼的问题;
当采用注解式(即零配置)配置spring bean时,发现action怎么也找不到

spring struts2 整合时,采用自动注解,找不到action问题解决办法_第1张图片

更诡异的是,通过使用InitializingBean接口,发现LoginAction确实是已经被spring加载了的,而且其他bean也都能够正确组装。

分析下后,觉得问题不在于spring没有加载bean,那是不是struts2配置的问题呢?

我struts2的配置(部分)如下:

<action name="login" class="LoginAction">
    <result name="teacher">/index.jsp</result>
    <result name="fail">/loginFailed.html</result>
</action>


当url为/login时,struts2会向spring索取名字为"LoginAction"的action

两边线索都向中间聚拢,就是在这接头点出了问题:
package com.jyzz.tlms.action;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.jyzz.tlms.logic.LoginLogic;

@Component
public class LoginAction extends CommonAction{

	/**
	 * 
	 */
	private static final long serialVersionUID = -6851791110535117944L;
	
	@Autowired
	private LoginLogic ll;
	
	public String execute() throws Exception{
		ll.doSth();
		return "teacher";
		
	}

}



这里面作为一个菜b,不明白@Component后,spring是如何命名这个类的id的,如果想当然的以为是"LoginAction"的话(受@Autowired误导),那就是这个bug的来源了


所以,在这,为了解决这个bug,只要
@Component("LoginAction")

即显式的指明这个action的id即可。



也希望有大神能对@Component不显式指明id的情况下,spring默认给的id是什么这个问题做点解答。

你可能感兴趣的:(spring,struts)