我在mysql數據庫中有一個BLOB類型的圖像,我想在jsp上顯示圖像。我使用Hibernate和Spring MVC。這是我的模型類:在jsp上用spring mvc和hibernate顯示圖像
@Repository
@Entity
@Table(name = "foto")
public class Image {
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "fk_id_users", nullable = false)
private Users user;
@Id
@Column(name = "id_foto")
@GeneratedValue(strategy = GenerationType.AUTO)
private int id_foto;
@Column(name = "tipo")
private String tipo;
@Column(name = "size")
private String size;
@Column(name = "nome")
private String nome;
@Column(name = "image")
private byte[] image;
//Getters and Setters
這是我的控制器:
@Controller
@SessionAttributes("UserSession")
public class LoginController {
@Autowired
private UsersService usersService;
@RequestMapping(value = "loginUsers", method = RequestMethod.POST)
public ModelAndView loginUsers(HttpServletRequest request,@RequestParam("username") String username,
@RequestParam("password") String password) {
Users user = usersService.loginUsers(username, password);
if(user == null) {
ModelAndView MV = new ModelAndView("login");
MV.addObject("erroreLogin", "username e/o password errati");
return MV;
} else if (user.getAmministratore() == false){
request.getSession().setAttribute("UserSession",user);
ModelAndView mav = new ModelAndView("homeUtente");
mav.addObject("galleria", usersService.getAllFoto());
return mav;
} else {
request.getSession().setAttribute("UserSession",user);
ModelAndView mav = new ModelAndView("utenti");
mav.addObject("lista", usersService.getAllUtenti());
return mav;
}
}
@RequestMapping(value = "logout", method = RequestMethod.GET)
public ModelAndView logout(HttpServletRequest request) {
request.getSession().invalidate(); //invalido i dati presenti in sessione
return new ModelAndView("login");
}
}
,並在我的JSP我用這個顯示從圖像列表我的形象,因爲每個用戶有一個畫廊,以顯示:
<img alt="Kangoo_image" src="data:image/jpeg;base64,${galleria.image}" />
當我試圖在我的jsp顯示它。它顯示的東西二進制像[B @ 59e73b47。我如何在jsp中顯示圖像?