struts2:是一个按MVC模式设计的Web层框架,
MVC是一个设计模式,它强制性的使应用程序的输入、处理和输出分开。使用MVC应用程序被分成三个核心部件:模型、视图、控制器。它们各自处理自己的任务。
(1)视图是用户看到并与之交互的界面.
(2)模型表示企业数据和业务规则.
(3)控制器接受用户的输入并调用模型和视图去完成用户的需求.
1:
加入struts2能力
2:
加入请求
3:
描述回应类Action类
public class TestAction{
public String execute(){
return "success";
}
}
4:
在/struts.xml文件中
<!-- 修改默认尾椎(尾椎默认为action).改变常量struts.action.extension的属性值 -->
<constant name="struts.action.extension" value="do"></constant>
<!-- 包配置,一般一个模块写在一个包中 .
name:代表包名,如果配置文件中出现多个包,包名不能相同
extends:代表继承struts的默认包,继承默认包中所有默认的增强方式
namespace:代表请求模块的名称
action中的method:不写时默认执行execute方法.可以使用method指定本次执行哪个方法
-->
<package name="test" extends="struts-default" namespace="/">
<action name="testAction" class="action.TestAction" method="execute">
<!-- type不写时默认为dispatcher:内部转 redirect:重定向
重动向的访问方式:"${pageContext.request.contextPath}/testAction!query.action?test.id="+id+"&test.name="+name;
-->
<result name="success" type="dispatcher">test/result.jsp</result>
<result name="fail" type="redirect">testAction!test.action</result>
</action>
</package>
5:
加入web能力:对象的获得方式
1.
public class TestAction implements ServletRequestAware,ServletResponseAware{
private HttpServletRequest request;
private HttpServletResponse response;
public void setServletRequest(HttpServletRequest request) {
this.request=request;
}
public void setServletResponse(HttpServletResponse response) {
this.response=response;
}
}
2.
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
HttpSession session = ServletActionContext.getRequest().getSession();
session.setAttribute(key, value);
ServletContext application=ServletActionContext.getServletContext();
application.setAttribute(name, object);
Map session2 = ActionContext.getContext().getSession();
session2.put(key, value);
Map application2 = ActionContext.getContext().getApplication();
application2.put(key, value);
6:
拦截器:(interceptor)
作用:对Action中方法的增强
拦截器与拦截器栈:
拦截器(interceptor):对某一方面的增强
拦截器栈(interceptor-stack):一组拦截器的整体配置
一个包中只能存在一个默认拦截器栈:default-interceptor-ref
public class TestInterceptor implements Interceptor{
public void destroy() {
System.out.println("TestInterceptor被销毁");
}
public void init() {
System.out.println("TestInterceptor被初始化");
}
public String intercept(ActionInvocation arg0) throws Exception {
System.out.println("TestInterceptor_前置增强");
String result = arg0.invoke();
System.out.println("TestInterceptor_后置增强");
return result;
}
}
<package name="test" extends="struts-default" namespace="/">
<interceptors>
<interceptor name="interceptor" class="interceptor.TestInterceptor"></interceptor>
</interceptors>
<action name="testAction" class="action.TestAction">
<interceptor-ref name="interceptor"></interceptor-ref>
<result name="success" type="dispatcher">test/result.jsp</result>
</action>
</package>
7:
文件上传:
页面部分:注意--表单form中的method=post enctype=multipart/form-data
<form action="${pageContext.request.contextPath }/uploadAction!upload.action" method="post" enctype="multipart/form-data" >
<table border="5" align="center">
<tr>
<td>文件名</td>
<td>
<input type="text" name="fileName"/>
</td>
</tr>
<tr>
<td>上传组件</td>
<td>
<input type="file" name="theFile"/>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="上传" />
</td>
</tr>
</table>
</form>
后台Action中:
//定义属性.需要set方法
private String fileName;
private File theFile;
private String theFileFileName;//文件原始名称
public String upload(){
this.uploadFile(fileName,theFile,theFileFileName);
return "success";
}
//文件上传方法
public void uploadFile(String fileName,File file,String protoFileName){
HttpServletRequest request = ServletActionContext.getRequest();
String path = "";
if(fileName==null){
path = request.getRealPath("/upload")+File.separator+UUID.randomUUID().toString()+protoFileName.substring(protoFileName.lastIndexOf("."));
}else{
path = request.getRealPath("/upload")+File.separator+fileName+protoFileName.substring(protoFileName.lastIndexOf("."));
}
FileOutputStream fos = null;
FileInputStream fis = null;
try {
fos = new FileOutputStream(new File(path));
fis = new FileInputStream(file);
byte[] buff = new byte[1024*1024];
int i = 0;
while((i=fis.read(buff)) != -1){
fos.write(buff, 0, i);
fos.flush();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
try {
if(fis!=null){
fis.close();
}
if(fos!=null){
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
8:解决文件上传过大问题:
文件上传默认大小为:1024*1024*2(2M)
(1)在struts.xml文件中加入标签<constant name="struts.multipart.maxSize" value="104857600"></constant>
(2)文件上传异常捕捉:<exception></exception>标签
<action name="uploadAction" class="action.UploadAction">
<exception-mapping result="exception" exception="java.lang.Exception"></exception-mapping>
<result name="exception">file/exception.jsp</result>
</action>
exception.jsp页面:
<script type="text/javascript">
function exception(){
alert("文件过大..请选择小于2M文件上传");
window.location="${pageContext.request.contextPath}/file/upload.jsp";
}
</script>
<body onload="exception()"></body>