SPRINGMVC Configuring a multi-View parser (velocity,jsp)

Source: Internet
Author: User

1. Custom View Resolver

Package Com.zhaochao.controller;import Java.util.hashmap;import Java.util.locale;import java.util.Map;import Java.util.set;import Org.apache.commons.logging.log;import Org.apache.commons.logging.logfactory;import Org.springframework.web.servlet.view;import org.springframework.web.servlet.viewresolver;/** * Custom visual schematic analysis ( Multi-view integration via configuration, e.g. jsp,velocity,freemarker,pdf,excel ...) * @author Zhaochao * */public class Jeedevviewresolver implements Viewresolver {private static Log logger = Logfactor    Y.getlog (Jeedevviewresolver.class); Public View resolveviewname (String viewName, locale locale) throws Exception {for (map.entry<set<string>, V           Iewresolver> Map:viewResolverMap.entrySet ()) {set<string> Suffixs = Map.getkey ();               for (String Suffix:suffixs) {System.out.println (suffix+ "= =" +viewname);                   if (viewname.endswith (suffix)) {viewresolver viewresolver = Map.getvalue ();  if (null! = Viewresolver) {                     if (logger.isdebugenabled ()) {Logger.debug ("found Viewresolver" + VIEWR                        Esolver + "' for ViewName '" + viewname+ "");                   } return Viewresolver.resolveviewname (ViewName, locale); }}}} if (defaultviewresolver! = null) {return Defaultviewresolver.resolvev       Iewname (viewname+ ". jsp", locale);    }//To allow for viewresolver chaining return null; } private map<set<string>,viewresolver> Viewresolvermap = new hashmap<set<string>,viewresolver&    gt; ();    Private Viewresolver defaultviewresolver = null;    public map<set<string>, viewresolver> Getviewresolvermap () {return viewresolvermap; } public void Setviewresolvermap (MAP&LT;SET&LT;STRING&GT;, viewresolver> viewresolvermap) {This.viewresolver    Map = Viewresolvermap; } public Viewresolver GetdefaultviewresolvER () {return defaultviewresolver; } public void Setdefaultviewresolver (Viewresolver defaultviewresolver) {this.defaultviewresolver = DefaultViewR    Esolver; }}

2.SpringMVC Configuration

<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:context= "Http://www.springframework.org/schema/context" Xmlns:mvc= "Http://www.springframework.org/schema/mvc" xmlns:p= "http://www.springframework.org/schema/p" xsi: schemalocation= "Http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/ Spring-mvc-3.0.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-3.0.xsdhttp://www.springframework.org/schema/context Http://www.springframework.org/schema/context /spring-context-3.0.xsd "default-autowire=" byname "><!--automatically scans all classes under the controller package, making it think of Spring MVC's controllers-->< Context:component-scan base-package= "Com.zhaochao.controller"/><!--avoid IE when performing Ajax, return json appears download file--><bean Id= "Mappingjacksonhttpmessageconverter" class= " Org.springframework.http.converter.json.MappingJacksonHttpMessageConverter "><pRoperty name= "Supportedmediatypes" ><list><value>text/html;charset=utf-8</value></list ></property></bean><!--Start the annotation feature of spring MVC and complete the mapping of request and annotation Pojo--><beanclass= " Org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter "><property name=" Messageconverters "><list><ref bean=" Mappingjacksonhttpmessageconverter "/><!--JSON Converter-- </list></property></bean><bean id= "Viewresolver" class= " Com.zhaochao.controller.JeeDevViewResolver "><property name=" Defaultviewresolver "ref=" Jspviewresolver "/ ><property name= "Viewresolvermap" ><map><entry><key><set><value>.vm</ Value></set></key><ref bean= "Velocityviewresolver"/></entry><entry><key> <set><value>jsp</value></set></key><ref bean= "JspViewResolver"/></entry ></map></property></bean><bean id= "BeanNameviewresolver "class=" Org.springframework.web.servlet.view.BeanNameViewResolver "/><bean id=" Jspviewresolver "class=" Org.springframework.web.servlet.view.InternalResourceViewResolver "><property name= "Viewclass" value= "Org.springframework.web.servlet.view.JstlView"/><property name= "prefix" value= "/web-inf/ jsp/"/><!--<property name=" suffix "value=". jsp "/>--></bean><!--Velocity viewresolver Configuration--><bean id= "Velocityviewresolver" class= " Org.springframework.web.servlet.view.velocity.VelocityViewResolver "><property name=" order "value=" 0 "/> <property name= "ContentType" value= "Text/html;charset=utf-8"/><property name= "Requestcontextattribute" Value= "req"/></bean><!--Velocity Configuration--><bean id= "Velocityconfig" class= " Org.springframework.web.servlet.view.velocity.VelocityConfigurer "><property name=" configlocation "value="/ Web-inf/velocity.properties "/><property name=" ResourceloAderpath "value="/web-inf/vm/"/><property name=" velocityproperties "><props><prop key=" Input.encoding ">utf-8</prop><prop key=" output.encoding ">utf-8</prop></props></ Property></bean></beans>

3.Controller Controller

Package Com.zhaochao.controller;import Java.util.list;import Javax.servlet.http.httpservletrequest;import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.beans.factory.annotation.qualifier;import Org.springframework.stereotype.controller;import Org.springframework.ui.model;import Org.springframework.ui.modelmap;import Org.springframework.web.bind.annotation.pathvariable;import Org.springframework.web.bind.annotation.requestmapping;import Com.zhaochao.bean.user.userbean;import Com.zhaochao.service.user.UserService; @Controller @requestmapping ("/useraction") public class Usercontroller { Private UserService userservice;public UserService Getuserservice () {return userservice;} @Autowiredpublic void Setuserservice (UserService userservice) {this.userservice = UserService;} @RequestMapping (value = "/def") public String def (httpservletrequest request, Modelmap map) {map.put ("name", "Hello World" "); Request.setattribute (" name "," Hello wrod! "); List<userbean> Users=userservice.getuserentities (); Map.put ("name", "Hello World"), Map.put ("Users", "Users"; return "Def";} @RequestMapping (value = "/jsp") public String test1 (httpservletrequest request, Modelmap map) {map.put ("name", "Hello World "); Request.setattribute (" name "," Hello wrod! "); List<userbean> users=userservice.getuserentities (); Map.put ("name", "Hello World"), Map.put ("Users", users); return "index.jsp";} @RequestMapping (value = "/VM") public String test3 (httpservletrequest request, Modelmap map) {list<userbean> users =userservice.getuserentities (); Map.put ("name", "Hello World"), Map.put ("Users", "Users"; return "INDEX.VM";}}

4.View Layer Directory Structure

5. Effects


Full project

http://download.csdn.net/detail/whzhaochao/8631177


SPRINGMVC Configuring a multi-View parser (velocity,jsp)

Related Article

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.