2017-10-23 1 views
0

는 기본적으로 난 프로퍼티 파일사용 속성 값 thymeleaf 일 : 조건

data.enabled = true 

에 구성이와 나는 그

@Configuration 
@PropertySource(value = {"classpath:dataconfig.properties"}) 
public class DataProperties { 
    private Boolean enabled; 
} 

에 대한 POJO 클래스를 추가하고 난 enabled 속성을 확인하려면 thymeleaf를 사용하는 html 태그.

+0

무엇이 오류입니까? –

+0

나는 유효한 속성 null을 얻고있다 – boycod3

+0

컨트롤러 코드를 보여주세요. 아니면 적어도 모델을 채우는 방법. – mrkernelpanic

답변

2

먼저 구성 클래스에 @ConfigurationProperties(prefix="data")을 추가해야합니다.

@Configuration 
@PropertySource("classpath:dataconfig.properties") 
@ConfigurationProperties(prefix="data") 
public class DataProperties { 
    private Boolean enabled; 

이 당신의 변수가 직접 @Value 주석을 사용하지 않고 속성 값에 바인더 제본되어 활성화됩니다. 속성 파일에 data.enabled이 있습니다. 즉, 접두사는 data입니다. 그래서 이것을 설정해야합니다.

thymeleaf에서 빈을 직접 사용하려면 특별한 명령을 사용해야합니다. 귀하의 경우는 그 같이한다 : (see point 5 in the docs)

<li th:if="${@dataProperties.getEnabled() == true}" ><h3>Hello</h3></li> 

추가 1 :

당신이 당신의 DataProperties

@Controller 
public class IndexController { 

    @Autowired 
    private DataProperties dataProperties; 

    @GetMapping("/") 
    public String index() { 
     dataProperties.getEnabled(); 
     return "index"; 
    } 

를 autowire하기 위해이 컨트롤러와 같은 다른 봄 콩 같은 속성을 사용하려면 말할 것도없이 필드에서 autowire는 나쁜 습관입니다. 하지만 그것을 사용하거나 생성자 나 설정자에게 autowire를 사용하도록 선택했습니다.

+0

및 컨트롤러에서 동일한 속성을 어떻게 사용할 수 있습니까 ?? – boycod3

+0

@ boycod3가 내 대답을 추가했습니다. 희망 당신이 무엇을 요구 – Patrick

+0

네 그것은 작동 ..... – boycod3