我正在尝试使用SpringMVC中的@Value注释填充一个属性,但它没有被填充。
我正在尝试使用Struts2JSP属性访问该属性。我的用例是这样的:
public class TransferCreditsAction extends StudentAwareAction {
protected Log logger = LogFactory.getLog(this.getClass());
@Value( "${transfer.credit.url}" )
private String transferCreditUrl;
public void setStates( List<TranslatedValue> states ) {
this.states = states;
}
@Value( "${transfer.credit.url}" )
public String getTransferCreditUrl() {
return transferCreditUrl;
}
}
我的属性文件看起来像:
transfer.credit.url
我使用JSP访问这个属性,它看起来像:
<s:property value='transferCreditUrl'/>"
我知道我的JSP可以访问这个字段,因为当我将这个字段设置为默认值时,我对它进行了测试。
但是,该字段不是从我的属性文件中填充的。我正在使用Spring 4.1.6
非常感谢您的帮助。
Spring只能在自己的托管springbean中注入值。这意味着您的TransferCreditsAction
应该是一个springbean。
有多种方法可以将TransferCreditsAction
类声明为springbean,其他地方已经回答过了。
您还没有在TransferCreditsAction
类的顶部添加什么。将在Bean环境中注入值。
有很多方法
假设我的属性文件包含
username=Ashish
app.name=Hello
1.
@Service
@PropertySource(value = { "classpath:sample.properties" })
public class PaloAltoSbiClientImpl implements PaloAltoSbiClient {
public static String username;
@Value("${username}")
public void setUrl(String data) {
username = data;
}
...
2.
@Service
public class PaloAltoSbiClientImpl implements PaloAltoSbiClient {
@Value("${username}")
public static String username;
...
3.
@Component
public class TokenHelper {
@Value("${app.name}")
private String APP_NAME;
只需在要获取的类的顶部提供属性文件引用。
@PropertySource(value = { "classpath:sample.properties" })
发生此问题是因为我的applicationContext中缺少<context:annotation-config/>
。一旦我添加了它,它就可以毫无问题地开始工作了。