课时15
- 访问或添加request/session/application属性
- 1.简单说 page指当前页面。在一个jsp页面里有效
2.request 指从http请求到服务器处理结束,返回响应的整个过程。在这个过程中使用forward方式跳转多个jsp。在这些页面里你都可以使用这个变量。
3.Session 有效范围当前会话,从浏览器打开到浏览器关闭这个过程。
4.application它的有效范围是整个应用。
ActionContext和ServletActionContext区别
1 public String execute() { 2 ActionContext ac = ActionContext.getContext(); 3 ac.getApplication().put("app", "应用范围"); 4 ac.getSession().put("ses","session范围"); 5 ac.put("req","request范围"); 6 7 return "success"; 8 }
${applicationScope.app}<br> ${sessionScope.ses}<br> ${requestScope.req}
- 获取HttpServletRequest / HttpSession / ServletContext / HttpServletResponse对象
方法一,通过ServletActionContext.类直接获取:
1 public String rsa() throws Exception{ 2 HttpServletRequest request = ServletActionContext.getRequest(); 3 ServletContext servletContext = ServletActionContext.getServletContext(); 4 request.setAttribute("req", "请求范围属性"); 5 request.getSession().setAttribute("ses", "会话范围属性"); 6 servletContext.setAttribute("app", "应用范围属性"); 7 //HttpServletResponse response = ServletActionContext.getResponse(); 8 9 return "success"; 10 }
方法二,实现指定接口,由struts框架运行时注入:
1 public class HelloWorldAction implements ServletRequestAware, ServletResponseAware, ServletContextAware{ 2 private HttpServletRequest request; 3 private ServletContext servletContext; 4 private HttpServletResponse response; 5 public void setServletRequest(HttpServletRequest req) { 6 this.request=req; 7 } 8 public void setServletResponse(HttpServletResponse res) { 9 this.response=res; 10 } 11 public void setServletContext(ServletContext ser) { 12 this.servletContext=ser; 13 }
- 放集合到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类中添加以下属性,属性红色部分对应于表单中文件字段的名称:
1 package tutorial; 2 3 import java.io.File; 4 import java.io.IOException; 5 6 import org.apache.commons.io.FileUtils; 7 import org.apache.struts2.ServletActionContext; 8 9 import com.opensymphony.xwork2.ActionContext; 10 11 12 13 public class HelloWorld { 14 private File image;//与上传字段名称相同的属性 15 private String imageFileName;//得到文件名称,上传字段+FileName 16 //private String imageContentType; //得到上传文件的类型 17 18 public String getImageFileName() { 19 return imageFileName; 20 } 21 22 23 public void setImageFileName(String imageFileName) { 24 this.imageFileName = imageFileName; 25 } 26 27 28 public File getImage() { 29 return image; 30 } 31 32 33 public void setImage(File image) { 34 this.image = image; 35 } 36 37 38 public String execute() throws IOException { 39 String realpath = ServletActionContext.getServletContext().getRealPath("/images");//得到“/images”真实路径 40 System.out.println(realpath); 41 if(image != null) { 42 File savefile = new File(new File(realpath),imageFileName); 43 if(!savefile.getParentFile().exists()) { //创建目录 44 savefile.getParentFile().mkdirs(); 45 } 46 FileUtils.copyFile(image, savefile);//拷贝文件 47 ActionContext.getContext().put("msg", "上传成功"); 48 } 49 return "success"; 50 } 51 52 }
默认大小为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
1 public class HelloWorld { 2 private File[] image;//与上传字段名称相同的属性 3 private String[] imageFileName;//得到文件名称,上传字段+FileName 4 // private String imageContentType; //得到上传文件的类型 5 6 7 8 public File[] getImage() { 9 return image; 10 } 11 12 13 14 public void setImage(File[] image) { 15 this.image = image; 16 } 17 18 19 20 public String[] getImageFileName() { 21 return imageFileName; 22 } 23 24 25 26 public void setImageFileName(String[] imageFileName) { 27 this.imageFileName = imageFileName; 28 } 29 30 31 32 public String execute() throws IOException { 33 String realpath = ServletActionContext.getServletContext().getRealPath("/images");//得到“/images”真实路径 34 System.out.println(realpath); 35 if(image != null) { 36 File savedir = new File(realpath); 37 if(!savedir.exists()) { 38 savedir.mkdirs(); 39 } 40 for(int i = 0; i < image.length; i++) { 41 File savefile = new File(savedir,imageFileName[i]); 42 FileUtils.copyFile(image[i], savefile);//拷贝文件 43 } 44 ActionContext.getContext().put("msg", "上传成功"); 45 } 46 return "success"; 47 } 48 49 }