Try to search your question here, if you can't find : Ask Any Question Now ?

Is there a way to display an image from MySql database in thymeleaf and spring boot?

HomeCategory: stackoverflowIs there a way to display an image from MySql database in thymeleaf and spring boot?
Avatarrupesh asked 4 months ago

I created a MySQL database named: department. I can display all other columns from the database except the ‘logo’ which is in BLOB format. I can save image into ‘logo’ but I cannot display it in a thymeleaf table. Do I need a separate Controller for displaying image?

Here is my Thymeleaf to display image:

<td>
<img th:src="${tempDepartment.logo}" >
</td>

This is my entity:

@Id
@Column(name="id")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;

@Column(name="dept_name")
private String deptName;

@Lob    
@Column(name="logo")
private byte[] logo;

This is my Controller:

//lists all departments
    @GetMapping("/departments")
    public String listDepartments(Model model) {
        List<Department> departments = departmentService.findAll();
        model.addAttribute("departments",departments);
        return "/departments/list"; // Your current thymeleaf template
    }

    //adding a new department
    @GetMapping("/showFormForAdd")
    public String showFormForAdd(Model theModel) {

        Department theDepartment=new Department();
        theModel.addAttribute("department",theDepartment);

        return "/departments/department-form";

    }

    //saving a department
    @PostMapping("/save")
        public String saveDepartment(@ModelAttribute("department") 
           Department theDepartment) {
            departmentService.save(theDepartment);
            return "redirect:/home/departments";
        }

I wish to display an image from the database but it is not showing.

1 Answers
Best Answer
AvatarAmit answered 4 months ago
Your Answer

13 + 17 =

Popular Tags

WP Facebook Auto Publish Powered By : XYZScripts.com