Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8710001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:30:43+00:00 2026-06-13T04:30:43+00:00

Can the same method in a controller be used for both JSP and other

  • 0

Can the same method in a controller be used for both JSP and other MIME types (like XML and JSON)?

I know of the following ways to resolve views in Spring MVC.

  1. Return a String with the view name and add attributes to the Model or ModelMap
  2. Return a ModelAndView with the view name and model
  3. Return an Object with a @ResponseBody annotation

I use 1 or 2 when I am dealing with JSP and 3 when I want to return JSON or XML.

I know I can use two methods and with the @RequestMapping(headers="accept=application/xml") or @produces annotations to define which MIME types they handle, but is it possible to do this in just one method?

The controller logic is pretty simple and it seems like unnecessary duplication to have two different methods mapped which return the same exact model, or is this just simply the way it’s done?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T04:30:44+00:00Added an answer on June 13, 2026 at 4:30 am

    Yes, this is straight forward in Spring MVC 3.x…

    You basically write your controller methods for just normal JSP page views and then you configure a ContentNegotiatingViewResolver bean in your Dispatcher servlet config., which looks at the requested mime-type (or file extension) and returns the appropriate output type.

    Follow the instructions here: Spring 3 MVC ContentNegotiatingViewResolver Example

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know I can create a generic method that I can call from other
In java can an instance variable and a method have the same name without
How can I easily separate JSON values that are sent in the same request?
I have a scenario where combobox can have same string values. for exa combo
I use this code to update data in database table. Can reuse same code
Why do we need the html5 canvas element, when the same can be achieved
Bellow is the code for the simplest GUI countdown. Can the same be done
Can we have same delegate for two events ? which have same number of
Where multiple people can work on same css. is it possible to follow semantic
Are there WinAPI functions that do the same what can do standard Windows utility

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.