前言
前面Struts博文基本把Struts的配置資訊講解完了.....本博文主要講解Struts對資料的處理
Action開發的三種方式
在第一次我們寫開發步驟的時候,我們寫的Action是繼承着ActionSupport類的...為啥我們繼承了ActionSupport類呢?下面我就會講解到
繼承ActionSupport類
我們來看一下ActionSupport幹了什麼:
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIn5GcuYWN4gDZkN2MzgDN4YWMkdjMkZ2M0IDOxU2MyQWO1EDOfdWbp9CXt92Yu4GZjlGbh5SZslmZxl3Lc9CX6MHc0RHaiojIsJye.png)
也就是說,如果我們在Action類中需要用到Struts為我們提供的資料校驗等Struts已經幫我們實作的功能,我們就繼承着ActionSupport類..
實作Action接口
我們再來看看Action接口幹了什麼:
當然啦,ActionSuppot也繼承着Action接口,是以ActionSuppot擁有Action接口的全部功能....是以,這種開發方式我們是比較少用的...
不繼承任何類、不實作任何接口
開發此類的Action,它是不繼承任何類、不實作任何接口的...也就是說,它就是一個普通的Java類....
- Action類
public class PrivilegeAction {
public String login() {
System.out.println("我是普通的javaAction,不繼承任何的類、不實作任何的接口");
return "success";
}
}
- 在配置檔案中配置:
<struts>
<package name="privilige" extends="struts-default">
<action name="login" class="privilegeaction.PrivilegeAction" method="login">
<result name="success">/index.jsp</result>
</action>
</package>
</struts>
- 效果:
小總結
- 如果我們使用到了Struts2一些特用的功能,我們就需要繼承ActionSupport
- 如果我們沒用到Struts2的特殊功能,隻要平凡寫一個Java類行了。
- 大多情況下,我們還是會繼承ActionSupport的。
請求資料封裝
一般地,我們使用Servlet的時候都是分為幾個步驟的:
- 得到web層的資料、封裝資料
- 調用service層的邏輯業務代碼
- 将資料儲存在域對象中,跳轉到對應的JSP頁面
現在問題來了,我們自己編寫的Action類是沒有request、response、Session、application之類的對象的....我們是怎麼得到web層的資料、再将資料存到域對象中的呢??
前面已經說過了,Struts預先幫我們完成了對資料封裝的功能,它是通過params攔截器來實作資料封裝的
<interceptor name="params" class="com.opensymphony.xwork2.interceptor.ParametersInterceptor"/>
register.jsp
首先,我們填寫表單頁面的資料,請求Action處理資料
<form action="${pageContext.request.contextPath}/date01" method="post">
使用者名:<input type="text" name="username"><br>
密碼:<input type="text" name="psd"><br>
年齡:<input type="text" name="age"><br>
生日:<input type="text" name="birthday"><br>
<input type="submit" value="注冊"><br>
</form>
Action封裝基本資訊
在Action設定與JSP頁面相同的屬性,并為它們編寫setter方法
private String username;
private String psd;
private int age;
private Date birthday;
public void setUsername(String username) {
this.username = username;
}
public void setPsd(String psd) {
this.psd = psd;
}
public void setAge(int age) {
this.age = age;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
我們直接在業務方法中通路這些變量,看是否能得到表單的值。
Action封裝對象
一般地,我們注冊的時候,都是在Servlet上把基本資訊封裝到對象上...那麼在Struts怎麼做呢?
- 建立一個User類,基本的資訊和JSP頁面是相同的。
package qwer;
import java.util.Date;
/**
* Created by ozc on 2017/4/27.
*/
public class User {
private String username;
private String psd;
private int age;
private Date birthday;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPsd() {
return psd;
}
public void setPsd(String psd) {
this.psd = psd;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
- 在Action中定義User對象出來,并給出setter和getter方法....值得注意的是:基本資訊隻要setter就夠了,封裝到對象的話,需要setter和getter
public class ccAction extends ActionSupport {
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String register() {
System.out.println(user.getUsername());
System.out.println(user.getPsd());
System.out.println(user.getAge());
System.out.println(user.getBirthday());
return "success";
}
}
- 在JSP頁面,送出的name要寫成
之類的user.username
<form action="${pageContext.request.contextPath}/register" method="post">
使用者名:<input type="text" name="user.username"><br>
密碼:<input type="text" name="user.psd"><br>
年齡:<input type="text" name="user.age"><br>
生日:<input type="text" name="user.birthday"><br>
<input type="submit" value="注冊"><br>
</form>
得到域對象
Struts怎麼把資料儲存在域對象中呢???Struts提供了三種方式
一、得到Servlet API
我們可以通過ServletActionContext得到Servlet API
由于每個使用者擁有一個Action對象,那麼底層為了維護使用者拿到的是目前線程的request等對象,使用ThreadLocal來維護目前線程下的request、response等對象...
//通過ServletActionContext得到Servlet API
javax.servlet.ServletContext context = ServletActionContext.getServletContext();
HttpServletRequest request = ServletActionContext.getRequest();
HttpSession session = request.getSession();
HttpServletResponse response = ServletActionContext.getResponse();
二、ActionContext類
我們還可以通過ActionContext類來得到request、response、session、application被Struts封裝的Map集合
//得到ActionContext 對象
ActionContext context = ActionContext.getContext();
Map<String, Object> session = context.getSession();
Map<String, Object> application = context.getApplication();
//這是request的Map
Map<String, Object> request = context.getContextMap();
三、實作接口
當web容器發現該Action實作了Aware接口,會把相對應的資源通過Aware接口注射進去,實際上就是一種IOC。
Aware實際就是一種攔截器,攔截代碼在執行Action之前執行、将資源注射到Action中
實作SessionAware, RequestAware, ApplicationAware接口,它就要在程式中實作三個方法:
private Map<String, Object> request;
private Map<String, Object> session;
private Map<String, Object> application;
@Override
public void setApplication(Map<String, Object> map) {
this.application = map;
}
@Override
public void setRequest(Map<String, Object> map) {
this.request = map;
}
@Override
public void setSession(Map<String, Object> map) {
this.session = map;
}
通過這些方法,我們就可以得到對應的Map對象.....
那麼,我們有三種方法可以得到Servlet對應的對象,那麼該使用哪一種呢???
分析:
- 第一種方法:需要導入Servlet的包,與Struts耦合了
- 第二種方法:隻能在業務方法中使用ActionContext類得到對應的Map對象,如果有多個方法,那麼每個方法都需要寫類似的代碼
- 第三種方法:可以在類上定義成員變量,以至于整個類都能使用。但是需要實作類、實作對應的方法
如果我們需要使用到對象的其他方法,類似getContextPath()之類的,那麼隻能使用第一種
如果我們就按照平常的開發,我們就使用第二種【擷取簡單,沒有耦合】
至于第三種,當我們将來可能開發BaseAction的時候,就使用它!
日期轉換問題
前面博文已經講解了,Struts2為我們實作了資料自動封裝...由上篇的例子我們可以看出,表單送出過去的資料全都是String類型的,但是經過Struts自動封裝,就改成是JavaBean對應成員變量的類型了。
但是呢,日期類型隻支援是yyyy-MM-dd這種格式的,因為我們在上個例子中直接使用的是Struts支援的格式,是以沒有報錯...本篇博文就是講解Struts如何對日期類型的格式更好地支援
當我們使用的是yyyyMMdd這種格式的時候,我們看看Struts的自動封裝能不能解析出相對應的日期
直接抛出了異常
分析
那麼,我們怎麼讓Struts能夠支援更多的日期格式呢??比如,我想Struts在自動封裝資料的時候支援yyyyMMdd,yyyy年MM月dd日這樣的日期格式.....
Struts提供了轉換器給我們使用,也就是,我們可以自定義轉換器,我們定義了什麼格式,Struts就可以根據對應的格式進行自動封裝...
當我們寫完自定義轉換器,是需要向Struts說明我們寫了,不然的話,Struts是不知道我們自定義了轉換器類的...
也就是說,我們要想實作類型轉換,需要兩步:
- 編寫自定義轉換器類
- 告訴Struts我們寫了轉換器類
自定義轉換器類
一般地,我們想要編寫自定義轉換器類,都是實作StrutsTypeConverter類的....
/**
* Created by ozc on 2017/5/1.
* 自定義異常轉換器類
*
* 我們要實作的就是:在Struts轉換的時候,
*
*/
public class MyConvter extends StrutsTypeConverter {
//需求,當Struts自動封裝資料時,也支援yyyyMMdd,yyyy年MM月dd日等格式的支援\
SimpleDateFormat[] format = {new SimpleDateFormat("yyyy-MM-dd"), new SimpleDateFormat("yyyyMMdd"), new SimpleDateFormat("yyyy年MM月dd日")};
/**
* 把String轉換為指定的類型 【String To Date】
*
*
* @param map
* 目前上下文環境
* @param strings
* jsp表單送出的字元串的值
* @param aClass
* 要轉換為的目标類型
*/
@Override
public Object convertFromString(Map map, String[] strings, Class aClass) {
//判斷是否有值
if (strings == null) {
return null;
}
//判斷是否是日期類型的
if (Date.class != aClass) {
return null;
}
//周遊循環
for (SimpleDateFormat dateFormat : format) {
try {
//解析傳遞進來的第一個就行啦
dateFormat.parse(strings[0]);
} catch (ParseException e) {
//如果格式不對,那麼就跳出目前的循環
continue;
}
}
return null;
}
@Override
public String convertToString(Map map, Object o) {
return null;
}
}
告訴Struts,我寫了轉換器類
告訴Struts我寫了一個轉換器類,也分兩種方式
- 定義了局部轉換器類,就目前包下的Action類有效
- 定義了全局轉換器類,整個項目有效
全局轉換器
步驟:
- 在src目錄下建立一個名為
的檔案xwork-conversion.properties
- 配置檔案的内容:需要轉換的類類型=轉換器類的全名
java.util.Date=qwer.MyConvter
局部轉換器類
- 在目前的Action包下建立名為
Action名-conversion.properties
- 檔案的内容為:需要轉換的字段【如果是JavaBean裡的字段,需要寫上JavaBean的】=轉換器類的全名
user.birthday=qwer.MyConvter
效果
錯誤提示頁面
當發生了日期轉換的異常時,Struts給出的頁面是這樣子的:
這個我們稱之為input視圖,我們要做的就是給出使用者更友好的提示,于是在struts.xml檔案中配置:如果傳回的是input視圖,那麼跳轉到我們相對應的頁面上
<result name="input">/error.jsp</result>
檔案上傳和下載下傳
在講解開山篇的時候就已經說了,Struts2架構封裝了檔案上傳的功能........本博文主要講解怎麼使用Struts架構來完成檔案上傳和下載下傳
回顧以前的檔案上傳
首先,我們先來回顧一下以前,我們在web中上傳檔案是怎麼做的....
http://blog.csdn.net/hon_3y/article/details/66975268可以使用FileUpload或者SmartUpload元件來完成檔案上傳的功能。但是呢,FileUpload元件使用起來是比較麻煩的...而SmartUPload解決中文的問題也非常麻煩
使用Struts進行檔案上傳
從要導入的jar包我們就可以知道:Struts内部還是使用fileUpload上傳元件....但是它極大的簡化地我們的具體操作
那我們怎麼用它呢??看下面的圖
- 在Action中使用在表單中定義的name,就可以擷取代表的上傳檔案的File對象
- 在Action中使用在表單中定義的name+FileName,就得到上傳檔案的名字
JSP頁面
在注冊頁面上擁有兩個上傳檔案控件
<form action="${pageContext.request.contextPath}/register" method="post" enctype="multipart/form-data">
<input type="file" name="photo"><br>
<input type="file" name="photo1"><br>
<input type="submit" value="注冊"><br>
</form>
Action
得到相對應的File對象、上傳檔案名稱、上傳檔案的類型
package fileupload;
import java.io.File;
/**
* Created by ozc on 2017/5/2.
*/
public class FileUploadAction {
//上傳檔案對應的File對象
private File photo;
private File photo1;
//得到上傳檔案的名稱
private String photoFileName;
private String photo1FileName;
//得到上傳檔案的類型
private String photoContentType;
private String photo1ContentType;
//給出相對應的setter
public void setPhoto(File photo) {
this.photo = photo;
}
public void setPhoto1(File photo1) {
this.photo1 = photo1;
}
public void setPhotoFileName(String photoFileName) {
this.photoFileName = photoFileName;
}
public void setPhoto1FileName(String photo1FileName) {
this.photo1FileName = photo1FileName;
}
public void setPhotoContentType(String photoContentType) {
this.photoContentType = photoContentType;
}
public void setPhoto1ContentType(String photo1ContentType) {
this.photo1ContentType = photo1ContentType;
}
public String register() {
System.out.println(photo1FileName);
System.out.println(photoFileName);
return "success";
}
}
成功得到資料:
Action業務代碼:
public String register() throws IOException {
//得到上傳的路徑
String path = ServletActionContext.getServletContext().getRealPath("upload");
System.out.println(path);
//建立檔案對象
File destFile = new File(path,photoFileName);
//調用工具類方法,将檔案拷貝過去
FileUtils.copyFile(photo, destFile);
return "success";
}
檔案下載下傳
我們以前是通過設定request消息頭來實作檔案下載下傳的.....那麼在Struts又如何實作檔案下載下傳呢??
我們請求伺服器處理都是通過Action類來完成的,但是呢,Action類的業務方法都是傳回字元串。是以,Struts在
<result>
節點中提供了類型為stream的type值。通過stream來配置相對應的資訊,進而實作下載下傳!
列出所有可以下載下傳的檔案
- Action類的業務方法
public class downLoadAction {
//列出所有可以下載下傳的檔案
public String list() {
//得到upload檔案夾
String path = ServletActionContext.getServletContext().getRealPath("/upload");
//建立file對象
File file = new File(path);
//列出檔案下所有的檔案
File[] files = file.listFiles();
//将這些檔案存到request域中
HttpServletRequest request = ServletActionContext.getRequest();
request.setAttribute("files", files);
return "list";
}
}
- Struts配置檔案
<action name="down_*" class="fileupload.downLoadAction" method="{1}">
<result name="{1}">/list.jsp</result>
<!-- <result name="{1}" type="stream">/index.jsp</result>-->
</action>
- JSP顯示頁面
<c:if test="${files==null}">
對不起,沒有下載下傳的頁面
</c:if>
<c:if test="${files!=null}">
<table border="1px">
<tr>
<td>編号</td>
<td>檔案名稱</td>
<td>操作</td>
</tr>
<c:forEach items="${files}" varStatus="file" var="fileName">
<tr>
<td>${file.count}</td>
<%--如果直接寫fileName,輸出的名字帶有路徑,使用EL方法庫來截取--%>
<td>${fn:substringAfter(fileName, "upload\\")}</td>
<td>
<%--使用url标簽來建構url,不然超連結帶有中文,會出現亂碼--%>
<c:url var="url" value="down_downLoad">
<c:param name="fileName">${fn:substringAfter(fileName, "upload\\")}</c:param>
</c:url>
<a href="${url}">下載下傳</a>
</td>
</tr>
</c:forEach>
</table>
</c:if>
- Action代碼:
/**
* 通路Action的業務方法僅僅傳回的是字元串。是以Struts在result節點提供了stream類型的type,
* 指定了stream就代表着我這是要下載下傳的...
* <p>
* 既然要下載下傳檔案,那麼肯定需要幾樣東西:
* 1、檔案名
* 2、代表檔案的流
*/
public String downLoad() {
return "downLoad";
}
//得到要下載下傳的檔案名,Struts提供了自動封裝的功能
private String fileName;
//如果檔案名是中文的,那麼需要手動轉換,因為超連結是get方法送出
public void setFileName(String fileName) throws UnsupportedEncodingException {
fileName = new String(fileName.getBytes("ISO8859-1"), "UTF-8");
this.fileName = fileName;
System.out.println(fileName);
}
//得到代表下載下傳檔案流,該方法由Struts調用
public InputStream getAttrInputStream() {
return ServletActionContext.getServletContext().getResourceAsStream("/upload/" + fileName);
}
//下載下傳時,顯示的名稱【如果是中文,可能會亂碼,是以要URLencode】---->在Struts.xml檔案中通過${}可擷取
public String getDownFileName() throws UnsupportedEncodingException {
fileName = URLEncoder.encode(fileName, "UTF-8");
return fileName;
}
- Struts.xml
<action name="down_*" class="fileupload.downLoadAction" method="{1}">
<result name="{1}">/list.jsp</result>
<result name="downLoad" type="stream">
<!--運作下載下傳的類型,指定為所有的二進制檔案-->
<param name="contentType">application/octet-stream</param>
<!-- 對應的是Action中屬性: 傳回流的屬性【其實就是getAttrInputStream()】 -->
<param name="inputName">attrInputStream</param>
<!-- 下載下傳頭,包括:浏覽器顯示的檔案名 --> <!--${}這裡不是EL表達式-->
<param name="contentDisposition">attachment;filename=${downFileName}</param>
<!-- 緩沖區大小設定 -->
<param name="bufferSize">1024</param>
</result>
</action>
模型驅動
什麼是模型驅動
在Struts2中模型驅動就是用來封裝資料的..完成資料的自動封裝.
為什麼要使用模型驅動?
我們之前就使用過Sturts2的資料自動封裝功能,是用params攔截器完成的...既然有了params攔截器,為啥還要模型驅動??
當我們使用params攔截器完成資料自動封裝的時候,如果要封裝的是JavaBean對象,那麼在web表單中就必須的name寫上
javaBean.屬性名
....
這樣的話,web層和Action層就耦合了...因為在web層必須要知道封裝的JavaBean對象是什麼才能夠實作自動封裝!
而模型驅動就解決了這個問題!即時不知道Action層的JavaBean對象是什麼,也能夠完成資料自動封裝!
模型驅動的實作原理
實作模型驅動功能也是由攔截器完成的,我們來看看攔截器到底做了什麼吧....
<interceptor name="modelDriven" class="com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor"/>
攔截方法的源碼是這樣的:
public String intercept(ActionInvocation invocation) throws Exception {
//得到目前要執行的Action對象
Object action = invocation.getAction();
//判斷該Action對象是否實作了ModelDriven接口
if(action instanceof ModelDriven) {
ModelDriven modelDriven = (ModelDriven)action;
//擷取值棧對象
ValueStack stack = invocation.getStack();
//得到model的對象
Object model = modelDriven.getModel();
//把對象存到值棧對象中
if(model != null) {
stack.push(model);
}
if(this.refreshModelBeforeResult) {
invocation.addPreResultListener(new ModelDrivenInterceptor.RefreshModelBeforeResult(modelDriven, model));
}
}
return invocation.invoke();
}
把model對象放到值棧對象之後,Parameters 攔截器将把表單字段映射到 ValueStack 棧的棧頂對象的各個屬性中.
也就是說,使用模型驅動是需要配合Params攔截器完成的!
使用資料模型驅動
實作ModelDriven接口
- 實作ModelDriven接口,重寫方法....實作接口時,要封裝的對象是什麼,形參類型就給什麼
public class UserAction extends ActionSupport implements ModelDriven<User> {
public String login() {
return SUCCESS;
}
@Override
public User getModel() {
return null;
}
}
對象執行個體化
public class UserAction extends ActionSupport implements ModelDriven<User> {
//這裡一定要執行個體化
User user = new User();
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
@Override
public User getModel() {
return user;
}
}
測試
JSP送出頁面,直接寫上JavaBean對象的屬性就行了..不需要寫上JavaBean對象的名稱!
<form action="${pageContext.request.contextPath}/user_execute">
<table border="1">
<tr>
<td>使用者名:<input type="text" name="username"></td>
</tr>
<tr>
<td> 密碼:<input type="password" name="password"></td>
</tr>
<tr>
<td>電話:<input type="text" name="cellphone"></td>
</tr>
<tr>
<td> 郵箱:<input type="text" name="email"></td>
</tr>
<tr>
<td><input type="submit" value="送出"></td>
</tr>
</table>
</form>
- 在Action業務方法中輸出User對象的資料
@Override
public String execute() throws Exception {
System.out.println(user);
return SUCCESS;
}
如果文章有錯的地方歡迎指正,大家互相交流。習慣在微信看技術文章,想要擷取更多的Java資源的同學,可以關注微信公衆号:Java3y
更多的文章可往:
文章的目錄導航