dsplynm
dsplynm

Reputation: 613

Pass Servlet Context Parameter to a Spring MVC Controller

I have a context parameter defined in tomcat server config xml for a given webapp. I want to use this value in a spring mvc controller.

How do I achieve this? How do I make the context param visible to the spring controller?

Upvotes: 0

Views: 3133

Answers (3)

bomb zj
bomb zj

Reputation: 91

as of 3.0:

@Value("#{contextParameters.param-name}")
private String paramName;

Upvotes: 0

slslprovider
slslprovider

Reputation: 16

You can also use HttpServletRequest parameter in a Controller method.

 public String getContextValue(HttpServletRequest httprequest) {

        HttpSession htsession = httprequest.getSession();
          ServletContext servContext = htsession.getServletContext();
            String paramValue = (String)servContext.getInitParameter("paramName");
            return  paramValue;
        }

Upvotes: 0

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 280174

Inject the ServletContext in your @Controller.

@Autowired
private ServletContext context; 

and use it to retrieve the context parameter

context.getInitParameter("param-name")

Upvotes: 2

Related Questions