码迷,mamicode.com
首页 > 其他好文 > 详细

struts常用知识

时间:2017-06-05 18:58:31      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:model   ping   视图   map   res   ring   sch   stack   hot   

一,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>    

 

struts常用知识

标签:model   ping   视图   map   res   ring   sch   stack   hot   

原文地址:http://www.cnblogs.com/hackxiyu/p/6946451.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!