Struts2 combined with Freemarker instance

Source: Internet
Author: User

Freemarker is a template engine that does not rely on web containers and generates text output based on templates. Its working principle is as follows:

Freemarker is not a web application framework, but is suitable as a component of a web application. With freemarker, you can use a defined template to generate various desired files. It is quite quick to use in some aspects. For example, you can use freemarker to compile a code generator. This article is an example of using Freemarker in combination with the Struts2 web application framework. I will not repeat the specific theoretical knowledge here.

This example is combined with Struts2 2.3.15 and freemarker 2.3.19. The main configuration file and Code are as follows:

(1) Configure Struts2

  

          struts2AFreemarker      org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter          struts2AFreemarker      /*  

(2) Configure Freemarker

          freemarker      freemarker.ext.servlet.FreemarkerServlet                                TemplatePath          /            1          freemarker      *.ftl  

(3) Configure Struts. basically, there is no major change to the xml file, that is, when the result is returned to the page, the type must be set to freemarker. In this way, the response information can be handed over to the Freemarker component for processing.

                                           /error.jsp                                                                                    

(4) In Action, use the normal method. If you want to upload a value to the Freemarker template, you only need to bind the value to the corresponding attribute. I uploaded the variable freemarker to index. ftl. Some action Code is as follows:

SerialVersionUID = 1L String freemarker = "free" "execute method executed! ". Freemarker =

(5) The specific template file (index. ftl) is as follows:

 
            
Test Freemarker page $ {freemarker}

As written above, if you want to use the Struts2 tag, you also need to copy the struts2-core-2.3.15.jar file under the MATA-INF, put to the location you want to place. Here, I put the WEB-INF/struts-tags.tld under the project directory, and then in the web. xml file, configure the Servlet for the tag, as shown below:

      JspSupportServlet  org.apache.struts2.views.JspSupportServlet    1  

In this way, you can use the Struts2 tag in the index. ftl template.

This completes the integration of Struts2 and Freemarker, which is easy to use and suitable for modular development. As long as the front-end staff organizes the page, the background staff only need to insert the data to the specified location on the page.

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.