Struts2与FreeMarker整合

struts.xml 配置

<?xml version= "1.0"  encoding= "UTF-8"  ?>   
<!DOCTYPE struts PUBLIC   
     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"   
     "http://struts.apache.org/dtds/struts-2.0.dtd" >   
  
<struts>   
  
    <constant name= "struts.enable.DynamicMethodInvocation"  value= "false"  />   
    <constant name= "struts.devMode"  value= "false"  />   
    < package  name= "default"   extends = "struts-default" >   
        <action name= "test"   class = "com.test.TestAction" >   
            <result name= "success"  type= "freemarker" >test-success.ftl </result>   
        </action>   
        <action name= "login" >   
            <result type= "freemarker" >/login.ftl </result>   
        </action>   
    </ package >   
</struts>

 

*.ftl 编写

<#assign s=JspTaglibs[ "/WEB-INF/struts-tags.tld" ] />   
<html>   
<head>   
    <title>Hello</title>   
</head>   
<body>   
< @s .form action= "/test.action" >   
< @s .textfield name= "name" />   
< @s .submit/>   
</ @s .form>   
</body>   
</html>

<#assign s=JspTaglibs[ "/WEB-INF/struts-tags.tld" ] />

这一句是为了让Freemarker识别struts2的标签,因为在Freemarker文件内,除了它自己的,都会原样显示。


web.xml 配置

<?xml version= "1.0"  encoding= "UTF-8" ?>   
<web-app id= "WebApp_9"  version= "2.4"  xmlns= "http://java.sun.com/xml/ns/j2ee"   
    xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"   
    xsi:schemaLocation= "http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" >   
  
    <display-name>Struts Blank</display-name>   
  
    <filter>   
        <filter-name>struts2</filter-name>   
        <filter- class >   
            org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter- class >   
    </filter>   
  
    <filter-mapping>   
        <filter-name>struts2</filter-name>   
        <url-pattern>/*</url-pattern>   
    </filter-mapping>   
    <servlet>   
        <servlet-name>JspSupportServlet</servlet-name>   
        <servlet- class >org.apache.struts2.views.JspSupportServlet</servlet- class >   
        <load-on-startup> 1 </load-on-startup>   
    </servlet>   
    <welcome-file-list>   
        <welcome-file>login.action</welcome-file>   
    </welcome-file-list>   
  
</web-app>

java 代码编写

import com.opensymphony.xwork2.ActionSupport; 
   public class TestAction extends ActionSupport { 
        private String name = "ChainHou"; 
        public String getName() { 
              return name; 
        } 
        public void setName(String name) { 
              this.name = name; 
        } 
      @Override 
      public String execute() throws Exception { 
             return SUCCESS; 
      } 
  }

 

相关推荐