流世幻羽

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

结果集:

//全局结果  源码:struts-default.xml中<package>标签中
<global-results> <result name="">/xxx.jsp</global-results>
    //局部结果
  <action><result name="">/xxx.jsp</result><</action>
    //结果类型结果类型是在父类配置struts-default中定义的,struts框架默认实现了绝大多数的。
<result type="dispacher">/xx.jsp</result>     //默认值//action--->jsp的转发
<result type="chain"><param name="actionName">xx.action</param></result> //action--->action的转发

<result type="redirect">//action-->jsp的重定向
<param name="actionName" type="redirectAction"></param>//action--action重定向
<param name="namespace" ></param>//表示的名字
</result> 
//图片的下载stream
<result name="stream" type="stream">//stream文件的
        <param name="contextType">image/jpeg</param>//设置文件的类型
        <param name="inputName">imagel</param>// 必须有get的方法,创建的fileInputStream的对象的名称要一致
        <param name="bufferSize">1024</param>//
        <!-- <param name="contentDisposition">attachment;filename=aaa.png</param> -->//设置就是下载
            </result>
 //json对象的配置//json的从定向  先导入一个插件(jar包plugin)//看源码<root>才有用
//记得修改extends 的是json-default
<package name="test02" namespace="/test02" extends="json-default">
        <action name="json" class="org.itheima.struts.test1.Test01"
            method="json">
            <result name="json" type="json">
                <param name="root">jsonObject</param>//在java代码中创建一个map集合的名称保持一致
                <param name="encoding">utf-8</param>//编码的形式
            </result>
        </action>
    </package>
jsonActionRedirect: 重定向。./////////////////////////////////////////////////////////////
<action name="xxx" method="xxx"
        class="org.itheima.struts.action.JsonAction">
        <result name="success" type="jsonActionRedirect">
            xxx.action
        </result>
</action>
由action重定向到json结果。

2.验证码的生成和验证

 

1./////////////////////////////////////////////////////////////////////////
    <img alt="验证码" src="${pageContext.request.contextPath}/user/validateCode.action" 
onclick="javascript:this.src='${pageContext.request.contextPath}/user/validateCode.action?a='+Math.random()"/> > 2. // 验证码 struts标准的写法/////////////////////////////////////////////////////////// ValidateCode validateCode = new ValidateCode(100, 40, 4, 20); validateCode.createCode(); String code = validateCode.getCode(); // 因为validate有一个validate.write(输出流) // 输出流变成输入流 ByteArrayOutputStream baos = null; baos = new ByteArrayOutputStream(); try { validateCode.write(baos); //获得输入流 imageStream=new ByteArrayInputStream(baos.toByteArray()); } catch (IOException e) { e.printStackTrace(); } return USER_VALIDATE; } //方法二 ValidateCode validateCode = new ValidateCode(100, 40, 4, 20); validateCode.createCode(); String code = validateCode.getCode(); sHttpSession session = ServletActionContext.getRequest().getSession(); session.setAttribute("code", code); //将验证码写会到浏览器 HttpServletResponse response = ServletActionContext.getResponse(); try { validateCode.write(response.getOutputStream()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null;//防止在写回去的时候关闭流 } private InputStream imageStream; public InputStream getImageStream() { return imageStream; } public void setImageStream(InputStream imageStream) { this.imageStream = imageStream; } 3.//strts-default.xml源码////////////////////////////////////////////////////// <result name="USER_VALIDATE" type="stream"> <param name="contentType">image/jpeg</param> <param name="inputName">imageStream</param> <!-- <param name="contentDisposition">attachment;filename="document.pdf"</param> --> <param name="bufferSize">1024</param> </result> 4.//登录验证码的验证. public String do_login() { // 判断输入的验证码和放入到缓存的是否一致. HttpSession session = ServletActionContext.getRequest().getSession(); String validate_code = (String) session.getAttribute("code"); if(!validate_code.equalsIgnoreCase(user.getValidate_code())){ addFieldError("validate_code", "验证码输入错误"); return USER_LOGERR; }

文件上传和修改时回显

上传:

//文件上传的时候表单提交
<form id="form1" name="form1" enctype="multipart/form-data">  
//源码;;setruts-core--fileup--interceptor
3个属性;路径在jdbc.properties
//注解标签   @value($(jdbc.))
1.<form>///////////////////////////////////////////////////////////////////////////
<form id="form1" name="form1" enctype="multipart/form-data">
<td>文件上传 :<s:file class="textbox" id="sChannel2"style="width: 180px;height: 21px" maxlength="50" name="cust_image_file" /></td>                            
2.action类中注意写法名字是根据源码来的///////////////////////////////////////////////////////////
@Value("$(fileupload)")//在.properties中配置  fileupload="D:/02";
private String fileupDir;//两种方法去配置
private File cust_image_file;//上传的文件  对象
private String cust_image_fileContentType;//上传文件的类型 cust_name_file + ContypeType
private String cust_image_fileFileName;//上传文件的名称 cust_name_file +  FileName;
//此处省略生成的get和set方法
        String name = FileUtils.getFileName(cust_image_fileFileName);
        File destFile=new File(fileupDir, name);//将文件以指名字存入
        if(!destFile.getParentFile().exists()){//如果目录不存在,就创建???
            destFile.getParentFile().mkdirs();//
        }
        System.out.println(destFile.getAbsolutePath());
        try {
            org.apache.commons.io.FileUtils.copyFile(cust_image_file, destFile);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        customer.setCust_image(name);
3.工具类/////////////////////////////////////////////////////////////////////////////////////////
public static String getFileName(String fileName) {//可以生成日期格式的文件夹..
        String extra = fileName.substring(fileName.lastIndexOf("."));
        String name = UUID.randomUUID().toString().concat(extra);
        // 添加时间前缀
        Calendar instance = Calendar.getInstance();
        instance.setTime(new Date());
        int year = instance.get(Calendar.YEAR);
        int month = instance.get(Calendar.MONTH) + 1;
        int date = instance.get(Calendar.DATE);
        int hour = instance.get(Calendar.HOUR_OF_DAY);
        // int minute = instance.get(Calendar.MINUTE);
        StringBuffer sb = new StringBuffer();
        sb.append(year);
        sb.append("/");
        sb.append(month);
        sb.append("/");
        sb.append(date);
        sb.append("/");
        sb.append(hour);
        sb.append("/");
        sb.append(name);
        return sb.toString();
    }
4.保存的位置的两种写法///////////////////////////////////////////////////////////////////////////////
//现在jdbc.properties配置
file.up.dir=c:/03
@Value("${file.up.dir}")
private String fileupDir;
或者第二种注入的方式
<bean id="customerAction">
<propety name="fileupDir" value="${file.up.dir}"/>
</bean>

图片的回显:

 

1.jsp中修改链接///////////////////////////////////////////////////////////////////
<img alt="照片" src="${pageContext.request.contextPath }/customer/picture.action?cust_id=<s:property value='cust_id' />">
2.编写action///////////////////////////////////////////////////////////////////////////////////////
/**
     * 修改用户的照片客户资质
     */
    public String do_picture() {
    1).findbyId    // 根据id查询
        Customer find = customerService.findbyId(customer.getCust_id());
        String cust_image = find.getCust_image();
        File file = new File(fileupDir, cust_image);
        FileInputStream fis = null;
    2).判断是否为空/////一般不会为空
      if (StringUtils.isEmpty(cust_image)) {
            return null;
        }
        // 流读写3).流读写   FileInputStream---->  ServletOutputStream
        try {
            ServletOutputStream os = ServletActionContext.getResponse().getOutputStream();
            fis = new FileInputStream(file);
            byte[] arr = new byte[1024];
            int len;
            while ((len = fis.read(arr)) != -1) {
                os.write(arr, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            if(fis!=null){
                try {
                    fis.close();//注意servletOutputstream流框架不用我们关闭
                } catch (IOException e) {
                    e.printStackTrace();
                }
                fis=null;
            }
        }
        return null;
    }
3.在edit中修改  if(!null) else{根据id获取图片再次存入}//////////////////////////////////
if (cust_image_file != null && cust_image_file.length() > 0) {              
    String fileName = FileUtils.getFileName(cust_image_fileFileName);       
    File destFile = new File(fileupDir, fileName);                          
    try {                                                                   
        if (!destFile.getParentFile().exists()) {                           
            destFile.getParentFile().mkdirs();                              
        }      
System.out.println(destFile.getAbsolutePath()); org.apache.commons.io.FileUtils.copyFile(cust_image_file, destFile); customer.setCust_image(fileName); }
catch (IOException e) { e.printStackTrace(); } } else { Customer find11 = customerService.findbyId(customer.getCust_id()); System.out.println("11111111111111111111111111111111111111"); String cust_image = find11.getCust_image();
   customer.setCust_image(cust_image); }
customer.update(customer);
}


因为
else {
Customer find11 = customerService.findbyId(customer.getCust_id());//对象find11存入到缓存区
String cust_image = find11.getCust_image();
System.out.println(find11.getCust_id());
customer.setCust_image(cust_image);
}
customerService.update(customer); //customer和find一样导致了相同的id有两个对象 return UPDATE_SUCCESS;

只需要 customerdao.clear();//清空缓存

 

 

posted on 2017-06-08 14:18  流世幻羽  阅读(185)  评论(0编辑  收藏  举报