<!-- 从后台传值过来-->
<s:set var="printTermID" value="#printTermID" ></s:set>
<form class="navbar-form navbar-left" action="UserAction_findTermID" method="post" id="findForm">
<select class="form-control" id="selectId" name="selectId">
<!-- 从数据库中选出ID, 显示在下拉列表中-->
<s:iterator var="c" value="#findAllTermID">
<!--在option中比对,后台传过来的值,如果一样,则设为默认的selected -->
<option value="<s:property value="#c"/>" <s:property value="#printTermID ==#c?'selected':''"/> > <s:property value="#c"/> </option>
</s:iterator>
</select>
<button type="button" class="btn btn-default" onclick="saveSuccess($('#selectId').val())">显示这一学期的信息</button>
</form>
其中的printTermID是从后台传值过去的,保存上一次的操作 所选中的值
然后在option中去和这个值比较,如果一样的话,就把那个值的属性设为selected即默认选项,这其中用到了三目表达式
这样就可以做到下拉列表的 回显,来看一下后台代码
///从数据库中查找指定的学期ID的情况
public String findTermID() throws Exception {
//从前台得到要查看的学期的ID
String termID = ServletActionContext.getRequest().getParameter("selectId");
printTermID = termID;//将选中的学期传给静态变量,方便下一次调用的时候直接使用,根据传过去的ID打印学期情况
//将这个变量传给前台进行比对
// System.out.println("findTermID运行了==="+printTermID+"==========findTermID运行了==="+termID+"=========");
List<User> findTermID = userService.findTermID(termID);
ActionContext.getContext().put("findTermID", findTermID);
System.out.println("findTermID"+findTermID);
ActionContext.getContext().put("printTermID", printTermID);
///从数据库中查找所有人的学期ID
findAllTermID();
///从数据库中查找所有人的学期ID
return "findAllUser";
}
其中的printTermID是静态 变量,因为我还开发了选中后打印的功能,即在下拉列表中选中要打印的学期后,点击查看即可显示学期信息,这个时候jsp已经提交,因为action是多例的,我再次点击 打印的话,就检测不到 我上次的选项值,所以我设置了一个静态变量,来接受每次下拉列表选中的值,然后再打印的方法中,用这个值作为参数打印即可,
/打印成word函数
public String printToWord() throws Exception {
System.out.println("printTermID里面的学期是===="+printTermID);
userService.printToWord(printTermID);
List<User> findTermID = userService.findTermID(printTermID);
ActionContext.getContext().put("findTermID", findTermID);
///从数据库中查找所有人的学期ID
findAllTermID();
///从数据库中查找所有人的学期ID
return "findAllUser";
}
打印的话用到了freeMarker框架
//打印指定学期信息
public void printToWord(String termID){
//@Test
//public void printToWord() {
// TODO Auto-generated method stub
Configuration configuration = null;
configuration = new Configuration(Configuration.VERSION_2_3_24);
configuration.setDefaultEncoding("UTF-8");
configuration.setClassForTemplateLoading(this.getClass(), "/"); //在这个类的当前目录下加载user.ftl src下
Template t=null;
try {
t = configuration.getTemplate("user.ftl");
// System.out.println("t===="+t);
} catch (IOException e) {
e.printStackTrace();
}
// TODO Auto-generated method stub
Map<String,Object> dataMap=new HashMap<String,Object>();
//在d盘创建一个目录
File of = new File("d:\\termMessage");
of.mkdirs();
//往这个目录里面存入学期的信息
// File file3 = new File("d:\\termMessage","user"+new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date())+".doc");
File file3 = new File("d:\\termMessage","第"+termID+"学期的信息"+".doc"); //如果文件里有这个名字的话,则不会再次创建
//输出文件的绝对路径
System.out.println("路径是========="+file3.getAbsolutePath() +"==="+file3.getName()+"--------");
Writer out = null;
//如果使用new BufferedWriter(new OutputStreamWriter(newFileOutputStream(outFile))),则生成的文件无法在word下打开。
//应设置编码格式,即new FileOutputStream(outFile),"UTF-8")。
try {
// file3.createNewFile(); //创建这一文件 不用也可以...
FileOutputStream fileOutputStream = new FileOutputStream(file3);
System.out.println("fileOutputStream"+fileOutputStream);
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream,"UTF-8");
out = new BufferedWriter(outputStreamWriter);
} catch (FileNotFoundException | UnsupportedEncodingException e1)
{
e1.printStackTrace();
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
dataMap.put("printDate", new SimpleDateFormat("yyyy年MM月dd日").format(new Date()));
dataMap.put("termID", termID);
List<User> list = new ArrayList<User>();
list = findTermID(termID);
dataMap.put("user", list);
//执行输出word文档
try {
t.process(dataMap, out);
} catch (TemplateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
打印的目录为我自己设定的地址,如果该目录下已经打印了一份信息,因为同一目录下不能有同名的文件,所以在此打印的话就不会重复, 不会进行二次打印。
<script type="text/javascript">
// var termID; //全局变量 提交后之后则为空
function alertSuccess(termID) { //应该提示导出的是哪个学期的情况,如果已经导出那么则提示文件已经存在
if(termID == null){
alert("请先选中学期!")
}else{
alert("导出成功!您导出的是第"+termID+"学期的学期信息,word文件在D:\\termMessage");
$("#printForm").submit();
}
// $("printForm").submit(); D:\\termMessage 要两个杠,因为有一个是转义字符
}
function saveSuccess(t) { //应该提示导出的是哪个学期的情况,如果已经导出那么则提示文件已经存在
// alert("您选的是"+t);
$("#findForm").submit();
}
</script>
我这次还熟悉了js代码,感觉有点东西,他可以根据 我们表单输入的东西先进行次验证,选择提交还是alert一个警告信息,你比如说这个例子中,当没有选择学期的时候,表单不会提交,而是会弹出警告避免后台处理错误。(图中提示文件已存在的功能还没有开发)
<form action="UserAction_printToWord" method="post" id="printForm">
<!-- <input type="text" id="s" > -->
<!--但是当不选学期直接导出的情况下,导出的是上一次static中的值 $('#selectId').val()-->
<button type="button" class="btn btn-primary btn-lg" onclick="alertSuccess(<s:property value="#printTermID"/>)"> 导出</button>
</form>
通过这个小功能的开发,熟悉了前后台的交互,struts标签和 ognl表达式,这个还是很强大的,需要后面继续 学习。
(道理我都懂,可为什么 浏览量怎么这么少。。。。sad,这次也不知道为什么,图片不可以上传,扎心了)