spring+struts2+freemarker整合

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

addStudent.ftl文件:

<#assign path="${request.getContextPath()}"> 
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@include file="/shar/taglib.jsp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>河北大学校友会</title>

<script type="text/javascript" src="../js/jquery-easyui-1.3.1/jquery-1.8.0.min.js"></script>
<script type="text/javascript" src="../js/jquery.validate.js"></script>
<script type="text/javascript" src="../js/jquery-easyui-1.3.1/jquery.easyui.min.js"></script>
<link rel="stylesheet" href="../js/jquery-easyui-1.3.1/themes/icon.css" type="text/css"></link>
<link rel="stylesheet" href="../js/jquery-easyui-1.3.1/themes/default/easyui.css" type="text/css"></link>
<script type="text/javascript" src="../js/jquery-easyui-1.3.1/locale/easyui-lang-zh_CN.js"></script>

<link rel="stylesheet" href="../css/common.css" type="text/css" />

<script type="text/javascript">
	$(document).ready(function() {
		$("#form1").validate({
			rules : {
				"stu.name" : {
					required : true
				},
				"stu.email" : {
					required : true,
					email : true
				}
			},
			messages : {
				"stu.name": {
					required : "姓名不能为空"
				},
				"stu.email" : {
					required : "请输入email地址",
					email : "请输入正确的email地址"
				}
			}
		});
	});
</script>


</head>

<body bgcolor="F0F3F5">
<div id="man_zone">
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td height="30"><table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td height="24" bgcolor="#353c44"><table width="100%" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
              <tr>
                <td width="6%" height="19" valign="bottom" bgcolor="#353c44"><div align="center"><img src="../images/tb.gif" width="14" height="14" /></div></td>
                <td width="94%" valign="bottom" bgcolor="#353c44"><span class="STYLE1"> 校友信息添加</span></td>
              </tr>
            </table></td>
          </tr>
        </table></td>
      </tr>
    </table></td>
  </tr>
  <tr>
    <td><fieldset>
    	<form action="studentAction!addStu.action" method="post" id="form1">
    <table width="100%" border="0" cellpadding="0" cellspacing="1" bgcolor="355B70" bordercolor="#0000FF" >
      <tr >
        <td width="20%">
			姓    名:
		</td>
		<td width="30%" >
			<input type="text" name="stu.name" id="name" value="" />
		</td>
		<td width="20%">
			性    别:
		</td>
		<td width="30%">
			<span> <select name="stu.sex" style="width:156px">
					<option value="">
						------------请选择------------
					</option>
					<option value="男">
						男
					</option>
					<option value="女">
						女
					</option>
				</select> </span>
		</td>
      </tr>
	  <tr bgcolor="EEF2FB">
	  
	  <td width="20%" >
			出生年月:
		</td>
		<td width="30%" >
			<span id="servernet">
				<input type="text" name="stu.birthday" class="easyui-datebox" style="width:156px"/>
			</span>
		</td>

		<td width="20%" >
			籍    贯:
		</td>
		<td width="30%" >
			<span id="server_name"> <select name="stu.nativePlace" style="width:156px">
					<option value="">
						------------请选择------------
					</option>
					<#list list2 as list2>
						<option value="${list2.name }">
							${list2.name }	
						</option>
					</#list>	
				</select> </span>
		</td>
	</tr>
	<tr bgcolor="EEF2FB">
		
		<td width="20%" >
			工作单位:
		</td>
		<td width="30%" >
			<span id="LbdwTotalPageFile"><input type="text"
					name="stu.placeOfWork"/>
			</span>
		</td>

		<td width="20%" >
			政治面貌:
		</td>
		<td width="30%" >
			<span id="serversoft"> <select name="stu.politicsStatus" style="width:156px">
					<option value="">
						------------请选择------------
					</option>
					<#list list6 as list6>
						<option value="${list6.name }">
							${list6.name }	
						</option>
					</#list>
				</select> </span>
		</td>
		
	</tr>
	<tr bgcolor="EEF2FB">

		<td width="20%" >
			通讯地址:
		</td>
		<td width="30%" >
			<span id="LbdwTotalVirtual">
			<input type="text" name="stu.address">
			</span>
		</td>

		<td width="20%" >
			职称职务:
		</td>
		<td width="30%">
			<span id="LbdwAvailPageFile"><input type="text" name="stu.job"/>
			</span>
		</td>
		
	</tr>
	<tr bgcolor="EEF2FB">

		<td width="20%" >
			办公电话:
		</td>
		<td width="30%">
			<span id="serverarea"><input type="text"
					name="stu.officePhoneNum"/>
			</span>
		</td>
		
		<td width="20%" >
			学历层次:
		</td>
		<td width="30%">
			<span id="aspnetcpu"> <select name="stu.degreeLevel" style="width:156px">
					<option value="">
						------------请选择------------
					</option>
		
					<#list list5 as list5>
						<option value="${list5.name }">
							${list5.name }	
						</option>
					</#list>	
		
				</select> </span>
		</td>
	</tr>
	<tr bgcolor="EEF2FB">

		<td width="20%" >
			手    机:
		</td>
		<td width="30%" >
			<span id="serveraccess"><input type="text" name="stu.mobilePhoneNum"/>
			</span>
		</td>
		
		<td width="20%" >
			行政级别:
		</td>
		<td width="30%" >
			<span id="LbdwTotalPhys"> <select name="stu.administrativeLevel" style="width:156px">
					<option value="">
						------------请选择------------
					</option>
		
					<#list list4 as list4>
						<option value="${list4.name }">
							${list4.name }	
						</option>
					</#list>	
		
				</select> </span>
		</td>
	</tr>
	<tr bgcolor="EEF2FB">
		<td width="20%" >
			所学专业:
		</td>
		<td width="30%">
			<span id="LbdwAvailPhys"><input type="text" name="stu.major">
			</span>
		</td>
		<td width="20%" >
			是否在职:
		</td>
		<td width="30%">
			<span id="serverip">
			 <select id="selectDemo" name="stu.beOnTheJob"  style="width:156px">
					<option value="">
						------------请选择------------
					</option>
		
				<option value="是">
						是
				</option>
				<option value="否">
					否
				</option>
		
				</select></span>
		</td>
		
	</tr>
	<tr bgcolor="EEF2FB">
		<td width="20%" >
			邮政编码:
		</td>
		<td width="30%">
			<span id="cpuc"><input type="text" name="stu.postalcode">
			</span>
		</td>

		<td width="20%" >
			毕业院系:
		</td>
		<td width="30%">
			<span id="aspnetn"> <select name="stu.graduatedDep" style="width:156px">
					<option value="">
						------------请选择------------
					</option>

					<#list list1 as list1>
						<option value="${list1.depName }">
							${list1.depName }	
						</option>
					</#list>	
		
				</select> </span>
		</td>
		
	</tr>
	<tr bgcolor="EEF2FB">
		<td width="20%" >
			电子信箱:
		</td>
		<td width="30%">
			<span id="cputype"><input type="text" name="stu.email" id = "email"/>
			</span>
		</td>
		<td width="20%" >
			添加信息:
		</td>
		<td width="30%">
			<span id="serveraccess"><img src="${path}/images/add-row.gif"/> 
				<input type="submit" value="添加"/>
			</span>
		</td>
	</tr>
    </table>
    </form>
    </fieldset></td>
  </tr>
</table>
</div>
</body>
</html>


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

 

service中生成html文件的方法:

*
	 * 
	 * templatePath存放模板的目录
	 * targetHtmlPath存放生成html的目录
	 * ftl所需的模板文件
	 * fileName生成html文件的名称
	 * */
	@Override
	public void createHtml(ServletContext context, Map<Object, Object> data,String templatePath,String targetHtmlPath,String ftl,String fileName) {
		Configuration cf = new Configuration();
		cf.setServletContextForTemplateLoading(context, templatePath);
		cf.setEncoding(Locale.getDefault(), "utf-8");
		Template template = null;
		try {
			template = cf.getTemplate(ftl);
		} catch (IOException e) {
			e.printStackTrace();
		}
		template.setEncoding("utf-8");
		String path = context.getRealPath(targetHtmlPath);
		String htmlFilePath = path +"/"+ fileName;
		File file = new File(htmlFilePath);
		try {
			Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file),"utf-8"));
			template.process(data, out);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}


action中代码:

public String add() {
		list1 = otherService.searchXueyuan();
		list2 = otherService.searchPlace();
		list3 = otherService.searchMinzu();
		list4 = otherService.searchStulevel();
		list5 = otherService.searchDegreelevel();
		list6 = otherService.searchPolitics();
		Map<Object, Object> data = new HashMap<Object,Object>();
		data.put("list1", list1);
		data.put("list2", list2);
		data.put("list3", list3);
		data.put("list4", list4);
		data.put("list5", list5);
		data.put("list6", list6);
		//用于解决生成的html文件导入图片、js等不能显示的问题,需要在前台获得request.getContextPath
		data.put("request", ServletActionContext.getRequest());
		otherService.createHtml(ServletActionContext.getServletContext(), data, "/templates/ftl", "/templates/html", "addStudent.ftl", "addStudent.html");
		return "add";
	}


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

 

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

<#assign path="${request.getContextPath()}">


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

<td width="30%">
			<span id="serveraccess"><img src="${path}/images/add-row.gif"/> 
				<input type="submit" value="添加"/>
			</span>
		</td>


 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值