SPRINGMVC Configuration Multi-view Jsp+freemarker, Practice success! (Many pits on the net)

Source: Internet
Author: User

Today, I configured a SPRINGMVC multi-view, this thought is very simple, after practice found a variety of problems, on-line search a lot of information, finally chose to see the source, finally know why failed, the following introduction.

Failed configuration! Success just changed a few small places.

<?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:p="/HTTP/ www.springframework.org/schema/p "xmlns:context=" Http://www.springframework.org/schema/context "xmlns:mvc=" http ://www.springframework.org/schema/mvc " xmlns:util=" Http://www.springframework.org/schema/util "xmlns:task= "Http://www.springframework.org/schema/task" xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"      xmlns:jaxrs= "Http://cxf.apache.org/jaxrs"      xmlns:cxf= "http// Cxf.apache.org/core "xsi:schemalocation="         http:// www.springframework.org/schema/beans        http:// www.springframework.org/schema/beans/spring-beans.xsd        http:// Www.springframework.org/schema/context        http://www.springframework.org/schema/context/spring-context.xsd         http://www.springframework.org/schema/mvc        http:// www.springframework.org/schema/mvc/spring-mvc.xsd        http:// www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd         http://www.springframework.org/schema/task http:// www.springframework.org/schema/task/spring-task-3.0.xsd        http:// Www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd ">  <context:component-scan base-package= "Com.controllers,com.services" &NBSP;/&GT;&LT;MVC: Annotation-driven enable-matrix-variables= "true"  /> <!--  Set the configuration file path of the JSP  --> <bean id= "Jspviewresolver"  class= "Org.sprinGframework.web.servlet.view.InternalResourceViewResolver ">          <property name= "Viewclass"  value= "Org.springframework.web.servlet.view.JstlView"/>         <property name= "Viewnames"  value= "*.jsp"  />       <property name= "prefix"  value= "/web-inf/classes/com/jsp"/>          <property name= "suffix"  value= ". jsp"/>           <property name= "Order"  value= "1"/>        </bean>        <!--   Set Freemarker profile path  --><bean id= "Freemarkerconfiguration" class= " Org.springframework.beans.factory.config.PropertiesFactoryBean "><property name="   Value= "Classpath:freemarker.properties"/></bean><!--  Configure Freemarker template path  --><bean id= "Freemarkerconfig" class= " Org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer "><property name=" Freemarkersettings " ref=" freemarkerconfiguration "/><property name=" TemplateLoaderPath "  Value= "classpath:com/jsp"/><property name= "Freemarkervariables" ><map><entry key= " Xml_escape " value-ref=" Fmxmlescape " /></map></property></bean><bean id= "Fmxmlescape"  class= "Freemarker.template.utility.XmlEscape"  /><!--  Configure Freemarker View resolver  --><bean id= "Viewresolver" class= " Org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver "><property name=" Viewnames "  value= "*.FTL"  /><property name= "ContentType"  value= "Text/html; charset=utf-8"  /><property name= "Cache"  value= "true"  /><property name= "prefix"  Value= ""/>         <property name= "suffix"  value= ". FTL"/>         <property name= "order"  value= "0" /> <property name= "Exposerequestattributes"  value= "true"  /><property name = "Allowsessionoverride"  value= "true"  /><property name= "Exposesessionattributes"   Value= "true"  /><property name= "Exposespringmacrohelpers"  value= "true"  /></bean ></beans>

The above is the most of the configuration I found on the Internet, the problem is, with the JSP configuration as an example:

<property name= "Viewnames" value= "*.jsp"/> <property name= "suffix" value= ". jsp"/> <property Name= "Order" value= "1"/>

First: Some people say that the order attribute does not work, I see the source debug when it is useful, he will specify which configuration to create the view, (the smaller the number the higher the priority), for example: Most of your project is a JSP is very rarely a part of the FTL or other views, If there is no special requirement, the JSP priority level should be higher, so that he will directly match the JSP view, and the match will not be looking for the FTL view.

The following goes to the point, is also the problem of the place,

Viewnames: Property represents the name of your return view. The file name must be suffixed so that spring goes back to determine if it ends with a. jsp.

If you do return the file name + suffix name, but suffix: the attribute will add a suffix to your view before creating it. JSP, so spring will help you add it again. JSP, which is sure to end up with an exception to the file that cannot be found.

Part of the source code:

Public static boolean simplematch (STRING&NBSP;PATTERN,&NBSP;STRING&NBSP;STR)  {if  ( pattern == null | |  str == null)  {return false;} Int firstindex = pattern.indexof (' * ');if  (firstindex == -1)  {return  Pattern.equals (str);} if  (firstindex == 0)  {if  (Pattern.length ()  == 1)  {return true;} Int nextindex = pattern.indexof (' * ',  firstindex + 1);if  (nextIndex ==  -1)  {return str.endswith (pattern.substring (1));} String part = pattern.substring (1, nextindex); Int partindex = str.indexof ( part);while  (partindex != -1)  {if  (SimpleMatch (pattern.substring (nextindex),  Str.substring (Partindex + part.length ())))  {return true;} Partindex = str.indexof (part, partindex + 1);} Return false;} return  (Str.length ()  >= firstindex &&pattern.substring (0, firstindex). Equals ( Str.substring (0, firstindex))  &&simplematch (pattern.substring (firstIndex),  Str.substring (FirstIndex)));}


The correct configuration is

<property name= "Viewnames" value= "*.jsp"/> <property name= "suffix" value= "/>

Java:

return "/jsp.jsp";


If you return without a suffix name,

<property name= "viewnames" value= "/> <property name=" suffix "value=". jsp "/>

Java:

return "/jsp";

I do not know so people will understand, these 2 properties can not be set, Spring automatically help you find the view you want, do not have to re-implement the Viewresolver interface, there are special circumstances can implement their own logic,

Upload Project later

SPRINGMVC Configuration Multi-view Jsp+freemarker, Practice success! (Many pits on the net)

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.