java - How to add a picture in html page in Spring application -


I got the spring application and I JBoss 7.1.1 my To run the server

When I click submit, it goes to my controller which allocates the method to another Java class. This method creates a picture:

  ... private string filePath = "./qrcode.png"; ... FileOutputStream fout = New FileOutputStream (filePath); ...   

Finally, the picture has been saved to the directory:

  jboss-as-7.1.1.Finnil \ bin   

Now, I want to show this image in my HTML page. In the controller I said:

  model.addAttribute ("qrimage", "/qrcodes/qrcode.png");   

I have found (I use thymeleaf ):

    

And when I look at my page, see: ? Background_fr ?? Instead of my picture when I use my Java class:

  Private string filepath = "./../standalone/deployments/myproject-web.war/qrcode Png ";   

instead of

  personal string filePath = "./qrcode.png";   

Everything works.

I have found in my mvc-servlet.xml :

  & lt; MVC: location of resources = "/ qrcodes" mapping = "/ qrcodes / **" />   

I would like to avoid ./../ standalone / deployment / hardcode path like my project-web.war / qrcode in my code. Png

Can anyone help me?


An Edit Controller:

  Package com.my.package.controller; Import java.io.IOException; Import org.springframework.beans.BeansException; Import org.springframework.context.ApplicationContext; Import org.springframework.context.ApplicationContextAware; Import org.springframework.stereotype.Controller; Import org.springframework.ui.model; Import org.springframework.ui.ModelMap; Import org.springframework.validation.BindingResult; Import organization Source: Import org.springframework.web.bind.annotation.RequestMapping; Import org.springframework.web.bind.annotation.RequestMethod; Import com.my.package.smsgenerator.QrCodeGenerator; @Controller Public Class QrCodeGeneratorController Applies AbstractController ApplicationContextAware {ApplicationContext applicationContext = null; @RequestMapping (method = RequestMethod.GET, value = "/ qrcode") public string getPage (model m, @model attribute ("membership") last QrCodeGenerator subscription) {return "qrcode"; } @RequestMapping (value = "/ subscribeth", params = {"save"}) public string save (final QrCodeGenerator subscription, last binding binding result, final modelmap model) throws IOException {subscription.buildQRCCode (); Model.addAttribute ("qrimage", applicationContext.getResource ("/ qrcodes / qrcode.png"). GetFile (). GetAbsolutePath ()); Return "forward: / qrcode"; } @ Override Public Wide Setup Coupons (AppContax CTX) throws a Beans expression {this.applicationContext = ctx; }}    

You load it with the resource class Through context:

  resource template = ctx.getResource ("some / resource / path / myTemplate.png");   

In the resource class, there are methods like getURL () , getFile () , etc., which you can use to get your path Picture for can

More about Spring Resources:

Comments

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -