A small example of spring and Freemarker

Source: Internet
Author: User

Due to the need of the project and the need to use Freemarker, today is just a small example of a simple case study.

First configure Web. XML,

<servlet> <servlet-name>springmvc</servlet-name> <servlet-class> Org.springframework.web.servlet.dispatcherservlet</servlet-class> <init-param> <param-name> Contextconfiglocation</param-name> <param-value>classpath:applicationcontext.xml</param-value   > </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.from</url-pattern> </ Servlet-mapping>

Then configure Applicationcontext.xml

  <bean id= "Freemarkerconfig"  class= " Org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer ">  <property name=" Templateloaderpath " value="/web-inf/pages/" />  <property name=" Freemarkersettings ">  <props>   <prop key=" defaultEncoding ">GBK </prop>  </props> </property> </bean> <bean id= " Freemarkerviewresolver " class=" Org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver " >  <property name= "Cache"  value= "true"  />  <property name= "prefix"  value= " />  <property name=" suffix " value=". FTL " />   <property name= "Order"  value= "1"  />  <property name= " ContentType ">   <value>text/html;charset=gbk</value>  </property> </bean> 

The

Then needs to write a simple entity class (omitted), and a controller

  @Controller @requestmapping ("/demo") public class testcontroller {  @RequestMapping ("/ Test.from ")  public string test (Model model) {  list<users> list =  new ArrayList<Users> ();   users user1 = new users ();   User1.setid (1);   user1.setusername ("ren ethereal"),   user1.setgender ("male");   user1.setsal ( 5000.0);   users user2 = new users ();   user2.setid (2);   User2.setusername ("text shortage");   user2.setgender ("male");   user2.setsal (5000.0);   users  user3 = new users ();   user3.setid (3);   user3.setusername ("Ren song");   user3.setgender ("male");   user3.setsal (5000.0);   users user4 = new  users ();   user4.setid (4);   user4.setusername ("Fall");   user4.setgender ("male");   //user4.setsAl (5000.0);   test null processing   list.add (user1);   list.add (User2);   list.add ( USER3);   list.add (user4);   model.addattribute ("username",  "slayers")   . AddAttribute ("pwd",  "123456"). AddAttribute ("userlist", list);  return  "user";  }

After the

We need to write an FTL file &NBSP;USER.FTL

 

Such a simple example completes the above FTL in the ${(XXX) for null values!} This way of handling to avoid the error

A small example of spring and Freemarker

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.