[TOC]html
前面Struts博文基本把Struts的配置信息講解完了.....本博文主要講解Struts對數據的處理java
在第一次咱們寫開發步驟的時候,咱們寫的Action是繼承着ActionSupport類的...爲啥咱們繼承了ActionSupport類呢?下面我就會講解到web
咱們來看一下ActionSupport幹了什麼:瀏覽器
也就是說,若是咱們在Action類中須要用到Struts爲咱們提供的數據校驗等Struts已經幫咱們實現的功能,咱們就繼承着ActionSupport類..服務器
咱們再來看看Action接口乾了什麼:微信
固然啦,ActionSuppot也繼承着Action接口,因此ActionSuppot擁有Action接口的所有功能....所以,這種開發方式咱們是比較少用的...session
開發此類的Action,它是不繼承任何類、不實現任何接口的...也就是說,它就是一個普通的Java類....app
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>
複製代碼
通常地,咱們使用Servlet的時候都是分爲幾個步驟的:框架
如今問題來了,咱們本身編寫的Action類是沒有request、response、Session、application之類的對象的....咱們是怎麼獲得web層的數據、再將數據存到域對象中的呢??jsp
前面已經說過了,Struts預先幫咱們完成了對數據封裝的功能,它是經過params攔截器來實現數據封裝的
<interceptor name="params" class="com.opensymphony.xwork2.interceptor.ParametersInterceptor"/>
複製代碼
首先,咱們填寫表單頁面的數據,請求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設置與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;
}
複製代碼
咱們直接在業務方法中訪問這些變量,看是否能獲得表單的值。
通常地,咱們註冊的時候,都是在Servlet上把基本信息封裝到對象上...那麼在Struts怎麼作呢?
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;
}
}
複製代碼
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";
}
}
複製代碼
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提供了三種方式
咱們能夠經過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類來獲得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對應的對象,那麼該使用哪種呢???
分析:
若是咱們須要使用到對象的其餘方法,相似getContextPath()之類的,那麼只能使用第一種
若是咱們就按照日常的開發,咱們就使用第二種【獲取簡單,沒有耦合】
至於第三種,當咱們未來可能開發BaseAction的時候,就使用它!
前面博文已經講解了,Struts2爲咱們實現了數據自動封裝...由上篇的例子咱們能夠看出,表單提交過去的數據全都是String類型的,可是通過Struts自動封裝,就改爲是JavaBean對應成員變量的類型了。
可是呢,日期類型只支持是yyyy-MM-dd這種格式的,由於咱們在上個例子中直接使用的是Struts支持的格式,所以沒有報錯...本篇博文就是講解Struts如何對日期類型的格式更好地支持
當咱們使用的是yyyyMMdd這種格式的時候,咱們看看Struts的自動封裝能不能解析出相對應的日期
直接拋出了異常
那麼,咱們怎麼讓Struts可以支持更多的日期格式呢??好比,我想Struts在自動封裝數據的時候支持yyyyMMdd,yyyy年MM月dd日這樣的日期格式.....
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我寫了一個轉換器類,也分兩種方式
步驟:
xwork-conversion.properties
的文件java.util.Date=qwer.MyConvter
步驟:
Action名-conversion.properties
的文件user.birthday=qwer.MyConvter
當發生了日期轉換的異常時,Struts給出的頁面是這樣子的:
這個咱們稱之爲input視圖,咱們要作的就是給出用戶更友好的提示,因而在struts.xml文件中配置:若是返回的是input視圖,那麼跳轉到咱們相對應的頁面上
<result name="input">/error.jsp</result>
複製代碼
在講解開山篇的時候就已經說了,Struts2框架封裝了文件上傳的功能........本博文主要講解怎麼使用Struts框架來完成文件上傳和下載
首先,咱們先來回顧一下之前,咱們在web中上傳文件是怎麼作的....blog.csdn.net/hon_3y/arti…
可使用FileUpload或者SmartUpload組件來完成文件上傳的功能。可是呢,FileUpload組件使用起來是比較麻煩的...而SmartUPload解決中文的問題也很是麻煩
從要導入的jar包咱們就能夠知道:Struts內部仍是使用fileUpload上傳組件....可是它極大的簡化地咱們的具體操做
那咱們怎麼用它呢??看下面的圖
在註冊頁面上擁有兩個上傳文件控件
<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>
複製代碼
獲得相對應的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";
}
}
複製代碼
成功獲得數據:
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來配置相對應的信息,從而實現下載!
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";
}
}
複製代碼
<action name="down_*" class="fileupload.downLoadAction" method="{1}">
<result name="{1}">/list.jsp</result>
<!-- <result name="{1}" type="stream">/index.jsp</result>-->
</action>
複製代碼
<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的業務方法僅僅返回的是字符串。所以Struts在result節點提供了stream類型的type, * 指定了stream就表明着我這是要下載的... * <p> * 既然要下載文件,那麼確定須要幾樣東西: * 一、文件名 * 二、表明文件的流 */
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;
}
複製代碼
<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攔截器完成的!
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>
複製代碼
@Override
public String execute() throws Exception {
System.out.println(user);
return SUCCESS;
}
複製代碼
若是文章有錯的地方歡迎指正,你們互相交流。習慣在微信看技術文章,想要獲取更多的Java資源的同窗,能夠關注微信公衆號:Java3y