struts

struts框架的配通:
 创建Web Project,加入struts1.3能力.
 
在struts-config.xml中配置form方式:
 <form-beans>
  <form-bean name="testForm" type="form.TestForm"></form-bean>
 </form-beans>
在struts-config.xml中配置action方式:
 <action-mappings>
  <!-- path:xx.do中的请求名  type:处理请求的类路径 -->
  <!--attribute属性指代在页面中取的时候的名称由${sessionScope.testForm.userName}转变为${sessionScope.test.userName}-->
  <action path="/test" type="action.TestAction" name="testForm" scope="session" attribute="test">
   <forward name="success" path="/test/result.jsp"/>
   <forward name="fail" path="" redirect="true"/>
  </action>
 </action-mappings>
 
在struts-config.xml中配置<global-forwards>全局转发
 <!-- 如果多个action同时向一个页面进行请求,可以把该中转发设置为全局转发,此时可以被多个action共享 -->
 <!-- action的execute方法执行之后,返回的返回名称首先在局部转发中寻找,如果局部转发中没有该转发名称.则到全局转发中寻找 -->
 <!-- 注意:如果出现局部转发和全局转发同名的现象,局部转发优先级高于全局转发 -->
 <global-forwards>
  <forward name="success" path="/test/result.jsp"/>
 </global-forwards>
 
两种解决form数据遗留问题方式:
 1:把<action>标签中把scope设置为request
 2:为form类型覆盖reset方法,并且在该方法中重置所有的属性值

三种转码方式:
 1:过滤器(SetCharacterEncodingFilter);
  编写过滤器EncodingFilter.java
   public class EncodingFilter implements Filter{
    public void destroy() {}
    public void doFilter(ServletRequest request, ServletResponse response,
      FilterChain chain) throws IOException, ServletException {
     HttpServletRequest request2 = (HttpServletRequest) request;
     if("POST".equalsIgnoreCase(request2.getMethod())){
     request.setCharacterEncoding("UTF-8");
     }
     chain.doFilter(request, response);
    }
    public void init(FilterConfig filterConfig) throws ServletException {}
   }
  在web.xml中配置过滤器:
   <filter>
    <filter-name>EncodingFilter</filter-name>
    <filter-class>filter.EncodingFilter</filter-class>
   </filter>
   <filter-mapping>
    <filter-name>EncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
   </filter-mapping>
 
 2:覆盖掉ActionServlet中的doPost();
  编写EncodingServlet类:
   public class EncodingServlet extends ActionServlet{
    public void doPost(HttpServletRequest request, HttpServletResponse response)
      throws IOException, ServletException {
     request.setCharacterEncoding("UTF-8");
     super.doPost(request, response);
    }
   }
  在web.xml中更改<servlet-class>path</servlet-class>的路径
   <servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>servlet.EncodingServlet</servlet-class>
    <load-on-startup>0</load-on-startup>
   </servlet>
   
 3:在form中覆盖reset方法:
  public void reset(ActionMapping mapping, HttpServletRequest request) {
   try {
    request.setCharacterEncoding("UTF-8");
   } catch (UnsupportedEncodingException e) {
    e.printStackTrace();
   }
  }
  
struts文件上传注意:
 1:页面部分:
  文件上传要放入form中进行处理,method=post enctype=multipart/form-data
  <form action="${pageContext.request.contextPath }/upload.do" method="post" enctype="multipart/form-data"></form>
 
 2:Form部分:
  注意:上传文件的数据类型为FormFile类型;
  private FormFile theFile;
 
 3:Action部分:
  FormFile formFile = uploadForm.getTheFile();
  取得被上传文件原始名称
  String protoFileName = formFile.getFileName();
  分析出上传文件路径名称
  String newFileName = request.getRealPath("/upload")+File.separator+protoFileName;
  取得文件的输入流
  FileInputStream fis = (FileInputStream) formFile.getInputStream();
  取得文件的输出流
  FileOutputStream fos = new FileOutputStream(new File(newFileName));
  
 struts文件上传默认大小为250M,可以通过修改struts-config.xml中的controller标签来指定特定大小(controller标签位于message标签之上).
  <controller maxFileSize="2097152"></controller>
  
 如果上传文件过大.可以通过request的方式进行相应的处理:
  //判断文件是否超出设置的最大值
  if(request.getAttribute(MultipartRequestHandler.ATTRIBUTE_MAX_LENGTH_EXCEEDED) != null){
   request.setAttribute("msg", "上传失败,文件过大");
   return mapping.findForward("result");
  }
  
  //jsp页面获取msg消息通知:${requestScope.msg}
    
    
  文件上传方法:
   public void uploadFile(HttpServletRequest request , FormFile formFile){

    //取得被上传文件原始名称
    String protoFileName = formFile.getFileName();
    //处理被上传的文件
    String newFileName = request.getRealPath("/upload")+File.separator+protoFileName;
    System.out.println("新文件路径名称: "+newFileName);
    //处理文件写入过程
    FileInputStream fis = null;
    FileOutputStream fos = null;
    try {
     //输入流
     fis = (FileInputStream) formFile.getInputStream();
     //输出流
     fos = new FileOutputStream(new File(newFileName));
     
     byte[] buff = new byte[1024*1024];
     int i = 0;
     while((i=fis.read(buff))!= -1){
      fos.write(buff, 0, i);
      fos.flush();
     }
    } catch (Exception e) {
     e.printStackTrace();
    } finally{
     try {
      if(fis != null){
       fis.close();
      }
      if(fos != null){
       fos.close();
      }
     } catch (IOException e) {
      e.printStackTrace();
     }
    }
   }
  
  
struts文件下载注意:
 (1)设置浏览器的返回数据类型
  response.setContentType("application/octet/stream,charset=UTF-8");
 (2)设置文件名和浏览器如何处置返回的文件(以附件方式下载还是浏览器一句自身的插件打开的方式)
  以浏览器方式打开
  response.setHeader("Content-Disposition", "inline;filename=moluo.jpg");
  以附件方式打开
  response.setHeader("Content-Disposition", "attachment;filename=moluo.jpg");
 
  如果定义文件名为中文.需要特殊处理
  response.setHeader("Content-Disposition", "attachment;filename="+URLEncoder.encode(newFileName, "UTF-8"));
 
  
  文件下载方法:
   public void downloadFile(HttpServletRequest request,HttpServletResponse response,String protoFileName,String newFileName){
    //(1)设置浏览器的返回数据类型
    response.setContentType("application/octet/stream,charset=UTF-8");
    //(2)设置文件名和浏览器如何处置返回的文件(以附件方式下载还是浏览器一句自身的插件打开的方式)
    //response.setHeader("Content-Disposition", "inline;filename=moluo.jpg");//以浏览器方式打开
    //response.setHeader("Content-Disposition", "attachment;filename=moluo.jpg");//以附件方式打开
    try {
     //如果定义文件名为中文.需要特殊处理
     response.setHeader("Content-Disposition", "attachment;filename="+URLEncoder.encode(newFileName, "UTF-8"));
    } catch (UnsupportedEncodingException e1) {
     e1.printStackTrace();
    }
    
    OutputStream os = null;
    FileInputStream fis = null;
    try {
     os = response.getOutputStream();
     fis = new FileInputStream(request.getRealPath("/upload")+File.separator+protoFileName);
     byte[] buff = new byte[1024*1024];
     int i = 0;
     while((i = fis.read(buff)) != -1){
      os.write(buff, 0, i);
      os.flush();
     }
    } catch (Exception e) {
     e.printStackTrace();
    } finally{
     try {
      if(os != null){
       os.close();
      }
      if(fis != null){
       fis.close();
      }
     } catch (IOException e) {
      e.printStackTrace();
     }
    }
   }

posted @ 2012-11-19 11:40  小病猫  阅读(231)  评论(0编辑  收藏  举报