开发者

Help creating a image servlet using Spring

I am creating a JSF application and I have a bunch of images (avatars) stored in my DB as blobs. I am trying to create a servlet so I can call a url such as "/imageServlet?123 and it will call my servlet pass the 123 as the id etc etc and output the Stream.

I am very new to Spring but I did some reading and it looks like I need to use the MVC but am confused how what the class and method declarations need to look like. Do I need to use the @Controller annotation? I really just need a decent example so I can follow it and know where to put my code.

Currently my class is declared like this

public class ImageServlet extends HttpServlet {
    ...
    @Override 
    protected void doGet(HttpServlet request, HttpServletResponse respose) {
      ...
    }
}

I am assuming this is wrong but I cant find any examples of how t开发者_如何学编程o do this in Spring. A little guidance would be really appreciated. Thanks.


If you don't need to access the spring context, you can safely have a servlet like the one you showed. You will just have to use request.getParameter("id") (with a url of type /imageServlet?id=123).

If you, however, need to access spring beans, so that you can get the user corresponding to passed id, then you can have a @Controller with a method like:

@RequestMapping("/image/{id}")
@ResponseBody
public byte[] getImage(@PathVariable int id) {
     // load the image into a byte array and return it
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜