Struts2
配置细节问题
struts.xml中默认配置
<default-action-ref name="default"></default-action-ref> <default-class-ref class="com.example.struts2.DefaultAction"> </default-class-ref> <action name="default"> <result name="success">/index.jsp</result> </action>
通用界面配置 —如果某个action中没有对应的result,默认跳转到default.jsp中
<global-results> <result>/default.jsp</result> </global-results>
全局异常
<global-exception-mappings> <exception-mapping result="error" exception="java.lang.Exception" name="ErrorAction"></exception-mapping> </global-exception-mappings>
常用常量配置
<constant name="struts.action.extension" value="action,do,"/> <constant name="struts.devMode" value="true" /> 提供详细报错页面,修改struts.xml后不需要重启服务器 (要求) <constant name="struts.serve.static.browserCache" value="false"/> false不缓存,true浏览器会缓存静态内容,产品环境设置true、开发环境设置false
struts.xml模块分离
<include file="struts-user.xml"></include> 复制 创建struts-user的xml文件
文件上传
企业常用文件上传技术 : jspSmartUpload(主要应用 JSP model1 时代) 、
fileupload (Apache commons项目中一个组件)、
Servlet3.0 集成文件上传 Part类
文件上传 enctype=”multipart/form-data”
是 MIME协议定义多部分请求体 (消息体)
private File upload;
private String uploadContentType;
private String uploadFileName;
public void setUpload(File upload) {
this.upload = upload;
}
public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}
public void setUploadContentType(String uploadContentType) {
this.uploadContentType = uploadContentType;
}
@Override
public String execute() throws Exception {
// 查询
String realPath = ServletActionContext.getServletContext().getRealPath(
"/upload");
// 路径
String targetPath = realPath + File.separator + uploadFileName;
// 开始上传
File targetFile = new File(targetPath);
FileUtils.copyFile(upload, targetFile);
return NONE;
}
多文件上传
private File[] upload;
private String[] uploadContentType;
private String[] uploadFileName;
// 查询
String realPath = ServletActionContext.getServletContext().getRealPath("/upload");
for (int i = 0; i < uploadFileName.length; i++) {
String targetPath = realPath + File.separator + uploadFileName[i];
File targetFile = new File(targetPath);
FileUtils.copyFile(upload[i], targetFile);
}
ServletActionContext.getRequest().setAttribute("name", "上传成功");