六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 45|回复: 0

围绕分页的练习(2)(ssh+freemarker)

[复制链接]

升级  30%

3

主题

3

主题

3

主题

童生

Rank: 1

积分
15
 楼主| 发表于 2013-1-28 19:23:27 | 显示全部楼层 |阅读模式
5、群删功能,通过checkbox选择。
function unselectall(){         if(document.form2.all.checked){                document.form2.all.checked =false;          }    }    function checkall(form){        for (var i=0;i<form.elements.length;i++){            var e = form.elements;            if (e.Name != 'all'&&e.disabled==false)                   e.checked = form.all.checked;        }   }   function  deluser(currentPage,pageSize){       document.form2.action="deluser.action?page.currentPage="+currentPage+"&page.pageSize="+pageSize;    //不必写userModel.delib   document.form2.submit();}  
6、查看用户通过单击单元格即可。
function redirecAction(uid){      document.form2.action="toLookPage.action?userModel.uid="+uid;   document.form2.submit();   }  
<td > 
7、解决中文乱码,统一编码为UTF-8。
   1)、struts.properties
           struts.i18n.encoding=UTF-8
           struts.locale=zh_CN
   2)、freemarker.properties
           locale=zh_CN
           default_encoding=UTF-8
   3)、struts.xml
<?xml version="1.0" encoding="UTF-8"?> 
   4)、html  
   
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
<form id="from3" name="from3" action="changpagesize.action" method="post">   
   5)、 如果有jsp相关的中文乱码得加个过滤器吧。    
       a) 、web.xml( SetCharacterEncoding.java和SetCharacterEncodingFilter.java)
<filter>        <filter-name>Set Character Encoding</filter-name>        <filter-class>nn.bbs.common.SetCharacterEncodingFilter</filter-class>        <init-param>             <param-name>encoding</param-name>             <param-value>UTF-8</param-value>        </init-param>       <init-param>             <param-name>ignore</param-name>             <param-value>true</param-value>      </init-param>     </filter>       <filter-mapping>      <filter-name>Set Character Encoding</filter-name>      <url-pattern>/*</url-pattern>   </filter-mapping> 
 或
 
<filter>       <filter-name>SetCharacterEncoding</filter-name>       <filter-class>nn.bbs.common.SetCharacterEncoding</filter-class>   </filter>   <filter-mapping>      <filter-name>SetCharacterEncoding</filter-name>    <url-pattern>/*</url-pattern>   </filter-mapping>  
       b)、jsp
<%@ page language="java" contentType="text/html;charset=utf-8"%> 
8、表的美化,鼠标指定某一单元格显示出来。参照
   
9、用freemarker操作select的显示出用户的市名、省名。
    
<select id="A"  onchange="getCity(this.value)">         <#if userModel.shengname?exists>           <option >${userModel.shengname?if_exists}</option>           <#else>             <option>请选择</option>          </#if>   </select>    <select id="B" name="userModel.shiid">         <#if userModel.shiname?exists>             <option>${userModel.shiname?if_exists}</option>          <#else>             <option>请选择</option>          </#if>   </select>   
10、将一页的数据保存为Excel文件。
   1)、struts-user.xml
      
<!-- 导出Excel --><action name="excel" method="getexcel" class="userAction">                <result name="excel" type="stream">                    <param name="contentType">application/vnd.ms-excel</param>    <!-- 注意这里的ContentType -->                    <param name="inputName">excelStream</param>                   <!-- 这里需要和Action里的变量名一致 -->                    <param name="contentDisposition">filename="standard.xls"</param>                    <param name="bufferSize">1024</param>                </result>        </action> 
   2)、UserAction.java
//导出数据到Ecxelpublic String getexcel() throws Exception {excelStream=userService.gettheInputStream();          return "excel";    } 
   3)、UserServerImpl.java
//导出为Excel    public  InputStream gettheInputStream(){    //create excel report //first create workbook HSSFWorkbook workBook = new HSSFWorkbook(); //create sheet HSSFSheet sheet = workBook.createSheet("UserSheet"); //create row , HSSFRow row = sheet.createRow(0); //create cell HSSFCell cell=null;String[] starr={"序号","姓名","密码","性别","电子邮箱","问题","回答","注册时间","市名"};        for(int i=0;i<starr.length;i++){        cell = row.createCell((short)i); cell.setEncoding(HSSFCell.ENCODING_UTF_16); cell.setCellValue(starr); //System.out.print(i+1);//System.out.println(starr);}ActionContext ctx = ActionContext.getContext();         Map m=ctx.getSession();   List list=new ArrayList(); list=(List) m.get("gettheexcel");for(int i=0;i<list.size();i++){Object[] obj=(Object[])list.get(i);row = sheet.createRow(i + 1); for(int j=0;j<starr.length;j++){cell = row.createCell((short)j); cell.setEncoding(HSSFCell.ENCODING_UTF_16); if(j==0){cell.setCellValue(i+1); }else{if(j==7){cell.setCellValue(((Date)obj[j]).toString()); }else{cell.setCellValue((String)obj[j]);}}}    System.out.println((String)obj[0]+"  "+(String)obj[1]+"  "+(String)obj[2]+"  "+(String)obj[3]+"  "+(String)obj[4]+"  "+(String)obj[1]+"  "+(String)obj[6]+"  "+((Date)obj[7]).toString()+"  "+(String)obj[8]);   }ByteArrayOutputStream os = new ByteArrayOutputStream(); try { //把workBook中的数据以字节数组的形式写进输出流中 workBook.write(os); } catch (IOException e) { e.printStackTrace(); } //获取输出流的内容 byte[] content = os.toByteArray(); InputStream excelStream  = new ByteArrayInputStream(content); return excelStream;    }  
  每当完成一项功能,都是令人欣喜的。做得很简陋,毕竟这是练习嘛。具体的代码请下载附件ssh_bbs.rar,包在“文章管理”-->“lib”-->“ssb运行的包”里,有什么不妥之处,请各位指正!
补:后来我用模型驱动改进了一下代码,修正了当list.size()=0时,显示出下一页、首页和currentPage=1的问题。改后的代码在附件"新.rar"中。
 
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表