07-03
27

人事部门管理系统(3)

      其实从上面两篇文章已经可以大体看出这个项目的思路来了,所以此篇文章不再讲述其他三个超链接的功能,接下来我们讲讲涉外功能,如预处理功能、国际化、以某种软件打开(如txt)和页面显示出的时间变化。

      为了页面可以显示中文,我们首先利用sturts的一个扩展功能,可以实现(相当于servlet中的过滤功能):

<controller contentType="text/html;charset=UTF-8" locale="true" processorClass="companypj.MyProRequestProcessor" />

      struts_config中的controller标签写入这样的代码,表明在每次处理代码之前先进入MyProRequestProcessor类里面,我们看下这个类:

import org.apache.struts.action.RequestProcessor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.tiles.TilesRequestProcessor;

public class MyProRequestProcessor extends TilesRequestProcessor
{
protected boolean processPreprocess (HttpServletRequest request,
HttpServletResponse response)
{
try
{
request.setCharacterEncoding("UTF-8");
//other code
}
catch(Exception e){}
return true;
}
}

      因为页面jsp使用的是utf-8,所以这里也做了下utf-8的预处理。重写原来的processPreprocess()方法,这个方法在父类TilesRequestProcessor里面是一个空的方法。为了方便我们以后使用,俗称的预处理功能。


      下面我们看一下struts中的国际化配置:

<message-resources parameter="ApplicationResources" />

      实在不愿意说国际化,这个工作就是民工的活,简单且繁重。。pass

      下面还有两个插件,一个是tiles框架,一个validator框架,都是struts中的经典:
<plug-in className="org.apache.struts.tiles.TilesPlugin">
    <set-property property="definitions-config" value="/WEB-INF/tiles-congfig.xml" />
    <set-property property="definition-parser-validate" value="true"/>
  </plug-in>
  <plug-in className="org.apache.struts.validator.ValidatorPlugIn">
    <set-property property="pathnames" value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml" />
  </plug-in>



      细心的朋友可能发现在sidebar页面有一个小太阳或小月亮的图标,后面跟随时间,来看看那段代码的实现吧:
      在jsp页面有这样两行代码,一个是调出年月日,一个是调出图标:

<%=companypj.OtherBean.getTime()%>
<img src="<%=companypj.OtherBean.bgPhoto()%>" alt="" >

  
     下面我们查看一下OtherBean类:

import java.util.*;
import java.text.SimpleDateFormat;

public class OtherBean {
    public static String getTime() {

        Date now = new Date();
        SimpleDateFormat smpDateFormat =
                new SimpleDateFormat("yyyy-MM-dd");

        String buf = smpDateFormat.format(now);
        return buf;
    }

    public static String bgPhoto() {
        GregorianCalendar calendar = new GregorianCalendar();
        String bgcolor = "";
        String path1="img/time/1.gif";
        String path2="img/time/2.gif";
        int hour = calendar.get(Calendar.HOUR_OF_DAY)+8;
        if(hour>6&&hour<18)
        {
            bgcolor=path1;

            //return bgcolor;
        }
        else if(hour<=6)
        {
            bgcolor=path2;

            //return bgcolor;
        }
        else if(hour>=18)
        {
            bgcolor=path2;

            //return bgcolor;
        }
      System.out.println(hour);
      System.out.println(bgcolor);
      return bgcolor;
    }

    public static void main(String arg[]) {
        new OtherBean().bgPhoto();
    }
}

       这里就可以清楚的看到为了图片会变化的原因了。另外,因为在虚拟机JVM上面跑,用的是GMT时间,必须+8才能是北京时间。

       另外值得说一下的是,在超链接“查看员工”的时候,有一个以txt打开某员工的信息(为了方便保存档案,大多情况下是Excel,大同小异),它的处理过程是这样的:

<html:link page="/link4_one_loaddown.do" paramId="id" paramName="ID"><bean:message key="lable.emp.read.open"/></html:link>


      在配置文件中的代码:
<action input="/link4_one_view.jsp" path="/link4_one_loaddown" parameter="link4_one_loaddown" type="companypj.LoadDownAction" validate="false" >
<forward name="link4_one_success" path="/link4_one_view.jsp">
</forward>

      接着我们找到LoadDownAction:

import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.Action;
import java.util.Collection;
import javax.sql.*;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.logging.Log;
import org.apache.struts.validator.DynaValidatorForm;
import java.io.*;

public class LoadDownAction extends Action {
    public ActionForward execute(ActionMapping mapping, ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response) {
       // DynaValidatorForm empDynForm = (DynaValidatorForm) form;
        javax.servlet.ServletOutputStream servletoutputstream = null;
        DataSource ds = getDataSource(request);
        Log log = LogFactory.getLog("A");
        String type = mapping.getParameter();
        if (type.equals("link4_one_loaddown")) {
            File f = null;
            FileInputStream fileinputstream = null;
            String downloadfilepath = "/WEB-INF/download/";//定义temp文件存在的目录
            String filename = "testDownload.txt";//文件名
            String path= request.getRealPath("/");//表示网站所在文件夹
            String filepathname = path + downloadfilepath;

            //写入文件完成
            EmployeeBean empbean = new EmployeeBean(ds);
            EmployeeVO emp = empbean.getEmployee(request.getParameter("id"));
            //读出文件开始
            try {
                f = new File(filepathname, filename);
                f.createNewFile();//创建文件
                LoadDownBean loadbean=new LoadDownBean();
                loadbean.writeFile(emp,f); //把人员信息和具体文件传入,写文件的具体过程

                //做读出过程
                fileinputstream = new FileInputStream(
                        filepathname+ filename);

                servletoutputstream = response.getOutputStream();
                response.reset();
                response.setHeader("Content-disposition",
                                   "attachment; filename=" + filename);
                response.setContentType("text/plain;charset=gb2312");
                int j = 0;
                int k = 0;
                byte abyte0[] = new byte[1024];
                while ((k = fileinputstream.read(abyte0)) != -1) {
                    j += k;
                    servletoutputstream.write(abyte0);
                }

            } catch (Exception ex) {
                ex.printStackTrace();
            } finally {
                //最后删除文件
                try {
                    servletoutputstream.close();
                    fileinputstream.close();
                    File f1 = new File(filepathname+filename);
                    if (f1.exists()) {
                        System.out.println("linnan");
                        f1.delete(); //删除File.txt文件
                    }
                } catch (Exception ex1) {
                    ex1.printStackTrace();
                }
            }
            //跳转回页完成,把原来信息再显示
            emp = empbean.getEmployee(request.getParameter("id"));
            request.setAttribute(Constants.EMP_KEY, emp);
            return mapping.findForward("link4_one_success");
        }
        return null;
    }
}

       request.getRealPath("/") 表示网站所在文件夹;
       request.getRealPath(". /") 表示本网页所在文件夹;
       request.getRealPath(". . /") 表示本网页文件夹的上层文件夹;
       上面每一部我都写了注释,方便理解,再来看下LoadDownBean,它就是做在txt里面的具体写入些什么,包括简单的排版:

package companypj;

import org.apache.commons.logging.LogFactory;
import org.apache.commons.logging.Log;
import java.io.*;


public class LoadDownBean {
    Log log = LogFactory.getLog("A");



    public void writeFile(EmployeeVO emp, File f) {

            try {
                FileWriter fw = new FileWriter(f);
                BufferedWriter bw = new BufferedWriter(fw);
                byte[] line = new byte[128];
                bw.write("*****************************");
                bw.write("*****************************");
                bw.write("员工ID号:"+emp.getId());
                bw.newLine(); //断行
                //bw.write("\r\n");
                bw.write("员工姓名:"+emp.getName());
                bw.newLine();
                bw.write("*****************************");
                bw.write("*****************************");
                bw.write("性别:"+emp.getSex());
                bw.newLine();
                bw.write("年龄:"+emp.getAge());
                bw.newLine();
                bw.write("月薪:"+emp.getSalary());
                bw.newLine();
                bw.write("所在部门:"+emp.getDepartment());
                bw.newLine();
                bw.write("婚否:"+emp.getMarriage());
                bw.newLine();
                bw.write("家庭住址:"+emp.getAddress());
                bw.newLine();
                bw.write("联系电话:"+emp.getPhone());
                bw.newLine();
                bw.write("员工简历:");
                bw.write("\r\n");
                bw.write(emp.getResume());
                bw.flush(); //将数据更新至文件
                fw.close(); //关闭文件流
                bw.close();

        } catch (Exception e) {
            // out.println(e);
            e.printStackTrace();
        }

    }

}


       好了,该讲的重点,到这里都讲完了!
      

文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags: Sturts SQL存储过程
相关日志:
评论: 0 | 引用: 0 | 查看次数: 1095
发表评论
昵 称:
密 码: 游客发言不需要密码.
内 容:
验证码: 验证码
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.
字数限制 1000 字 | UBB代码 开启 | [img]标签 关闭