2014-10-30 16 views
0

У меня есть пользовательский тег. Когда я вставляю ресурс весеннего сообщения и хочу получить доступ к файлу свойств, инжектируемый компонент всегда имеет значение null. Что я делаю неправильно,Почему значение введенного компонента bean равно нулю

@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

Что такое MessageSource? класс или интерфейс. Можете ли вы показать конфигурацию XML для весны – mahesh

ответ

0

Основываясь на моем опыте, то MessageSource может быть введен успех если текущий класс имеет среду ServletContext . Поэтому вам нужно ввести его в свой слой contronller, а затем передать его как параметр конструкции вашему текущему классу тегов.

Для того, чтобы сделать это, я думаю, вы не должны использовать @Componment парафировать класс GridColumnsCutomTagHander, вам нужно создать его в вашем коде hand.Below мое решение для этого:

  • Код для класс GridColumnsCutomTagHander
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 
} 
} 
Смежные вопросы