我是徐大志

有志者事竟成,破釜沉舟,百二秦关终属楚;
苦心人天不负,卧薪尝胆,两千越甲可吞吴。

struts常用知识

一,struts2是什么?

  struts2是一个控制框架,相当于连接底层和显示层,控制页面和数据展示

 

二,为什么用struts2?

  jsp+javabean模式:jsp里的小脚本java代码太多,页面杂乱  

  jsp+servlet+javabean模式:servlet和jspAPI强耦合,代码复用率低

  jsp+struts2+javabean模式: 把servlet进行了封装,使用拦截器拦截请求,使用结果视图进行响应


三,struts2怎么用?

  1.导入框架所需的包,软件默认在web-info下添加全局过滤器

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    
  <display-name></display-name>    
  
  <!-- 首页配置 -->
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  <!-- struts配置 -->
  <filter>
      <filter-name>struts2</filter-name>
      <filter-class>
          org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
      </filter-class>
  </filter>
  <filter-mapping>
      <filter-name>struts2</filter-name>
      <url-pattern>/*</url-pattern>
  </filter-mapping>
  
</web-app>

 

  2.编写action类,【继承ActionSupport】

package com.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.commons.io.IOUtils;
import org.apache.struts2.ServletActionContext;

import com.bean.User;
import com.dao.IUserDao;
import com.dao.impl.UserDaoImpl;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class UserAction extends ActionSupport implements ModelDriven<User>{
    
    //文件上传
    private File file;
    private String fileContentType;
    private String fileFileName;
    
    public File getFile() {
        return file;
    }
    public void setFile(File file) {
        this.file = file;
    }
    public String getFileContentType() {
        return fileContentType;
    }
    public void setFileContentType(String fileContentType) {
        this.fileContentType = fileContentType;
    }
    public String getFileFileName() {
        return fileFileName;
    }
    public void setFileFileName(String fileFileName) {
        this.fileFileName = fileFileName;
    }

    //数据接收
    private User u;
    
    public User getU() {
        return u;
    }
    public void setU(User u) {
        this.u = u;
    }
    @Override
    public User getModel() {
        if(this.u==null){
            this.u=new User();
        }
        return this.u;
    }
    
    public String add(){
        
        //保存文件
        String path = ServletActionContext.getServletContext().getRealPath("/upload/");
        File dir=new File(path);
        if(!dir.exists()){
            dir.mkdirs();
        }
        try {
            FileInputStream input=new FileInputStream(file);
            FileOutputStream output=new FileOutputStream(path+"/"+fileFileName);
            IOUtils.copy(input, output);
            input.close();
            output.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        //调用数据接口,添加数据库
        IUserDao iud = new UserDaoImpl();
        this.u.setPhoto(fileFileName);
        Integer result = iud.addUser(u);
        if(result>-1){
            System.out.println("添加成功!");
        }else{
            System.out.println("添加失败!");
        }
        
        return SUCCESS;
    }

    
}

 

  3.在struts.xml中注册action和相应的结果视图

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
    <!-- 设置上传文件大小 -->
    <constant name="struts.multipart.maxSize" value="10000000"></constant>
    
    <package name="test" namespace="/" extends="struts-default">
    
        <action name="*_*" class="com.action.{1}Action" method="{2}">
            <!-- 拦截器指定文件大小 -->
            <interceptor-ref name="fileUpload">
                <param name="maximumSize">10000000</param>
            </interceptor-ref>
            <interceptor-ref name="defaultStack"/>
            
            <result name="success">success.jsp</result>
        </action>
        
        <!-- 文件上传测试 -->
        <action name="Uplod" class="com.action.UploadAction">
            <!-- 拦截器指定文件大小 -->
            <interceptor-ref name="fileUpload">
                <param name="maximumSize">10000000</param>
            </interceptor-ref>
            <interceptor-ref name="defaultStack"/>
            
            <result name="success">success.jsp</result>
        </action>
        
    </package>

</struts>    

 

posted @ 2017-06-05 18:28  我是徐大志  阅读(203)  评论(0编辑  收藏  举报
【少年,我看你目光炯炯有神,将来一定能成大事!】