Get ServletConfig in the controller of spring MVC

Source: Internet
Author: User

When you use Smartupload for file uploads, you need to use Srevletconfig:


Writing in a servlet is, of course, easy to implement:

        Private ServletConfig config;//initialization servletfinal public void init (ServletConfig config) throws servletexception{ This.config=config;}

The Init method gets to ServletConfig when the servlet is initialized.

But how do you get it in the controller? Through the small series of advice, you can inherit servletconfigaware,servletcontextaware these two interfaces to achieve:

Package Module.system.controller;import Java.io.ioexception;import Javax.servlet.servletconfig;import Javax.servlet.servletcontext;import Javax.servlet.servletexception;import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;import Module.system.common.fileload;import Org.springframework.stereotype.controller;import Org.springframework.web.bind.annotation.requestmapping;import Org.springframework.web.bind.annotation.requestmethod;import Org.springframework.web.bind.annotation.responsebody;import Org.springframework.web.context.ServletConfigAware; Import org.springframework.web.context.servletcontextaware;/** * File upload download. * */@Controller @requestmapping ("/fileload") public class Fileloadcontroller implements Servletconfigaware, Servletcontextaware{private ServletContext ServletContext, @Overridepublic void Setservletcontext (ServletContext    arg0) {this.servletcontext = arg0;} Private ServletConfig ServletConfig; @Overridepublic void Setservletconfig (ServletConfig arg0) {this.servletconfig = arg0;} @RequestMapping (value = "/upload.do", method = Requestmethod.post) @ResponseBody//This note indicates that the return value skips the View Processing section and writes directly to the HTTP response Body public String Upload (httpservletrequest request,httpservletresponse response) {Fileload fileload = new Fileload (); try {fileload.upload (request, Response,servletconfig),} catch (Servletexception e) {e.printstacktrace ();} catch ( IOException e) {e.printstacktrace ();} Return "";}}

Inside this upload method is another written method that will be introduced in the next article. By doing so, we can get servletconfig.



Get ServletConfig in the controller of spring MVC

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.