课时15

  • 访问或添加request/session/application属性
  • 1.简单说 page指当前页面。在一个jsp页面里有效
    2.request 指从http请求到服务器处理结束,返回响应的整个过程。在这个过程中使用forward方式跳转多个jsp。在这些页面里你都可以使用这个变量。
    3.Session 有效范围当前会话,从浏览器打开到浏览器关闭这个过程。
    4.application它的有效范围是整个应用。

   struts之ActionContext

   ActionContext和ServletActionContext区别

 public String execute() {
ActionContext ac = ActionContext.getContext();
ac.getApplication().put("app", "应用范围");
ac.getSession().put("ses","session范围");
ac.put("req","request范围"); return "success";
}
${applicationScope.app}<br>
${sessionScope.ses}<br>
${requestScope.req}
  • 获取HttpServletRequest / HttpSession / ServletContext / HttpServletResponse对象

   方法一,通过ServletActionContext.类直接获取:

 public String rsa() throws Exception{
HttpServletRequest request = ServletActionContext.getRequest();
ServletContext servletContext = ServletActionContext.getServletContext();
request.setAttribute("req", "请求范围属性");
request.getSession().setAttribute("ses", "会话范围属性");
servletContext.setAttribute("app", "应用范围属性");
//HttpServletResponse response = ServletActionContext.getResponse(); return "success";
}

方法二,实现指定接口,由struts框架运行时注入:

 public class HelloWorldAction implements ServletRequestAware, ServletResponseAware, ServletContextAware{
private HttpServletRequest request;
private ServletContext servletContext;
private HttpServletResponse response;
public void setServletRequest(HttpServletRequest req) {
this.request=req;
}
public void setServletResponse(HttpServletResponse res) {
this.response=res;
}
public void setServletContext(ServletContext ser) {
this.servletContext=ser;
}
  • 放集合到request范围

  HelloWorld.java

 ac.put("names", Arrays.asList("111","222","333"));

  Test.jsp

  <%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %>

    <c:forEach items="${names}" var="name">
${name}<br>
</c:forEach>

  

课时16

  • 文件上传

第一步:在WEB-INF/lib下加入commons-fileupload-1.2.1.jar、commons-io-1.3.2.jar。这两个文件可以从http://commons.apache.org/下载。

    第二步:把form表的enctype设置为:“multipart/form-data“,默认情况下,enctype的值是application/x-www-form-urlencoded,不能用于文件上传;只有使用了multipart/form-data,才能完整的传递文件数据。如下:

<form action="${pageContext.request.contextPath}/test/list_execute.action" enctype="multipart/form-data"  method="post">
文件:<input type="file" name="image">
<input type="submit" value="上传">
</form>

第三步:在Action类中添加以下属性,属性红色部分对应于表单中文件字段的名称:

 package tutorial;

 import java.io.File;
import java.io.IOException; import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionContext; public class HelloWorld {
private File image;//与上传字段名称相同的属性
private String imageFileName;//得到文件名称,上传字段+FileName
//private String imageContentType;  //得到上传文件的类型 public String getImageFileName() {
return imageFileName;
} public void setImageFileName(String imageFileName) {
this.imageFileName = imageFileName;
} public File getImage() {
return image;
} public void setImage(File image) {
this.image = image;
} public String execute() throws IOException {
String realpath = ServletActionContext.getServletContext().getRealPath("/images");//得到“/images”真实路径
System.out.println(realpath);
if(image != null) {
File savefile = new File(new File(realpath),imageFileName);
if(!savefile.getParentFile().exists()) { //创建目录
savefile.getParentFile().mkdirs();
}
FileUtils.copyFile(image, savefile);//拷贝文件
ActionContext.getContext().put("msg", "上传成功");
}
return "success";
} }

  默认大小为2097152字节(2M),更改默认大小

<constant name="struts.multipart.maxSize" value="10701096"></constant>

  

课时17

  • 多文件上传
文件1:<input type="file" name="image"><br/>
文件2:<input type="file" name="image"><br/>
文件3:<input type="file" name="image"><br/>

  数组或者list

 public class HelloWorld  {
private File[] image;//与上传字段名称相同的属性
private String[] imageFileName;//得到文件名称,上传字段+FileName
// private String imageContentType; //得到上传文件的类型 public File[] getImage() {
return image;
} public void setImage(File[] image) {
this.image = image;
} public String[] getImageFileName() {
return imageFileName;
} public void setImageFileName(String[] imageFileName) {
this.imageFileName = imageFileName;
} public String execute() throws IOException {
String realpath = ServletActionContext.getServletContext().getRealPath("/images");//得到“/images”真实路径
System.out.println(realpath);
if(image != null) {
File savedir = new File(realpath);
if(!savedir.exists()) {
savedir.mkdirs();
}
for(int i = 0; i < image.length; i++) {
File savefile = new File(savedir,imageFileName[i]);
FileUtils.copyFile(image[i], savefile);//拷贝文件
}
ActionContext.getContext().put("msg", "上传成功");
}
return "success";
} }

最新文章

  1. js常见报错之Unexpected token in JSON at position
  2. BZOJ1097: [POI2007]旅游景点atr
  3. 使用MacBook Air的4项基本技巧
  4. windows下IIS+PHP解决大文件上传500错问题
  5. Sharepoint学习笔记—习题系列--70-576习题解析 -(Q25-Q28)
  6. PHP&amp;MySQL(二)——困也得啃书
  7. iOS学习37数据处理之CoreData
  8. myeclipse注册码生成器
  9. javascript动态添加本地文件列表信息
  10. Qt: 网络编程之UDP(理论+实例)
  11. Hackerrank 2020 February 2014 解题报告
  12. a标签增加onclick事件提示未定义function
  13. 『安全科普』HTTP协议讲解及手工模拟发送
  14. 行业百科知识--Github
  15. Ubuntu-升级linux软件源,安装vim/五笔
  16. Inlay技术要求
  17. win7下安装memcached出现failed to install service or service already installed解决办法
  18. GIT入门笔记(9)- git的add和commit机制原理
  19. python 开发练习之 监控
  20. 【Django】不知道为什么就是想学一下 01

热门文章

  1. NoSql存储日志数据之Spring+Logback+Hbase深度集成
  2. implement Google&#39;s Open Source Slam &quot;Cartographer&quot; demos in ROS/rviz
  3. 问题处理:找不到Pch预编译文件?
  4. nginx安装配置域名转发
  5. 49个jquery代码经典片段
  6. 美团网基于机器学习方法的POI品类推荐算法
  7. Nexus4_文件名乱码
  8. Python学习笔记8—语句
  9. java中compareTo和compare方法之比较
  10. Android 图片Exif信息相关的获取与修改