<%@ page language="java" pageEncoding="GBK"%> <html> <head> <title>得到Token </title> </head> <body> <h3><a href="tokenforward.do">獲取Token,輸入數據</a></h3> </body> </html>
編寫ActionForm —— TokenforwardForm.javahtml
package org.lxh.struts.form; import javax.servlet.http.HttpServletRequest; import org.apache.struts.action.ActionErrors; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionMapping; public class TokenforwardForm extends ActionForm { public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) { return null; } public void reset(ActionMapping mapping, HttpServletRequest request) { } }
定義接收輸入數據的Action —— InputAction.javajava
package org.lxh.struts.action; import org.lxh.struts.form.InputForm; public class InputAction extends Action { public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { InputForm inputForm = (InputForm) form; if (super.isTokenValid(request)) { // 若是設置的Token正確,則輸出內容 System.out.println("輸入內容:" + inputForm.getInfo()); super.resetToken(request); // 取消設置的Token } else { // 沒有Token,應該進行錯誤顯示 ActionMessages errors = new ActionMessages(); // 設置錯誤信息保存 errors.add("token", new ActionMessage("error.token")); // 設置錯誤內容 this.saveErrors(request, errors); // 保存錯誤信息 return mapping.getInputForward(); // 返回到錯誤頁 } return null; } }
修改struts-config.xml配置文件 apache
<form-beans> <form-bean name="tokenforwardForm" type="org.lxh.struts.form.TokenforwardForm" /> <form-bean name="inputForm" type="org.lxh.struts.form.InputForm" /> </form-beans> <action-mappings> <action attribute="tokenforwardForm" input="/ch17/input.jsp" name="tokenforwardForm" path="/ch17/tokenforward" scope="request" type="org.lxh.struts.action.TokenforwardAction"> <forward name="input" path="/ch17/input.jsp"></forward> </action> <action attribute="inputForm" input="/ch17/input.jsp" name="inputForm" path="/ch17/input" scope="request" type="org.lxh.struts.action.InputAction" /> </action-mappings>
編寫資源文件,添加錯誤信息 —— ApplicationResources.properties瀏覽器
# 請不要重複提交!
error.token = \u8bf7\u4e0d\u8981\u91cd\u590d\u63d0\u4ea4\uff01
No.服務器 |
方法app |
類型jsp |
描述post |
1this |
public byte[] getFileData() throws FileNotFoundException,IOExceptionspa |
普通 |
取得上傳文件大小 |
2 |
public InputStream getInputStream() throws FileNotFoundException,IOException |
普通 |
取得上傳文件的輸入流 |
3 |
public int getFileSize() |
普通 |
取得上傳文件的大小 |
4 |
public String getFileName() |
普通 |
取得上傳文件的名稱 |
5 |
public String getContentType() |
普通 |
取得上傳文件的類型 |
<%@ page language="java" pageEncoding="GBK"%> <%@ taglib uri="http://www.mldn.cn/struts/bean" prefix="bean"%> <%@ taglib uri="http://www.mldn.cn/struts/html" prefix="html"%> <%@ taglib uri="http://www.mldn.cn/struts/logic" prefix="logic"%> <html:html lang="true"> <head> <title>文件上傳</title> </head> <body> <html:form action="/ch17/upload.do" method="post" enctype="multipart/form-data"> 請選擇要上傳的文件;<html:file property="photo"/> <html:submit value="上傳"></html:submit> </html:form> </body> </html:html>
package org.lxh.struts.form; import javax.servlet.http.HttpServletRequest; import org.apache.struts.action.ActionErrors; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionMapping; import org.apache.struts.upload.FormFile; public class UploadForm extends ActionForm { private FormFile photo ; // 接收上傳文件 public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) { // 暫不驗證 return null; } public void reset(ActionMapping mapping, HttpServletRequest request) { } public FormFile getPhoto() { return photo; } public void setPhoto(FormFile photo) { this.photo = photo; } }
public class UploadAction extends Action { public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { UploadForm uploadForm = (UploadForm) form; IPTimeStamp its = new IPTimeStamp(request.getRemoteAddr()); // 自動生成文件名 String fileName = its.getIPTimeRand() + "." + uploadForm.getPhoto().getFileName().split("\\.")[uploadForm .getPhoto().getFileName().split("\\.").length - 1];// 生成文件名 File outFile = new File(super.getServlet().getServletContext() .getRealPath("/") + "upload"+ File.separator + uploadForm.getPhoto().getFileName().split("\\.")); // 輸出文件路徑 try{ OutputStream output = new FileOutputStream(outFile) ; // 文件輸出 byte data[] = new byte[1024] ; // 接收文件 int temp = 0 ; // 結束判斷 while ((temp = uploadForm.getPhoto().getInputStream() .read(data, 0, 1024)) != -1) { // 分塊保存 output.write(data) ; // 保存文件 } output.close() ; // 關閉輸出 }catch(Exception e){ e.printStackTrace() ; // 錯誤輸出 } return null; } }
<struts-config> <form-beans> <form-bean name="uploadForm" type="org.lxh.struts.form.UploadForm" /> </form-beans> <action-mappings> <action attribute="uploadForm" input="/upload.jsp" name="uploadForm" path="/upload" scope="request" type="org.lxh.struts.action.UploadAction" /> </action-mappings> </struts-config>
這樣就能夠很方便的完成文件上傳操做。
<struts-config> <form-beans> <form-bean name="newsForm" type="org.apache.struts.action.DynaActionForm"> <form-property name="title" type="java.lang.String"> </form-property> <form-property name="content" type="java.lang.String"> </form-property> </form-bean> </form-beans> <action-mappings> <action attribute="newsForm" input="/ch17/news.jsp" name="newsForm" path="/ch17/news" scope="request" type="org.lxh.struts.action.NewsAction" /> </action-mappings> </struts-config>
<%@ page language="java" pageEncoding="GBK"%> <%@ taglib uri="http://www.mldn.cn/struts/bean" prefix="bean"%> <%@ taglib uri="http://www.mldn.cn/struts/html" prefix="html"%> <%@ taglib uri="http://www.mldn.cn/struts/logic" prefix="logic"%> <html:html lang="true"> <head> <title>動態actionForm</title> </head> <body> <html:form action="/ch17/news.do" method="post"> 標題:<html:text property="title"/><br> 內容:<html:text property="content"/><br> <html:submit value="提交"/><html:reset value="重置"/> </html:form> </body> </html:html>
package org.lxh.struts.action; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.action.DynaActionForm; public class NewsAction extends Action { public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { DynaActionForm dynaForm = (DynaActionForm) form ; String title = dynaForm.getString("title") ; // 取得title輸入內容 String content = dynaForm.getString("content") ; // 取得content輸入內容 System.out.println("title --> " + title) ; // 輸出title內容 System.out.println("content --> " + content); // 輸出content內容 return null; } }