Struts 引入多个文件
<include file="struts_user.xml"></include>
<include file="struts_login.xml"></include>
重定向 : 默认转发:
<result name="add_submit" type="redirect">/user_list.do</result>
<!-- 默认action action找不到时 执行index.jsp页面-->
<default-action-ref name="index"/>
<action name="index">
<result>/index.jsp</result>
</action>
拦截器
<interceptors>
<!-- 定义拦截器
name:拦截器名称
class:拦截器类路径
-->
<interceptor name="timer" class="com.kay.timer"></interceptor>
<interceptor name="logger" class="com.kay.logger"></interceptor>
<!-- 定义拦截器栈 -->
<interceptor-stack name="mystack">
<interceptor-ref name="timer"></interceptor-ref>
<interceptor-ref name="logger"></interceptor-ref>
</interceptor-stack>
</interceptors>
<!-- 定义默认的拦截器 每个Action都会自动引用
如果Action中引用了其它的拦截器 默认的拦截器将无效 -->
<default-interceptor-ref name="mystack"></default-interceptor-ref>
创建自定义拦截器
public class MyInterceptor extends AbstractInterceptor { public String intercept(ActionInvocation invocation)throws Exception{ /* let us do some pre-processing */ String output = "Pre-Processing"; System.out.println(output); /* let us call action or next interceptor */ String result = invocation.invoke(); /* let us do some post-processing */ output = "Post-Processing"; System.out.println(output); return result; } }
文件上传:
jsp 页面:<form action="user_file" method="post" enctype="multipart/form-data">
姓名:<input type="text" name="username"/><br>
密码:<input type="text" name="password"/><br>
<input type="file" name="myfile" ><br>
<input type="submit" value="提交"/><br>
</form>
配置文件上传的最大值
struts.multipart.maxSize=1000000
action :
需要set方法
private File myfile; //上传文件 name值
private String myfileFileName; //文件名 name值+FileName
private String myfileContentType; //文件类型 name值+ContentType
public String file() throws IOException {
System.out.println("myfileFileName:"+myfileFileName+" name:"+username+" "+myfile);
String realpath = ServletActionContext.getServletContext().getRealPath("/images");
if(myfile!=null){
File savefile = new File(new File(realpath), myfileFileName);
if (!savefile.getParentFile().exists())
savefile.getParentFile().mkdirs();
FileUtils.copyFile(myfile, savefile);
System.out.println(savefile);
}
return "file";
}