Struts2学习总结三
Struts2框架OGNL與文件上傳以及防止表單重復(fù)提交
- Struts2學(xué)習(xí)總結(jié)三
- 1、OGNL表達(dá)式
- 1.1、什么是OGNL。
- 1.2、OGNL的3個(gè)符號(hào)。
- 1.2.1、#符號(hào)
- 1.2.1.1、訪問非root對(duì)象
- 1.2.1.1、用于過濾和投影集合
- 1.2.1.1、構(gòu)造Map集合
- 1.3、OGNL的功能。
- 1.3.1、支持普通方法調(diào)用
- 1.3.2、訪問靜態(tài)成員(方法,字段)
- 1.3.2.1、訪問靜態(tài)屬性
- 1.3.2.2、訪問靜態(tài)方法
- 1.3.2.3、調(diào)用Math靜態(tài)
- 1.3.3、操作集合對(duì)象
- 1.4、OGNL常用標(biāo)簽
- s:iterator標(biāo)簽
- s:if標(biāo)簽
- 1.5、不常用標(biāo)簽
- 2、文件上傳
- 2.1、文件上傳配置
- 2.1.1、創(chuàng)建FileUploadAction類
- 2.1.2、配置文件上傳大小
- 2.1.3、限制文件上傳類型
- 2.1.4、限制文件上傳拓展名
- 2.1.5、編寫index.jsp
- 2.1.6測(cè)試結(jié)果
- 2.2、文件上傳錯(cuò)誤消息配置
- 2.3、多文件上傳
- 2.3.1、修改FileUploadAction類
- 2.3.2、修改index.jsp
- 2.3.3、修改success.jsp
- 2.3.4、測(cè)試
- 2.4、文件下載
- 2.4.1、編寫DownloadAction類
- 2.4.2、編寫download.jsp
- 2.4.3、編寫struts.xml
- 2.4.4、測(cè)試
- 3、防止表單重復(fù)提交
- 至此,所有的Struts2的基本內(nèi)容總結(jié)完畢。那些國際化七七八八的就算了,現(xiàn)在很多企業(yè)都不用Struts2了,還管那些干啥= =!
- 參考
Struts2學(xué)習(xí)總結(jié)三
其實(shí),寫到這里,Struts2早已經(jīng)被淘汰!雖然面試的時(shí)候還會(huì)面試有關(guān)于struts2的相關(guān)內(nèi)容。不過開發(fā)已經(jīng)不作為重點(diǎn)了。所以接下在幾個(gè)點(diǎn),就略略的概括掉一下就行了!
今天寫這么幾個(gè)點(diǎn):
1、OGNL表達(dá)式
分幾個(gè)點(diǎn)介紹一下:
1.1、什么是OGNL。
OGNL(Object-Graph Navigation Language,對(duì)象圖導(dǎo)航語言),它是一個(gè)功能強(qiáng)大的表達(dá)式語言,用來獲取和設(shè)置Java對(duì)象的屬性以及調(diào)用對(duì)象的方法。它旨在提供一個(gè)更高的更抽象的層次來對(duì)Java對(duì)象圖進(jìn)行導(dǎo)航。
OGNL是Struts2的默認(rèn)表達(dá)語言,OGNL表達(dá)式的計(jì)算是圍繞OGNL上下文(Context)進(jìn)行的,相當(dāng)于一個(gè)Map對(duì)象,見下圖的結(jié)構(gòu):
可以看到值棧是root。
1.2、OGNL的3個(gè)符號(hào)。
OGNL中重要的3個(gè)符號(hào):#、%、$。
1.2.1、#符號(hào)
有以下三個(gè)功能:
1.2.1.1、訪問非root對(duì)象
例如:
<!-- 等同于${sessionScope.user} --> <s:property value="#session.user"/> <!-- 等同于${requestScope.user} --> <s:property value="#request.user"/>1.2.1.1、用于過濾和投影集合
例如(下面的那個(gè)標(biāo)簽類似于c:foreach):
<!-- 三種表現(xiàn)形式:1. ?#:滿足條件的所有2. ^#:滿足條件的第一條3. $#:滿足條件的最后一條--><!-- 假設(shè)有一個(gè)List<User>的集合名為userList并且User有一個(gè)age的屬性--><!-- value=""等價(jià)于c:foreach的items --><!-- OGNL過濾集合的語法為:collection.{?expression} --> <s:iterator value="userList.{?#this.age>17}"><!-- 篩選出大于17歲也就是大于等于18歲的人的用戶集合 --> </s:iterator> <!-- 其他兩個(gè)同理 -->1.2.1.1、構(gòu)造Map集合
例如:
<s:radio list="#{'male':'男','female':'女'}" />1.3、OGNL的功能。
有下面幾個(gè)功能:
1.3.1、支持普通方法調(diào)用
例如:
<s:property value="'hello'.length()"/>1.3.2、訪問靜態(tài)成員(方法,字段)
在使用靜態(tài)之前,需要在struts.xml文件中配置這個(gè)常量值:<constant name="struts.ognl.allowStaticMethodAccess" value="true"/>。
1.3.2.1、訪問靜態(tài)屬性
/* 訪問靜態(tài)屬性表達(dá)式的格式為:@[類全名(包括包路徑)]@[值名] */ <s:property value="@包路徑.類名@字段名"/>1.3.2.2、訪問靜態(tài)方法
/* 調(diào)用靜態(tài)方法表達(dá)式的格式為:@[類全名(包括包路徑)]@[方法名] */ <s:property value="@包路徑.類名@方法名(參數(shù)...)"/>1.3.2.3、調(diào)用Math靜態(tài)
<s:property value="@@max(2, 3)" />1.3.3、操作集合對(duì)象
<!-- 假設(shè)值棧中,存在一個(gè)List<User>的集合對(duì)象,名為userList --> <s:property value="userList[0].username"/> <s:property value="userList[1].username"/><!-- 或者值棧中還存在一個(gè)Map<String,String>的集合對(duì)象,名為map其中有這樣的鍵值對(duì):"hello"-"world","struts1"-"struts2"--> <s:property value="map.struts1"/> <s:property value="map.['hello']"/><!-- 訪問map中所有的key和value --> <s:property value="map.keys"/> <s:property value="map.values"/>1.4、OGNL常用標(biāo)簽
s:iterator標(biāo)簽
<!-- 迭代標(biāo)簽假設(shè)值棧中有一個(gè)List<User>,名為userList。--> <s:iterator value="userList" var="user" status="s">索引:<s:property value="#s.index"/> 數(shù)量:<s:property value="#s.count"/> 是否奇數(shù):<s:property value="#s.odd"/> 是否偶數(shù):<s:property value="#s.even"/>用戶名:<s:property value="#user.username"/> </s:iterator>s:if標(biāo)簽
<s:if test="userList[9]!=null">值棧中的userList第10個(gè)元素不為空 </s:if> <s:elseif test="userList!=null">值棧中的userList不為空 </s:elseif> <s:else>值棧中的userList為空 </s:else>1.5、不常用標(biāo)簽
- s:set標(biāo)簽
- s:action標(biāo)簽
- s:url標(biāo)簽
- s:a標(biāo)簽
- 等…
2、文件上傳
分下面幾點(diǎn)說明:
2.1、文件上傳配置
以下幾步:
2.1.1、創(chuàng)建FileUploadAction類
package com.csa.action;import java.io.File; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport;public class FileUploadAction extends ActionSupport {// 創(chuàng)建getter/setter方法,說明存入值棧中!private String url;// 下面三個(gè)字段都是upload.jar提供的,struts要求必須這樣寫!// 上傳的文件private File img;// 文件名,固定寫法 文件變量名+FileNameprivate String imgFileName;// 文件類型,固定寫法 文件變量名+ContentTypeprivate String imgFileContentType;public String upload() {// 1.存儲(chǔ)路徑String dir = ServletActionContext.getServletContext().getRealPath("/WEB-INF/files");File file = new File(dir);if (!file.exists()) {file.mkdirs();}// 2.存儲(chǔ)img.renameTo(new File(file, imgFileName));url = dir + File.separator + imgFileName;return SUCCESS;}public File getImg() {return img;}public void setImg(File img) {this.img = img;}public String getImgFileName() {return imgFileName;}public void setImgFileName(String imgFileName) {this.imgFileName = imgFileName;}public String getImgFileContentType() {return imgFileContentType;}public void setImgFileContentType(String imgFileContentType) {this.imgFileContentType = imgFileContentType;}public String getUrl() {return url;}public void setUrl(String url) {this.url = url;}}2.1.2、配置文件上傳大小
在struts.xml中配置:<constant name="struts.multipart.maxsize" value="10485760"></constant>
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN""http://struts.apache.org/dtds/struts-2.3.dtd"> <struts><!-- 開發(fā)模式 --><constant name="struts.devMode" value="true"></constant><!-- 配置文件上傳大小 --><!-- 10MB --><constant name="struts.multipart.maxsize" value="10485760"></constant><package name="upload" extends="struts-default" namespace="/"><action name="*" class="com.csa.action.FileUploadAction" method="{1}"><result name="success">/success.jsp</result></action></package> </struts>2.1.3、限制文件上傳類型
這個(gè)還不太懂。
2.1.4、限制文件上傳拓展名
這個(gè)也還不太懂。
2.1.5、編寫index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE html> <html><head><title>上傳頁面</title></head><body><form method="post" action="${pageContext.request.contextPath}/upload.action" enctype="multipart/form-data">上傳文件:<input type="file" name="img"/><br/><input type="submit" value="上傳"/></form></body> </html>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"> <html><head><title>上傳成功</title></head><body><h1>路徑:${url}</h1></body> </html>2.1.6測(cè)試結(jié)果
2.2、文件上傳錯(cuò)誤消息配置
這個(gè)也不太懂。
2.3、多文件上傳
修改兩處地方:
2.3.1、修改FileUploadAction類
package com.csa.action;import java.io.File; import java.util.ArrayList; import java.util.List;import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport;public class FileUploadAction extends ActionSupport {// 創(chuàng)建getter/setter方法,說明存入值棧中!private List<String> urlList;// 下面三個(gè)字段都是upload.jar提供的,struts要求必須這樣寫!// 上傳的文件private File[] img;// 文件名,固定寫法 文件變量名+FileNameprivate String[] imgFileName;// 文件類型,固定寫法 文件變量名+ContentTypeprivate String[] imgFileContentType;public String upload() {// 1.存儲(chǔ)路徑String dir = ServletActionContext.getServletContext().getRealPath("/WEB-INF/files");File file = new File(dir);if (!file.exists()) {file.mkdirs();}urlList = new ArrayList<>();// 2.存儲(chǔ)for (int i=0;i<img.length;i++) {img[i].renameTo(new File(file, imgFileName[i]));urlList.add(dir + File.separator + imgFileName[i]);}return SUCCESS;}public List<String> getUrlList() {return urlList;}public void setUrlList(List<String> urlList) {this.urlList = urlList;}public File[] getImg() {return img;}public void setImg(File[] img) {this.img = img;}public String[] getImgFileName() {return imgFileName;}public void setImgFileName(String[] imgFileName) {this.imgFileName = imgFileName;}public String[] getImgFileContentType() {return imgFileContentType;}public void setImgFileContentType(String[] imgFileContentType) {this.imgFileContentType = imgFileContentType;}}2.3.2、修改index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE html> <html><head><title>上傳頁面</title></head><body><form method="post" action="${pageContext.request.contextPath}/upload.action" enctype="multipart/form-data">上傳文件1:<input type="file" name="img"/><br/>上傳文件2:<input type="file" name="img"/><br/><input type="submit" value="上傳"/></form></body> </html>2.3.3、修改success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE html> <html><head><title>上傳成功</title></head><body><s:iterator value="urlList" var="url"><h1>路徑:${url}</h1><!-- ${url}與<s:property value="#url"/>是等價(jià)的!--></s:iterator></body> </html>2.3.4、測(cè)試
2.4、文件下載
分下面幾步:
2.4.1、編寫DownloadAction類
package com.csa.action;import java.io.File; import java.io.FileInputStream; import java.io.InputStream;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport;public class DownLoadAction extends ActionSupport {// 文件名private String fileName;// 文件路徑private static String dir = ServletActionContext.getServletContext().getRealPath("/WEB-INF/files");;// 需要一個(gè)文件輸入流private InputStream fileInputStream;public String download() throws Exception {fileInputStream = new FileInputStream(new File(dir + File.separator + fileName));return "success";}public String downloadPage() {File file = new File(dir);String[] listFiles = file.list();ActionContext.getContext().getValueStack().set("listFiles", listFiles);return "DLpage";}public String getFileName() {return fileName;}public void setFileName(String fileName) {this.fileName = fileName;}public InputStream getFileInputStream() {return fileInputStream;}public void setFileInputStream(InputStream fileInputStream) {this.fileInputStream = fileInputStream;}}2.4.2、編寫download.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE html> <html><head><title>下載頁面</title></head><body><s:iterator value="listFiles" var="file"><a href='${pageContext.request.contextPath}/download.action?fileName=<s:property value="#file"/>'><s:property value="#file"/></a><br/></s:iterator></body> </html>2.4.3、編寫struts.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN""http://struts.apache.org/dtds/struts-2.3.dtd"> <struts><!-- 開發(fā)模式 --><constant name="struts.devMode" value="true"></constant><package name="download" extends="struts-default" namespace="/"><action name="*" class="com.csa.action.DownLoadAction" method="{1}"><!-- 其實(shí)action中也可以依賴注入DI --><!-- <param name="某個(gè)擁有g(shù)et/set的字段"></param> --><!-- 下載其實(shí)就是一個(gè)流 --><result name="success" type="stream"><!-- 需要一個(gè)流對(duì)象 --><param name="inputName">fileInputStream</param><!-- 需要一個(gè)下載的文件名 --><!-- 這里可以使用特定的表達(dá)式,比如$或者OGNL --><param name="contentDisposition">attachment;filename=${fileName}</param><!-- 需要指定下載的類型 --><param name="contentType">application/octet-stream</param></result><result name="DLpage">/download.jsp</result></action></package></struts>2.4.4、測(cè)試
3、防止表單重復(fù)提交
就只有一點(diǎn)(常用,方便):
至此,所有的Struts2的基本內(nèi)容總結(jié)完畢。那些國際化七七八八的就算了,現(xiàn)在很多企業(yè)都不用Struts2了,還管那些干啥= =!
參考
實(shí)驗(yàn)樓Struts2教程
黑馬筆記。
總結(jié)
以上是生活随笔為你收集整理的Struts2学习总结三的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 低幼年龄段在线教育白皮书
- 下一篇: 公司让我试岗