Thymeleaf img没有调用spring controler

时间:2016-01-01 18:44:43

标签: spring spring-boot thymeleaf

这是我的gradle依赖项:

dependencies {
    compile("org.springframework.boot:spring-boot-starter-thymeleaf")
    compile("org.springframework.boot:spring-boot-starter-security")
    compile("org.springframework:spring-jdbc:4.1.0.RELEASE")
    compile("org.springframework.boot:spring-boot-starter-data-jpa")
    compile("mysql:mysql-connector-java:5.1.+")
    compile("org.webjars:bootstrap:3.0.3")
    compile("org.webjars:jquery:2.0.3-1")
    compile("org.springframework.security.oauth:spring-security-oauth2:2.0.7.RELEASE")  
    compile("org.springframework.security:spring-security-test:4.0.0.RELEASE")
    compile("org.thymeleaf:thymeleaf-spring4")
    compile("nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect")
    compile("org.springframework.boot:spring-boot-starter-web")
    compile("com.google.code.gson:gson:2.2.4")
    compile("javax.mail:mail:1.4.5")
    compile("org.springframework:spring-context-support:3.2.2.RELEASE")
    compile("org.apache.commons:commons-io:1.3.2")
    providedRuntime("org.springframework.boot:spring-boot-starter-tomcat")

    testCompile("junit:junit")
}

我在控制器中定义了一个方法:

@ResponseBody
    @RequestMapping(value = "/mainMenu/companyOfficeMainMenu/imagetest/{server_image_id}", method = RequestMethod.GET, produces = MediaType.IMAGE_JPEG_VALUE)
    public byte[] testphoto(@PathVariable("server_image_id") Long server_image_id) throws IOException {



        Image notUpdatedImage = mobileManagment.getImage(server_image_id);

        String locationRoot =  env.getProperty("location.sever.root");
        String locationOfCompanyData =  env.getProperty("location.compay.data");
        String locationOfImage =  env.getProperty("location.compay.media.images");


        String storeImageName = 
                locationOfCompanyData 
                + File.separator
                + locationOfImage 
                + File.separator
                + notUpdatedImage.getCompany_id() 
                + File.separator +
                + notUpdatedImage.getServer_questionnaire_attempt_key()
                + File.separator 
                + notUpdatedImage.getImage_name();      
        File uploadedFile = new File(locationRoot, storeImageName);             
        InputStream inputStream = new FileInputStream(uploadedFile.getAbsolutePath());


        return IOUtils.toByteArray(inputStream);
    }

在Thymeleaf,我试着称之为:

<img th:src="@{/mainMenu/companyOfficeMainMenu/imagetest/${object.getServer_image_id()}}" />

我在控制器的方法中有一个断点,但它没有被调用。

这是另一种不被调用的方法:

@RequestMapping(value = "/mainMenu/companyOfficeMainMenu/image/{server_image_id}", method = RequestMethod.GET)
    public ResponseEntity<byte[]> getImage(@PathVariable("server_image_id") Long server_image_id) throws IOException {

        Image notUpdatedImage = mobileManagment.getImage(server_image_id);

        String locationRoot =  env.getProperty("location.sever.root");
        String locationOfCompanyData =  env.getProperty("location.compay.data");
        String locationOfImage =  env.getProperty("location.compay.media.images");


        String storeImageName = 
                locationOfCompanyData 
                + File.separator
                + locationOfImage 
                + File.separator
                + notUpdatedImage.getCompany_id() 
                + File.separator +
                + notUpdatedImage.getServer_questionnaire_attempt_key()
                + File.separator 
                + notUpdatedImage.getImage_name();      
        File uploadedFile = new File(locationRoot, storeImageName);             
        InputStream inputStream = new FileInputStream(uploadedFile.getAbsolutePath());


        byte[] imageContent = IOUtils.toByteArray(inputStream);
        final HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.IMAGE_PNG);
        return new ResponseEntity<byte[]>(imageContent, headers, HttpStatus.OK);
    }


    @ResponseBody
    @RequestMapping(value = "/mainMenu/companyOfficeMainMenu/imagetest/{server_image_id}", method = RequestMethod.GET, produces = MediaType.IMAGE_JPEG_VALUE)
    public byte[] testphoto(@PathVariable("server_image_id") Long server_image_id) throws IOException {



        Image notUpdatedImage = mobileManagment.getImage(server_image_id);

        String locationRoot =  env.getProperty("location.sever.root");
        String locationOfCompanyData =  env.getProperty("location.compay.data");
        String locationOfImage =  env.getProperty("location.compay.media.images");


        String storeImageName = 
                locationOfCompanyData 
                + File.separator
                + locationOfImage 
                + File.separator
                + notUpdatedImage.getCompany_id() 
                + File.separator +
                + notUpdatedImage.getServer_questionnaire_attempt_key()
                + File.separator 
                + notUpdatedImage.getImage_name();      
        File uploadedFile = new File(locationRoot, storeImageName);             
        InputStream inputStream = new FileInputStream(uploadedFile.getAbsolutePath());


        return IOUtils.toByteArray(inputStream);
    }

当我在屏幕上按f12时,我可以看到图像标签的输出:

<img src="/mainMenu/companyOfficeMainMenu/image/${object.getServer_image_id()}">

看起来不对。

当我输出时我得到27(这是正确的)

<p th:text="${object.getServer_image_id()}"></p>

1 个答案:

答案 0 :(得分:2)

您没有正确使用Thymeleaf Link URL语法。您需要在大括号中指定一个参数(类似于@RequestMapping),例如。 {server_image_id}。然后指定要在括号(server_image_id=${object.getServer_image_id()})之间替换它的值。

所有在一起:

<img th:src="@{/mainMenu/companyOfficeMainMenu/imagetest/{server_image_id}(server_image_id=${object.getServer_image_id()})}" />