spring+struts2+freemarker整合

http://blog.csdn.net/legendj/article/details/8817344

废话就不多说了,直接上代码:

addStudent.ftl文件:

[html] view plain copy
 
  1. <#assign path="${request.getContextPath()}">   
  2. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  3. <%@include file="/shar/taglib.jsp" %>  
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  5. <html xmlns="http://www.w3.org/1999/xhtml">  
  6. <head>  
  7. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
  8. <title>河北大学校友会</title>  
  9.   
  10. <script type="text/javascript" src="../js/jquery-easyui-1.3.1/jquery-1.8.0.min.js"></script>  
  11. <script type="text/javascript" src="../js/jquery.validate.js"></script>  
  12. <script type="text/javascript" src="../js/jquery-easyui-1.3.1/jquery.easyui.min.js"></script>  
  13. <link rel="stylesheet" href="../js/jquery-easyui-1.3.1/themes/icon.css" type="text/css"></link>  
  14. <link rel="stylesheet" href="../js/jquery-easyui-1.3.1/themes/default/easyui.css" type="text/css"></link>  
  15. <script type="text/javascript" src="../js/jquery-easyui-1.3.1/locale/easyui-lang-zh_CN.js"></script>  
  16.   
  17. <link rel="stylesheet" href="../css/common.css" type="text/css" />  
  18.   
  19. <script type="text/javascript">  
  20.     $(document).ready(function() {  
  21.         $("#form1").validate({  
  22.             rules : {  
  23.                 "stu.name" : {  
  24.                     required : true  
  25.                 },  
  26.                 "stu.email" : {  
  27.                     required : true,  
  28.                     email : true  
  29.                 }  
  30.             },  
  31.             messages : {  
  32.                 "stu.name": {  
  33.                     required : "姓名不能为空"  
  34.                 },  
  35.                 "stu.email" : {  
  36.                     required : "请输入email地址",  
  37.                     email : "请输入正确的email地址"  
  38.                 }  
  39.             }  
  40.         });  
  41.     });  
  42. </script>  
  43.   
  44.   
  45. </head>  
  46.   
  47. <body bgcolor="F0F3F5">  
  48. <div id="man_zone">  
  49. <table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">  
  50.   <tr>  
  51.     <td height="30"><table width="100%" border="0" cellspacing="0" cellpadding="0">  
  52.       <tr>  
  53.         <td height="24" bgcolor="#353c44"><table width="100%" border="0" cellspacing="0" cellpadding="0">  
  54.           <tr>  
  55.             <td><table width="100%" border="0" cellspacing="0" cellpadding="0">  
  56.               <tr>  
  57.                 <td width="6%" height="19" valign="bottom" bgcolor="#353c44"><div align="center"><img src="../images/tb.gif" width="14" height="14" /></div></td>  
  58.                 <td width="94%" valign="bottom" bgcolor="#353c44"><span class="STYLE1"> 校友信息添加</span></td>  
  59.               </tr>  
  60.             </table></td>  
  61.           </tr>  
  62.         </table></td>  
  63.       </tr>  
  64.     </table></td>  
  65.   </tr>  
  66.   <tr>  
  67.     <td><fieldset>  
  68.         <form action="studentAction!addStu.action" method="post" id="form1">  
  69.     <table width="100%" border="0" cellpadding="0" cellspacing="1" bgcolor="355B70" bordercolor="#0000FF" >  
  70.       <tr >  
  71.         <td width="20%">  
  72.             姓    名:  
  73.         </td>  
  74.         <td width="30%" >  
  75.             <input type="text" name="stu.name" id="name" value="" />  
  76.         </td>  
  77.         <td width="20%">  
  78.             性    别:  
  79.         </td>  
  80.         <td width="30%">  
  81.             <span<select name="stu.sex" style="width:156px">  
  82.                     <option value="">  
  83.                         ------------请选择------------  
  84.                     </option>  
  85.                     <option value="男">  
  86.                         男  
  87.                     </option>  
  88.                     <option value="女">  
  89.                         女  
  90.                     </option>  
  91.                 </select</span>  
  92.         </td>  
  93.       </tr>  
  94.       <tr bgcolor="EEF2FB">  
  95.         
  96.       <td width="20%" >  
  97.             出生年月:  
  98.         </td>  
  99.         <td width="30%" >  
  100.             <span id="servernet">  
  101.                 <input type="text" name="stu.birthday" class="easyui-datebox" style="width:156px"/>  
  102.             </span>  
  103.         </td>  
  104.   
  105.         <td width="20%" >  
  106.             籍    贯:  
  107.         </td>  
  108.         <td width="30%" >  
  109.             <span id="server_name"<select name="stu.nativePlace" style="width:156px">  
  110.                     <option value="">  
  111.                         ------------请选择------------  
  112.                     </option>  
  113.                     <#list list2 as list2>  
  114.                         <option value="${list2.name }">  
  115.                             ${list2.name }    
  116.                         </option>  
  117.                     </#list>    
  118.                 </select</span>  
  119.         </td>  
  120.     </tr>  
  121.     <tr bgcolor="EEF2FB">  
  122.           
  123.         <td width="20%" >  
  124.             工作单位:  
  125.         </td>  
  126.         <td width="30%" >  
  127.             <span id="LbdwTotalPageFile"><input type="text"  
  128.                     name="stu.placeOfWork"/>  
  129.             </span>  
  130.         </td>  
  131.   
  132.         <td width="20%" >  
  133.             政治面貌:  
  134.         </td>  
  135.         <td width="30%" >  
  136.             <span id="serversoft"<select name="stu.politicsStatus" style="width:156px">  
  137.                     <option value="">  
  138.                         ------------请选择------------  
  139.                     </option>  
  140.                     <#list list6 as list6>  
  141.                         <option value="${list6.name }">  
  142.                             ${list6.name }    
  143.                         </option>  
  144.                     </#list>  
  145.                 </select</span>  
  146.         </td>  
  147.           
  148.     </tr>  
  149.     <tr bgcolor="EEF2FB">  
  150.   
  151.         <td width="20%" >  
  152.             通讯地址:  
  153.         </td>  
  154.         <td width="30%" >  
  155.             <span id="LbdwTotalVirtual">  
  156.             <input type="text" name="stu.address">  
  157.             </span>  
  158.         </td>  
  159.   
  160.         <td width="20%" >  
  161.             职称职务:  
  162.         </td>  
  163.         <td width="30%">  
  164.             <span id="LbdwAvailPageFile"><input type="text" name="stu.job"/>  
  165.             </span>  
  166.         </td>  
  167.           
  168.     </tr>  
  169.     <tr bgcolor="EEF2FB">  
  170.   
  171.         <td width="20%" >  
  172.             办公电话:  
  173.         </td>  
  174.         <td width="30%">  
  175.             <span id="serverarea"><input type="text"  
  176.                     name="stu.officePhoneNum"/>  
  177.             </span>  
  178.         </td>  
  179.           
  180.         <td width="20%" >  
  181.             学历层次:  
  182.         </td>  
  183.         <td width="30%">  
  184.             <span id="aspnetcpu"<select name="stu.degreeLevel" style="width:156px">  
  185.                     <option value="">  
  186.                         ------------请选择------------  
  187.                     </option>  
  188.           
  189.                     <#list list5 as list5>  
  190.                         <option value="${list5.name }">  
  191.                             ${list5.name }    
  192.                         </option>  
  193.                     </#list>    
  194.           
  195.                 </select</span>  
  196.         </td>  
  197.     </tr>  
  198.     <tr bgcolor="EEF2FB">  
  199.   
  200.         <td width="20%" >  
  201.             手    机:  
  202.         </td>  
  203.         <td width="30%" >  
  204.             <span id="serveraccess"><input type="text" name="stu.mobilePhoneNum"/>  
  205.             </span>  
  206.         </td>  
  207.           
  208.         <td width="20%" >  
  209.             行政级别:  
  210.         </td>  
  211.         <td width="30%" >  
  212.             <span id="LbdwTotalPhys"<select name="stu.administrativeLevel" style="width:156px">  
  213.                     <option value="">  
  214.                         ------------请选择------------  
  215.                     </option>  
  216.           
  217.                     <#list list4 as list4>  
  218.                         <option value="${list4.name }">  
  219.                             ${list4.name }    
  220.                         </option>  
  221.                     </#list>    
  222.           
  223.                 </select</span>  
  224.         </td>  
  225.     </tr>  
  226.     <tr bgcolor="EEF2FB">  
  227.         <td width="20%" >  
  228.             所学专业:  
  229.         </td>  
  230.         <td width="30%">  
  231.             <span id="LbdwAvailPhys"><input type="text" name="stu.major">  
  232.             </span>  
  233.         </td>  
  234.         <td width="20%" >  
  235.             是否在职:  
  236.         </td>  
  237.         <td width="30%">  
  238.             <span id="serverip">  
  239.              <select id="selectDemo" name="stu.beOnTheJob"  style="width:156px">  
  240.                     <option value="">  
  241.                         ------------请选择------------  
  242.                     </option>  
  243.           
  244.                 <option value="是">  
  245.                         是  
  246.                 </option>  
  247.                 <option value="否">  
  248.                     否  
  249.                 </option>  
  250.           
  251.                 </select></span>  
  252.         </td>  
  253.           
  254.     </tr>  
  255.     <tr bgcolor="EEF2FB">  
  256.         <td width="20%" >  
  257.             邮政编码:  
  258.         </td>  
  259.         <td width="30%">  
  260.             <span id="cpuc"><input type="text" name="stu.postalcode">  
  261.             </span>  
  262.         </td>  
  263.   
  264.         <td width="20%" >  
  265.             毕业院系:  
  266.         </td>  
  267.         <td width="30%">  
  268.             <span id="aspnetn"<select name="stu.graduatedDep" style="width:156px">  
  269.                     <option value="">  
  270.                         ------------请选择------------  
  271.                     </option>  
  272.   
  273.                     <#list list1 as list1>  
  274.                         <option value="${list1.depName }">  
  275.                             ${list1.depName }     
  276.                         </option>  
  277.                     </#list>    
  278.           
  279.                 </select</span>  
  280.         </td>  
  281.           
  282.     </tr>  
  283.     <tr bgcolor="EEF2FB">  
  284.         <td width="20%" >  
  285.             电子信箱:  
  286.         </td>  
  287.         <td width="30%">  
  288.             <span id="cputype"><input type="text" name="stu.email" id = "email"/>  
  289.             </span>  
  290.         </td>  
  291.         <td width="20%" >  
  292.             添加信息:  
  293.         </td>  
  294.         <td width="30%">  
  295.             <span id="serveraccess"><img src="${path}/images/add-row.gif"/>   
  296.                 <input type="submit" value="添加"/>  
  297.             </span>  
  298.         </td>  
  299.     </tr>  
  300.     </table>  
  301.     </form>  
  302.     </fieldset></td>  
  303.   </tr>  
  304. </table>  
  305. </div>  
  306. </body>  
  307. </html>  


struts2和spring的配置很简单,这里就不多说了。

 

service中生成html文件的方法:

[html] view plain copy
 
  1. *  
  2.      *   
  3.      * templatePath存放模板的目录  
  4.      * targetHtmlPath存放生成html的目录  
  5.      * ftl所需的模板文件  
  6.      * fileName生成html文件的名称  
  7.      * */  
  8.     @Override  
  9.     public void createHtml(ServletContext context, Map<Object, Object> data,String templatePath,String targetHtmlPath,String ftl,String fileName) {  
  10.         Configuration cf = new Configuration();  
  11.         cf.setServletContextForTemplateLoading(context, templatePath);  
  12.         cf.setEncoding(Locale.getDefault(), "utf-8");  
  13.         Template template = null;  
  14.         try {  
  15.             template = cf.getTemplate(ftl);  
  16.         } catch (IOException e) {  
  17.             e.printStackTrace();  
  18.         }  
  19.         template.setEncoding("utf-8");  
  20.         String path = context.getRealPath(targetHtmlPath);  
  21.         String htmlFilePath = path +"/"+ fileName;  
  22.         File file = new File(htmlFilePath);  
  23.         try {  
  24.             Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file),"utf-8"));  
  25.             template.process(data, out);  
  26.         } catch (Exception e) {  
  27.             e.printStackTrace();  
  28.         }  
  29.     }  


action中代码:

[html] view plain copy
 
  1. public String add() {  
  2.         list1 = otherService.searchXueyuan();  
  3.         list2 = otherService.searchPlace();  
  4.         list3 = otherService.searchMinzu();  
  5.         list4 = otherService.searchStulevel();  
  6.         list5 = otherService.searchDegreelevel();  
  7.         list6 = otherService.searchPolitics();  
  8.         Map<Object, Objectdata = new HashMap<Object,Object>();  
  9.         data.put("list1", list1);  
  10.         data.put("list2", list2);  
  11.         data.put("list3", list3);  
  12.         data.put("list4", list4);  
  13.         data.put("list5", list5);  
  14.         data.put("list6", list6);  
  15.         //用于解决生成的html文件导入图片、js等不能显示的问题,需要在前台获得request.getContextPath  
  16.         data.put("request", ServletActionContext.getRequest());  
  17.         otherService.createHtml(ServletActionContext.getServletContext(), data, "/templates/ftl", "/templates/html", "addStudent.ftl", "addStudent.html");  
  18.         return "add";  
  19.     }  


跳转路径则为生成的html的路径。

 

对于生成的html文件不能正常显示图片,在网上搜了一下,需要在ftl模板文件中加入:

[html] view plain copy
 
  1. <#assign path="${request.getContextPath()}">  


然后在图片的引用路径上加上path:

[html] view plain copy
 
    1. <td width="30%">  
    2.             <span id="serveraccess"><img src="${path}/images/add-row.gif"/>   
    3.                 <input type="submit" value="添加"/>  
    4.             </span>  
    5.         </td>  
posted @ 2016-07-01 08:55  zewentdcq  阅读(164)  评论(0)    收藏  举报