struts2 笔记

来源:互联网 发布:剑灵秦夕妍捏脸数据 编辑:程序博客网 时间:2024/06/02 06:17

struts2 笔记

下载地址:http://struts.apache.org

1.配置:
- 在 web.xml 中声明Struts 2 提供的过滤器,过滤路径等。

<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>*.action</url-pattern>  </filter-mapping>   <filter-mapping>    <filter-name>struts2</filter-name>    <url-pattern>*.jsp</url-pattern>  </filter-mapping>

2.在Struts.xml文件中配置Action以及映射页面。

<struts>  <package name="default" namespace="/" extends="struts-default">    <action name="userAction" class="cn.edu.pzhu.zc.Action.UserAction" method="add">            <result name="error">login.jsp</result>            <result name="success">success.jsp</result>        </action></struts>        
  • name:用于配置 Action 对象被请求的URL映射
  • class:指定Action对象的类名
  • method:设置请求Action对象时调用改对象的哪一个方法

3.请求参数注入原理:
表单提交的数据会自动注入到与Action 对象中相应的属性,例如创建一个UserAction类,提供username,password两个属性:

public class UploadMoreFile extends ActionSupport {    private String username;    private String password;    public void setUsername(String username) {        this.username= username;    }    public void getUsername() {        return username;    }    public void setPassword(String password) {        this.password= password;    }    public void getPassword() {        return password;    }
  • 需要注入属性值的 Action 对象必须为属性提供setter() 方法,因为Struts 2 的内部是按照 JavaBean 规范提供的
    setter 方法自动为属性注入值。
  • Action 必须要继承ActionSupport

4.Struts 标签库:
在JSP页面顶部引入标签:

<%@taglib prefix="s" uri="/struts-tags" %>

5.提交 JSP 页面:

<s:form action="login.action" method="post" enctype="multipart/form-data">    <s:textfield name="username" label="username"/><br/>    <s:password name="password" label="password"></s:password><br/>    <s:submit value="提交" /></s:form>

提交到名为login的action中。
超链接提交:

<a href="userAction.action?username='Tom'">提交</a>

6.文件上传:
Action:

public class UploadAction extends ActionSupport {    private File upload;    private String uploadFileName;    private String uploadContentType;  //设置上传文件类型    public File getUpload() {        return upload;    }    public void setUpload(File upload) {        this.upload = upload;    }    public String getUploadFileName() {        return uploadFileName;    }    public void setUploadFileName(String uploadFileName) {        this.uploadFileName = uploadFileName;    }    public String getUploadContentType() {        return uploadContentType;    }    public void setUploadContentType(String uploadContentType) {        this.uploadContentType = uploadContentType;    }    public String upload() throws IOException {        //创建的是真实目录,不是虚拟目录。        InputStream in;        String uploadPath = "d:\\upload";        try {            File file = new File(uploadPath);            if(!file.exists()){                file.mkdirs();            }        } catch (Exception e) {            System.out.println("目录创建失败!");        }        try {            in = new FileInputStream(upload);            OutputStream out = new FileOutputStream(uploadPath+"\\"+uploadFileName);            byte buffer[] = new byte[8192];            int count = 0;            while((count = in.read(buffer))>0){                out.write(buffer, 0, count);            }            in.close();            out.close();            //将上传的文件进行封装,方便后面下载。            String url = "d:\\upload\\" + uploadFileName;            FileMsg fileMsg = new FileMsg(uploadFileName,url,new Date());            FileList.getInstance().getList().add(fileMsg);            ServletUtil.GetRequest().getSession().setAttribute("LIST", FileList.getInstance().getList());        } catch (FileNotFoundException e) {            System.out.println("文件上传失败!");            e.printStackTrace();        }        return SUCCESS;    }}

JSP:

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><%@taglib prefix="s" uri="/struts-tags" %><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>上传单个文件</title></head><body>    <s:form action="upload.action" method="post" enctype="multipart/form-data">        <s:file name="upload" label="选择文件"/><br/>        <s:submit value="提交" />    </s:form></body></html>

注意:前端file控件的name必须与Action中属性值一致,否则注入失败。

比如:JSP中: <s:file name="upload" label="选择文件"/><br/>   Action中:      private File upload;    private String uploadFileName;    private String uploadContentType;如果JSP中: <s:file name="uploads" label="选择文件"/><br/> Action中:      private File uploads;    private String uploadsFileName;    private String uploadsContentType;

download 页面:

<body><%    out.clear();    out = pageContext.pushBody();    String path = request.getParameter("path");    SmartUpload su = new SmartUpload();    su.initialize(pageContext);    su.downloadFile(path);%></body>

文件列表页面:

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><%@taglib prefix="c" uri = "http://java.sun.com/jsp/jstl/core" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>文件列表</title></head><body> <table border="1">    <tr>        <th>上传路径</th>        <th>上传时间</th>    </tr>    <c:forEach items="${LIST }" var = "temp">        <tr>            <td><a href=download.jsp?path=${temp.url}>${temp.url}</a></td>            <td>${temp.date}</td>        </tr>       </c:forEach></table></body></html>

结果展示:
运行效果