<%@ page language="java" pageEncoding="GBK"%> <html> <head> <title>获得Token </title> </head> <body> <h3><a href="tokenforward.do">获取Token,输入数据</a></h3> </body> </html>
编写ActionForm —— TokenforwardForm.java
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.java
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配置文件
<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. |
方法 |
类型 |
描述 |
1 |
public byte[] getFileData() throws FileNotFoundException,IOException |
普通 |
取得上传文件大小 |
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; } }