Spring MVC returns different views for different request headers

Source: Internet
Author: User
Tags http request

All of the functions in the work are requested to process each page, but later, most of the ways to change to Ajax are told.


And you don't want to go back and copy the code and change it.


It then uses spring's AOP to process different views based on the HTTP request header.


First, configure the interceptor in the configuration file

<mvc:interceptors>
    	<bean class= "**.**. Viewinterceptor "/>
</mvc:interceptors>


Viewinterceptor Code:


Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;

Import Org.apache.log4j.Logger;
Import Org.springframework.web.servlet.ModelAndView;
Import Org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
Import Org.springframework.web.servlet.view.json.MappingJacksonJsonView;

public class Viewinterceptor extends handlerinterceptoradapter{
	private Logger Logger = Logger.getlogger ( Viewinterceptor.class);
	@Override public
	void Posthandle (HttpServletRequest request,
			httpservletresponse response, Object handler,
			Modelandview Modelandview) throws Exception {
		String RequestType = Request.getheader ("X-requested-with"); C12/>if (requesttype!=null) {
			Mappingjacksonjsonview view = new Mappingjacksonjsonview ();
			View.setattributesmap (Modelandview.getmodelmap ());
			Modelandview.setview (view);
		}
		Super.posthandle (Request, response, Handler, Modelandview);
	}
	
}


The controller is configured as follows:

@RequestMapping (value = "/CHOOSEOTHER.AF") public
	Modelandview Chooseother (httpservletrequest request,
			HttpServletResponse response,string categoryId) {
		map<string, object> Map = new hashmap<string, object> ();
		Map.put ("ParentID", categoryId);
		list<category> list = Categoryservice.getlistbymap (map);
		Map.put ("list", list);
		return new Modelandview ("", map);
	


This makes it possible for the same URL to return different content based on different requests.


PS: Refer to this article http://haohaoxuexi.iteye.com/blog/1750680 to learn about interceptor interceptors

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.