我有自定义标签。当我注入spring消息资源并想要访问属性文件时,注入的bean始终为null。 我做错了什么,
@Component
public class GridColumnsCutomTagHander extends TagSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
@Inject
private MessageSource messageSource;
private String propertyFileName;
/**
* @return the propertyFileName
*/
public String getPropertyFileName() {
return this.propertyFileName;
}
/**
* @param propertyFileName
* the propertyFileName to set
*/
public void setPropertyFileName(String propertyFileName) {
this.propertyFileName = propertyFileName;
}
@Override
public int doStartTag() throws JspException {
try {
JspWriter out = pageContext.getOut();
String gridColumnValues = messageSource.getMessage(propertyFileName,
null, EWMSUser.getCurrentLanguageLocale());
out.println(gridColumnValues );
} catch (IOException e) {
e.printStackTrace();
}
return (SKIP_BODY);
}
请帮我弄清问题在哪里
答案 0 :(得分:0)
根据我的经验,如果当前类具有servletContext环境 ,则可以将MessageSource注入成功 。因此,您需要将其注入 contronller < / strong> layer,然后将其作为构造参数传递给当前标记类。
为了做到这一点,我认为你不应该使用@Componment来初始化GridColumnsCutomTagHander类,你需要手动在你的代码中创建它.Below是我的解决方案:
public class GridColumnsCutomTagHander extends TagSupport {
private static final long serialVersionUID = 1L;
private MessageSource messageSource;
public GridColumnsCutomTagHander(){
}
public GridColumnsCutomTagHander(MessageSource messageSource){
this.messageSource=messageSource;
}
private String propertyFileName;
/**
* @return the propertyFileName
*/
public String getPropertyFileName() {
return this.propertyFileName;
}
/**
* @param propertyFileName
* the propertyFileName to set
*/
public void setPropertyFileName(String propertyFileName) {
this.propertyFileName = propertyFileName;
}
@Override
public int doStartTag() throws JspException {
try {
JspWriter out = pageContext.getOut();
String gridColumnValues = messageSource.getMessage(propertyFileName,
null, EWMSUser.getCurrentLanguageLocale());
out.println(gridColumnValues );
} catch (IOException e) {
e.printStackTrace();
}
return (SKIP_BODY);
}
@Controller
public TestController{
@Inject
private MessageSource messageSource;
@RequestMapping("test")
public void test(){
new GridColumnsCutomTagHander(messageSource);//now the messageSource is not null
}
}